# Makefile for the MetaPost support programs.

SHELL = rx
srcdir = .
VPATH = .

# The missing `configure' mechanism requires this to be done by hand.
bindir = TeXMF:bin/web2c61
rexxdir = TeXMF:web2c61/rexx

# This is so kpathsea will get remade automatically if you change
# something in it and recompile from the package directory.
kpathsea_parent = ../..
kpathsea_dir = $(kpathsea_parent)/kpathsea
kpathsea = $(kpathsea_dir)/kpathsea.lib

# Routines used everywhere.
commondefines = $(srcdir)/../lib/common.defines
commonh = $(srcdir)/../lib/config.h $(kpathsea_dir)/paths.h
commono = ../lib/lib.lib $(kpathsea)

CC = sc
CFLAGS = define=_STRICT_ANSI define=ANSI data=far math=ieee \
	ignore=85+88 noicons optimize
CPPFLAGS = idir=$(srcdir)/../lib idir=../..

CCLD = $(CC)
LDFLAGS = $(CFLAGS) $(XLDFLAGS)
LIBS = $(extralibs)
libs = $(commono) $(LIBS)

TIE = tie
WMERGE = wmerge

INSTALL = copy
INSTALL_PROGRAM = copy
INSTALL_DATA = copy

.SUFFIXES:
.SUFFIXES: .o .c .p .ch
.p.c:
	$(SHELL) $(srcdir)/../lib/convert.rexx $*.p $*.c
.ch.p:
	../web/tangle $*.web $*.ch
.c.o:
	$(CC) $(CPPFLAGS) $(CFLAGS) $*.c

default: all

all: tex troff
tex: dvitomp mptotex newer
troff: dmp mptotr newer

# Don't test mptotr because its source file is identical to mptotex.c
# except for a few string constants.
# This will abort if either diff returns nonzero exit status
# otherwise the test is considered passed.
run-triptrap: mptotex newer
	./mptotex testex.mp > testex.tex.new
	diff testex.tex.new testex.tex
	@delete testex.tex.new
	sed "16s/^%//" <testex.mp >testex1
	-mptotex testex1 -Etestex2.1 >NIL:
	sed "17s/^%//" <testex.mp >testex1
	-mptotex testex1 -Etestex2.2 >NIL:
	sed "18s/^%//" <testex.mp >testex1
	-mptotex testex1 -Etestex2.3 >NIL:
	sed "19s/^%//" <testex.mp >testex1
	-mptotex testex1 -Etestex2.4 >NIL:
	sed "20s/^%//" <testex.mp >testex1
	-mptotex testex1 -Etestex2.5 >NIL:
	join testex2.1 testex2.2 testex2.3 testex2.4 testex2.5 to testex2
	@delete testex2.?
	diff testex.err testex2

# DVItoMP
dvitomp: dvitomp.o $(commono)
	$(CCLD)  link to dvitomp $(LDFLAGS) dvitomp.o $(libs)
dvitomp.o: dvitomp.c $(commonh)
dvitomp.c: dvitomp.p $(commondefines)
dvitomp.p: dvitomp.web andydvitomp.ch
	../web/tangle dvitomp.web andydvitomp.ch
andydvitomp.ch: dvitomp.ch dvitomp.chch
	$(TIE) -c andydvitomp.ch dvitomp.web dvitomp.ch dvitomp.chch

# MPtoTEX
mptotex: andymptotex.o
	$(CCLD) link to mptotex $(LDFLAGS) andymptotex.o
andymptotex.o: andymptotex.c
andymptotex.c: mptotex.c mptotex.ch
	$(WMERGE) mptotex.c mptotex.ch andymptotex.c

# MPtoTR
mptotr: andymptotr.o
	$(CCLD) link to mptotr $(LDFLAGS) andymptotr.o
andymptotr.o: andymptotr.c
andymptotr.c: mptotr.c mptotr.ch
	$(WMERGE) mptotr.c mptotr.ch andymptotr.c

# DMP

SITEDIR = ../mpdist # where the original "site.h" and "default.h" are found

dmp: andydmp.o pathexp.o
	$(CCLD) link to dmp $(LDFLAGS) andydmp.o pathexp.o
andydmp.o: andydmp.c site.h
	$(CC) $(CPPFLAGS) $(CFLAGS) idir=.. idir=$(SITEDIR) andydmp.c
andydmp.c: dmp.c dmp.ch
	$(WMERGE) dmp.c dmp.ch andydmp.c
pathexp.o: ../mp/pathexp.c pathexp.ch
	$(WMERGE) ../mp/pathexp.c pathexp.ch pathexp.c
	$(CC) $(CPPFLAGS) $(CFLAGS) idir=.. idir=$(SITEDIR) pathexp.c

site.h: $(SITEDIR)/site.h site.hch
	$(WMERGE) $(SITEDIR)/site.h site.hch site.h

# newer
newer: andynewer.o
	$(CCLD) link to newer $(LDFLAGS) andynewer.o
andynewer.o: andynewer.c
andynewer.c: newer.c newer.ch
	$(WMERGE) newer.c newer.ch andynewer.c

install: install-exec install-data

install-exec: all
	$(INSTALL_PROGRAM) $(program) $(bindir)
install-data: makempx.rexx
	$(INSTALL_DATA) makempx.rexx $(rexxdir)

clean:
	- delete dvitomp.p dvitomp.pool \#?.o \#?.lnk testex1 testex2
	- delete dvitomp mptotex mptotr dmp newer
veryclean:	clean
	- delete dvitomp.c dvitomp.h \#?.bak \#?.ckp andy\#? \
	site.h pathexp.c
