# Overall Makefile for making TeX, Metafont, and friends, using WEB to C.
# 
#
# TeX 3.1 + Metafont 2.7 + web2c a.
# 
version=5.8a

# Directory that site.h is in relative to subdirectories, i.e., `..'
# means the directory containing this Makefile.
SITEDIR=..

# Directory that site.h is in relative to the current directory, i.e.,
# `.' means the directory containing this Makefile.
siteh=./site.h
# Default C compiler.
CC=cc

# Default flags to give the C compiler.  (Don't define CFLAGS directly.)
OPT=-g -O

# Default flags to give to the loader, including libraries.  (LDFLAGS is
# given last on the command line.)
LDFLAGS=

# Make sure we use the sh.  If your sh is broken, you might try bash,
# the GNU replacement.
SHELL=/bin/sh

# The yacc program to use on web2c/web2c.yacc.
YACC=bison -y

# The lex program to use on web2c/web2c.lex.
LEX=lex
# -ll for lex, -lfl for flex.
LEXLIB=-ll

# Need -D_POSIX_SOURCE here, perhaps.
LEXCFLAGS=

# Support libraries for Metafont.
x10lib=-lX
x11lib=-lXt -lX11 -lXext
sunlib=-lsuntool -lsunwindow -lpixrect

# This line should select the libraries that match what you said in site.h.
wlibs=

# These lines define the format and base files that `make fmts' and
# `make bases' will try to make, and `make install' will try to install.
formats=tex.fmt latex.fmt
bases=mf.base

# The name of the file that defines your local devices, for use with
# Metafont.  (Only relevant during `make bases'.)
localmodes=waits

# You may need INSTALL=cp on system V, if you don't have GNU install. 
# Or you can install the programs by hand.
# $(fileinstall) is used for the format and base files, man files, and
# other non-executables, and $(programinstall) is used for
# the executables.
INSTALL=install -g tex
fileinstall=$(INSTALL) -m  644
proginstall=$(INSTALL) -m  755 -c

# These pathnames are used to customize the manual pages with local
# directory names; some of them are used in `make install'.  They should
# match the system directories in the paths defined in `./site.h'.
prefix=/softlib/tex
bindir=$(prefix)/bin
formatdir=$(prefix)/formats
mandir=$(prefix)/man/manl
texlibdir=$(prefix)
texinputdir=$(prefix)/texinputs
texfontdir=$(prefix)/fonts
texpooldir=$(prefix)/formats
mflibdir=$(prefix)
mfpooldir=$(mflibdir)/mfbases
mfinputdir=$(mflibdir)/mfinput
basedir=$(mflibdir)/mfbases

# Where the Computer Modern sources are.
# (The man pages claim that you have installed the standard utility
# sources, like `grayf.mf' and `expr.mf', somewhere in $(mfinputdir).)
cmsources=$(mfinputdir)/cm

default: all


# Stuff below here probably doesn't need to be changed.
# 
alldirs=web tex mf texware mfware fontutil bibtex dviutil
triptrapdirs=tex texware mf mfware

makeargs=SITEDIR="$(SITEDIR)" CC="$(CC)" OPT="$(OPT)" \
SHELL="$(SHELL)" LDFLAGS="$(LDFLAGS)" wlibs="$(wlibs)"

commonsources=common/extra.c common/main.c
web2csources=web2c/fixwrites.c web2c/regfix.c web2c/splitup.c \
             web2c/web2c.c web2c/web2c.lex web2c/web2c.yacc \
             web2c/web2c.h
web2cprograms=web2c/web2c web2c/regfix web2c/splitup web2c/fixwrites

triptrap: stamp-tangle
	for name in $(triptrapdirs);					\
        do								\
          (cd $${name}; make $(makeargs) triptrap);			\
        done  

run-triptrap: run-trip run-trap

clean-triptrap:
	cd tex; make veryclean
	cd mf; make veryclean

all:	stamp-web2c stamp-common
	for name in $(alldirs);						\
        do								\
          (cd $${name}; echo $${name}; make $(makeargs) all);		\
        done

TeX:	stamp-web2c stamp-tangle stamp-common
	cd tex; make $(makeargs) all

run-trip:
	cd tex; make run-trip

MF:	stamp-web2c stamp-tangle stamp-common
	cd mf; make $(makeargs) all

run-trap:
	cd mf; make run-trap


# These targets must be up to date for anything to be made.
# 
stamp-web2c: $(web2cprograms)
	touch stamp-web2c

$(web2cprograms): $(web2csources)
	cd web2c; \
          make $(makeargs) YACC="$(YACC)" LEX="$(LEX)" LEXLIB="$(LEXLIB)" all

