# This file is part of CWEB.
# This program by Silvio Levy is based on a program by D. E. Knuth.
# It is distributed WITHOUT ANY WARRANTY, express or implied.
#
# Modified for the c89 HP compiler under HP-UX 8.0 on a HP 9000/400 by
# Klaus Guntermann <gunterma@iti.informatik.th-darmstadt.de> and
# Hans-Hermann Bode <HHBODE@DOSUNI1.BITNET>, August 1992.
# Last updated by Klaus Guntermann, August 1992

# Copyright (C) 1987,1990 Silvio Levy and Donald E. Knuth
# Copyright (C) 1992 	Klaus Guntermann

# Permission is granted to make and distribute verbatim copies of this
# document provided that the copyright notice and this permission notice
# are preserved on all copies.

# Permission is granted to copy and distribute modified versions of this
# document under the conditions for verbatim copying, provided that the
# entire resulting derived work is distributed under the terms of a
# permission notice identical to this one.

# 
# Read the readme.c89 file, then edit this file to reflect local conditions
#

# directory for TeX inputs (cwebmac.tex goes here)
MACROSDIR= /usr/local/lib/tex/inputs

# directory for CWEB inputs in @i files
INCLUDEDIR= /usr/local/lib/cweb

# extension for manual pages ("l" distinguishes local from system stuff)
MANEXT= l
#MANEXT= 1

# directory for manual pages (cweb.1 goes here)
MANDIR= /u/man/man$(MANEXT)

# destination directory for executables; must end in /
DESTDIR= /usr/local/bin/

# directory for GNU EMACS Lips code (cweb.el goes here)
EMACSDIR= /usr/local/emacs/lisp

# Set DESTPREF to null if you want to call the executables "tangle" and "weave"
# (probably not a good idea)
DESTPREF=c

# Set CHCHANGES to common-foo.hch if you need changes to common.h
CHCHANGES=common-p.hch

# Set CCHANGES to common-foo.ch if you need changes to common.w
CCHANGES=common-p.ch

# Set TCHANGES to ctangle-foo.ch if you need changes to ctangle.w
TCHANGES=ctangl-p.ch

# Set WCHANGES to cweave-foo.ch if you need changes to cweave.w
WCHANGES=cweave-p.ch

# Set PCHANGES to prod-foo.ch if you need changes to prod.w
PCHANGES=prod-p.ch

# Set MCHANGES to wmerge-foo.ch if you need changes to prod.w
MCHANGES=wmerge-p.ch

# We keep debugging info around, for fun, but most users don't need it
CFLAGS = -DDEBUG -DSTAT

# Set WMERGE such that the wmerge processor can be found.
# It will be needed if you made additional changes to 
# common-p.hch, prod-w.ch, or ccwebmac.ch.
# If you installed a copy, e.g. in /usr/local/bin, and this is in
# you PATH, just call wmerge; but if it is not installed, you may
# have to call it from the subdirectory
WMERGE=./examples/wmerge

# RM and CP are used below in case rm and cp are aliased
RM= /bin/rm
CP= /bin/cp
INSTALL= install
#INSTALL= /bin/cp  # use this if `install' isn't available
CC = c89

##########  You shouldn't have to change anything after this point #######

CWEAVE = ./cweave
CTANGLE = ./ctangle
SOURCES = cweave.w common.w ctangle.w
ALMOSTALL = common.w ctangle.w makefile.org readme.org common.c common.h \
	ctangle.c cwebman.tex cwebmac.tex examples cweb.1 cweb.el prod.w \
	$(CCHANGES) $(CHCHANGES) $(TCHANGES) $(WCHANGES) $(PCHANGES) \
	readme.c89 ccwebmac.ch cweb.man readme.pc makefile.pc
ALL =  $(ALMOSTALL) cweave.w

.SUFFIXES: .tex .dvi .w

.w.tex:
	$(CWEAVE) $*

.tex.dvi:	
	tex $*

.w.dvi:
	make $*.tex
	make $*.dvi

