# Makefile for the WEB programs.
# tangle is bootstrapped from the supplied C source.

SED = sed
SHELL = rx
WMERGE = wmerge
srcdir = .
VPATH = .

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

# 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)

program = tangle weave pooltype

CC = sc
CFLAGS = data=far code=far math=IEEE noicons optimize
CPPFLAGS = idir=$(srcdir)/../lib idir=../..

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

INSTALL = copy 
INSTALL_PROGRAM = copy

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

default: all
all: $(program)
triptrap: tangle

tangleboot: tangleboot.o $(commono)
	$(CCLD) link to tangleboot $(LDFLAGS) tangleboot.o $(libs)

tangleboot.o: tangleboot.c tangleboot.h

tangleboot.c tangleboot.h: tangleboot.p
	$(SHELL) $(srcdir)/../lib/convert.rexx tangleboot.p temp.c
	$(SED) s/tangle.h/tangleboot.h/ < temp.c > tangleboot.c
	delete temp.c

# To make tangleboot.p, we must assume a working tangle.
tangleboot.p: tangle.web tangle.ch
	tangle tangle.web tangle.ch
	copy tangle.p tangleboot.p
	delete tangle.p

tangle: tangle.o $(commono)
	$(CCLD) link to tangle $(LDFLAGS) tangle.o $(libs)
tangle.o: tangle.c $(commonh)
tangle.c: tangle.p $(commondefines)

# Making tangle.p is a special case, since it is needed to compile
# itself.  We compile the (distributed) tangleboot.c to make a tangle
# which we can use to make the other programs.  (We don't bother to make
# tangle again using itself.)
tangle.p: tangleboot tangle.web andytangle.ch
	./tangleboot tangle.web andytangle.ch

tangle.web:
	@echo "You seem to be missing tangle.web, perhaps because you"
	@echo "only retrieved web2c.tar.gz, and not web.tar.gz."
	@echo "You need both."
	@echo
	@echo "web.tar.gz should be available from the"
	@echo "same place that you got web2c.tar.gz."
	@echo "In any case, you can get it from ftp.cs.umb.edu:pub/tex."
	exit 1

andytangle.ch: tangle.ch tangle.chch
	$(WMERGE) tangle.ch tangle.chch andytangle.ch

andyweave.ch: weave.ch weave.chch
	$(WMERGE) weave.ch weave.chch andyweave.ch

andypooltype.ch: pooltype.ch pooltype.chch
	$(WMERGE) pooltype.ch pooltype.chch andypooltype.ch

pooltype: pooltype.o $(commono)
	$(CCLD) link to pooltype $(LDFLAGS) pooltype.o $(libs)
pooltype.o: pooltype.c $(commonh)
pooltype.c: pooltype.p $(commondefines)
pooltype.p: pooltype.web andypooltype.ch
	./tangle pooltype.web andypooltype.ch

weave: weave.o $(commono)
	$(CCLD) link to weave $(LDFLAGS) weave.o $(libs)
weave.o: weave.c $(commonh)
weave.c: weave.p $(commondefines)
weave.p: weave.web andyweave.ch
	./tangle weave.web andyweave.ch

c-sources: pooltype.c tangle.c weave.c
.PHONY: c-sources

install install-exec: all
	$(INSTALL_PROGRAM) $(program) $(bindir)
install-data:

# Can't remove *.p *.c *.h because of tangleboot.
clean:
	- delete \#?.(dvi|pool) pooltype.(p|c|h) tangle.(p|c|h) weave.(p|c|h)

veryclean: clean
	- delete \#?.(lnk|o|bak) andy\#?.ch

extraclean: veryclean
	- delete $(program) tangleboot

# Do not remove tangleboot.[chp] at the cleanup steps, even though there
# are targets for them in this Makefile, because we need a working tangle
# to use them, and we might not have one.