stamp-common: $(commonsources) $(siteh)
	cd common; make $(makeargs) all
	touch stamp-common

stamp-tangle: stamp-web2c stamp-common
	cd web; make $(makeargs) tangle
	touch stamp-tangle


manpages:
	cd man; make SITEDIR=$(SITEDIR)					\
		bindir=$(bindir) texlibdir=$(texlibdir)			\
		texpooldir=$(texpooldir) formatdir=$(formatdir)		\
		texinputdir=$(texinputdir) texfontdir=$(texfontdir)	\
		mflibdir=$(mflibdir) mfpooldir=$(mfpooldir)		\
		basedir=$(basedir) mfinputdir=$(mfinputdir)		\
                cmsources=$(cmsources)					\
                all

bases: stamp-bases
stamp-bases:
	cd mf; MFPOOL=.; export MFPOOL; \
          make $(makeargs) bases="$(bases)" localmodes="$(localmodes)" bases
	touch stamp-bases

formats: stamp-formats
stamp-formats:
	cd tex; TEXPOOL=.; export TEXPOOL; \
          make $(makeargs) formats="$(formats)" fmts
	touch stamp-formats


# Installation targets.
# 
install-manpages:
	cd man; make mandir=$(mandir) INSTALL="$(fileinstall)" \
          SITEDIR="$(SITEDIR)" install

install:
	-mkdir $(bindir)
	$(proginstall) tex/initex $(bindir)/initex
	$(proginstall) tex/virtex $(bindir)/virtex
	$(proginstall) mf/inimf $(bindir)/inimf
	$(proginstall) mf/virmf $(bindir)/virmf
	$(proginstall) web/tangle $(bindir)/tangle
	$(proginstall) web/weave $(bindir)/weave
	$(proginstall) texware/dvitype $(bindir)/dvitype
	$(proginstall) texware/pltotf $(bindir)/pltotf
	$(proginstall) texware/tftopl $(bindir)/tftopl
	$(proginstall) mfware/gftodvi $(bindir)/gftodvi
	$(proginstall) mfware/gftopk $(bindir)/gftopk
	$(proginstall) mfware/gftype $(bindir)/gftype
	$(proginstall) mfware/mft $(bindir)/mft
	$(proginstall) fontutil/pktogf $(bindir)/pktogf
	$(proginstall) fontutil/pktype $(bindir)/pktype
	$(proginstall) fontutil/vftovp $(bindir)/vftovp
	$(proginstall) fontutil/vptovf $(bindir)/vptovf
	$(proginstall) bibtex/bibtex $(bindir)/bibtex
	$(proginstall) dviutil/dvicopy $(bindir)/dvicopy
	-mkdir $(texpooldir)
	if test -s tex/tex.pool; then \
	$(fileinstall) -c tex/tex.pool $(texpooldir)/tex.pool; fi
	-mkdir $(mfpooldir)
	if test -s mf/mf.pool; then \
	$(fileinstall) -c mf/mf.pool $(mfpooldir)/mf.pool; fi

install-bases: stamp-bases
	-mkdir $(basedir)
	for f in $(bases) ;						\
	do								\
	  rm -f $(bindir)/`basename $$f .base`;				\
	  ln -s $(bindir)/virmf $(bindir)/`basename $$f .base`;		\
          $(fileinstall) mf/$$f $(basedir)/$$f;				\
	done
	cd $(basedir); rm -f plain.base; ln -s mf.base plain.base


# Need GNU tar here.
# 
web2c-dist: web/tangleboot.pas
	@echo -n "check -linet; xt vs. xlib.;tangleboot;modes file;"
	@echo "big{tex,mf,bib}.ch; trie change"
	cd ..;								\
	  find src-$(version) \! -name '*.web' -type f -print > /tmp/files; \
	  tar czfT web2c$(version).tarZ /tmp/files;			\
          ls -ld web*

web-dist: web/tangleboot.pas
	cd ..;								\
          find src-$(version) -name '*.web' -print > /tmp/webfest;	\
          tar czfT web-$(version).tar.Z /tmp/webfest;			\
          ls -ld web*

web/tangleboot.pas:
	cd web; make tangle.p; cp tangle.p tangleboot.pas; make veryclean

clean:
	rm -f stamp-*
	for name in web2c common $(alldirs);				\
        do								\
          (cd $${name}; make clean);					\
        done

veryclean:
	rm -f stamp-*
	for name in web2c common man $(alldirs);			\
        do								\
          (cd $${name}; make veryclean);				\
        done
	rm -f \#*\# *~ *.bak *.ckp core

.PHONY: default triptrap run-triptrap clean-triptrap all TeX run-trip \
	MF run-trap manpages bases formats install-manpages install \
        install-formats install-bases web2c-dist web-dist clean veryclean