.w.c:
	$(CTANGLE) $*

.w.o:
	make $*.c
	make $*.o

all: ctangle cweave examples/wmerge

cautiously: ctangle
	$(CP) common.c SAVEcommon.c
	./ctangle common $(CCHANGES)
	diff common.c SAVEcommon.c
	$(RM) SAVEcommon.c
	$(CP) ctangle.c SAVEctangle.c
	./ctangle ctangle $(TCHANGES)
	diff ctangle.c SAVEctangle.c
	$(RM) SAVEctangle.c

SAVEctangle.c:
	$(CP) ctangle.c SAVEctangle.c

SAVEcommon.c:
	$(CP) common.c SAVEcommon.c

common.c: common.w $(CCHANGES)
	$(CTANGLE) common $(CCHANGES)

common.o: common.c
	$(CC) $(CFLAGS) -DINCLUDEDIR -c common.c

ctangle: ctangle.o common.o
	$(CC) $(CFLAGS) -o ctangle ctangle.o common.o

ctangle.c: ctangle.w $(TCHANGES) common-p.h
	$(CTANGLE) ctangle $(TCHANGES)

cweave: cweave.o common.o
	$(CC) $(CFLAGS) -o cweave cweave.o common.o

cweave.c: cweave.w $(WCHANGES) common-p.h prod-p.w
	$(CTANGLE) cweave $(WCHANGES)

# additional rules to `wmerge' the remaining files
#
examples/wmerge: examples/wmerge.c
	(cd examples; $(CC) $(CFLAGS) -o wmerge wmerge.c)

examples/wmerge.c: examples/wmerge.w examples/$(MCHANGES)
	(cd examples; ../$(CTANGLE) wmerge $(MCHANGES))

common-p.h: common.h $(CHCHANGES)
	$(WMERGE) common.h $(CHCHANGES) > common-p.h

prod-p.w: prod.w $(PCHANGES)
	$(WMERGE) prod.w $(PCHANGES) > prod-p.w

ccwebmac.tex: cwebmac.tex ccwebmac.ch
	$(WMERGE) cwebmac.tex ccwebmac.ch > ccwebmac.tex

doc:	$(SOURCES) examples/wmerge.w
	for i in $?; do make `echo $$i | sed "s/w$$/dvi/"`; done
	@touch doc

# making the documentation we will have to include the change files
ctangle.tex: ctangle.w $(TCHANGES)
	$(CWEAVE) ctangle $(TCHANGES)

cweave.tex: cweave.w $(WCHANGES)
	$(CWEAVE) cweave $(WCHANGES)

common.tex: common.w $(CCHANGES)
	$(CWEAVE) common $(CCHANGES)

examples/wmerge.tex: examples/wmerge.w
	(cd examples; ../$(CWEAVE) wmerge $(MCHANGES))

# be sure to leave ctangle.c and common.c for bootstrapping
clean:
	$(RM) -f -r *~ *.o common.tex cweave.tex cweave.c ctangle.tex \
	  *.log *.dvi *.toc core cweave.w.[12] cweave ctangle \
	  examples/wmerge.tex examples/wmerge

install: all ccwebmac.tex
	$(INSTALL) cweave $(DESTDIR)$(DESTPREF)weave
	$(INSTALL) examples/wmerge $(DESTDIR)wmerge
	$(INSTALL) ctangle $(DESTDIR)$(DESTPREF)tangle
	$(INSTALL) ccwebmac.tex $(MACROSDIR)
	$(INSTALL) cweb.1 $(MANDIR)/cweb.$(MANEXT)
	$(INSTALL) cweb.el $(EMACSDIR)

bundle: $(ALL)
	sed -n '1,1500 p' cweave.w > cweave.w.1
	sed -n '1501,$$ p' cweave.w > cweave.w.2
	/usr/local/bin/shar -m100000 -c -v -f cweb $(ALMOSTALL) cweave.w.[12]

floppy: $(ALL)
	bar cvf /dev/rfd0 $(ALL)
	bar tvf /dev/rfd0
	eject
