# Makefile for ptex
version = @WEB2CVERSION@
configopt = @configopt@
configkcode = @configkcode@

default: programs formats

programs=ptex tftopl pltotf pdvitype jbibtex
euc-formats=ptex-euc.fmt platex-euc.fmt
sjis-formats=ptex-sjis.fmt platex-sjis.fmt

ac_include ../../make/paths.mk

# Used for triptrap.
DIFF = diff
DIFFFLAGS =

.SUFFIXES: # get rid of .p.o
ac_include ../../make/common.mk
ac_include ../../make/programs.mk

@MAINT@ifeq ($(CC), gcc)
@MAINT@XDEFS = -Wimplicit -Wreturn-type -Wcomment -Wformat
@MAINT@endif

# With --enable-ipc, TeX may need to link with -lsocket.
socketlibs = @socketlibs@

common_makeargs = $(MFLAGS) CC='$(CC)' CFLAGS='$(CFLAGS)' LDFLAGS='$(LDFLAGS)' $(XMAKEARGS)
install_makeargs = INSTALL='$(INSTALL)' INSTALL_DATA='$(INSTALL_DATA)' \
  INSTALL_PROGRAM='$(INSTALL_PROGRAM)' \
  $(makevars) $(common_makeargs)

proglib = ../lib/lib.a

# The sh script that does the conversion:
web2c = srcdir=$(thisdir) $(SHELL) $(thisdir)/pconvert
# Additional dependencies:
web2c_aux = $(thisdir)/pconvert $(cwebdir)/common.defines \
	$(thisdir)/kanji.defines
web2c_programs = $(cwebdir)/fixwrites $(cwebdir)/splitup $(cwebdir)/web2c

# Unfortunately, suffix rules can't have dependencies, or multiple
# targets, and we can't assume all makes supports pattern rules.
.SUFFIXES: .p .c .ch .p
.p.c: # really depends on $(web2c_programs), and generates .h.
	 $(web2c) $*
.ch.p: # really depends on $(srcdir)/tangle; for ptex, also generates .pool
	$(srcdir)/tangle $(thisdir)/$*.web $<

# These definitions have to come before the rules that expand them.
# The tex{ini,0,1,2}.c files are created by splitup, run as part of convert.
ptex_c = texini.c tex0.c tex1.c tex2.c
ptex_o = texini.o tex0.o tex1.o tex2.o ptexextra.o
plib_o = kanji.o version.o usage.o

# Prevent Make from deleting the intermediate forms.
.PRECIOUS: %.p %.c

#  
# Rules for pTeX, TFtoPL, PLtoTF.
# fix dir
thisdir = .
srcdir = ..
top_srcdir= $(srcdir)/..
cwebdir = $(srcdir)/web2c

kpathsea_parent = $(top_srcdir)
kpathsea_srcdir_parent = $(kpathsea_parent)

programs: $(programs)

### pTeX
ptex: $(ptex_o) $(plib_o) $(kpathsea) $(proglib)
	$(kpathsea_link) $(ptex_o) $(plib_o) $(socketlibs) $(LOADLIBES)

$(ptex_o): $(ptex_c) texcoerce.h texd.h kanji.h

texini.o: texini.c texcoerce.h texd.h kanji.h ptexhelp.h
tex0.o: tex0.c texcoerce.h texd.h kanji.h ptexhelp.h
tex1.o: tex1.c texcoerce.h texd.h kanji.h ptexhelp.h
tex2.o: tex2.c texcoerce.h texd.h kanji.h ptexhelp.h
ptexextra.o: ptexextra.c ptexextra.h texcoerce.h texd.h kanji.h ptexhelp.h

$(ptex_c) texcoerce.h texd.h: ptex.p $(web2c_aux) $(web2c_programs)
	$(web2c) ptex

ptex.p: ptex.web ptex.ch $(srcdir)/tangle

ptex.web: $(srcdir)/tex.web $(srcdir)/tex.ch $(srcdir)/tie
	$(srcdir)/tie -m ptex.web $(srcdir)/tex.web $(srcdir)/tex.ch

ptex.ch: $(srcdir)/tie ptex.web ptex-base.ch
	$(srcdir)/tie -c ptex.ch ptex.web ptex-base.ch

### TFtoPL
tftopl: tftopl.o $(plib_o) $(kpathsea) $(proglib)
	$(kpathsea_link) tftopl.o $(plib_o) $(LOADLIBES)
tftopl.o: tftopl.c kanji.h ptexhelp.h
tftopl.c: tftopl.p $(web2c_aux)
tftopl.p: tftopl.web tftopl.ch
tftopl.web: $(srcdir)/tftopl.web $(srcdir)/tftopl.ch
	rm -f tftopl.web && \
	$(srcdir)/tie -m tftopl.web $(srcdir)/tftopl.web $(srcdir)/tftopl.ch


### PLtoTF
pltotf: pltotf.o $(plib_o) $(kpathsea) $(proglib)
	$(kpathsea_link) pltotf.o $(plib_o) $(LOADLIBES)
pltotf.o: pltotf.c kanji.h ptexhelp.h
	$(compile) -DKANJI_C=1 -c pltotf.c
pltotf.c: pltotf.p $(web2c_aux)
pltotf.p: pltotf.web pltotf.ch
pltotf.web: $(srcdir)/pltotf.web $(srcdir)/pltotf.ch
	rm -f pltotf.web && \
	$(srcdir)/tie -m pltotf.web $(srcdir)/pltotf.web $(srcdir)/pltotf.ch

### pDVItype
pdvitype: pdvitype.o $(plib_o) $(kpathsea) $(proglib)
	$(kpathsea_link) pdvitype.o $(plib_o) $(LOADLIBES)
pdvitype.o: pdvitype.c kanji.h ptexhelp.h
	$(compile) -DDVITYPE -DHEX_CHAR_CODE -c pdvitype.c
pdvitype.c: pdvitype.p $(web2c_aux)
pdvitype.p: pdvitype.web pdvitype.ch
pdvitype.web:
	$(LN) $(srcdir)/dvitype.web pdvitype.web

### jBibTeX
jbibtex: jbibtex.o jbibextra.o $(plib_o) $(kpathsea) $(proglib)
	$(kpathsea_link) jbibtex.o jbibextra.o $(plib_o) $(LOADLIBES)
jbibextra.o: jbibextra.c jbibd.h kanji.h ptexhelp.h
jbibtex.o: jbibtex.c jbibextra.h kanji.h ptexhelp.h
	$(compile) -DBIBTEX -c jbibtex.c
jbibtex.c jbibd.h: jbibtex.p $(web2c_aux) jbibd.sed
jbibtex.p: jbibtex.web jbibtex.ch
jbibtex.web:
	$(LN) $(srcdir)/bibtex.web jbibtex.web

# Additional dependencies for recompiling the C code are generated
# automatically, included at the end.
kanji.o: kanji.c kanji.h ptexhelp.h
version.o: version.c kanji.h ptexhelp.h
usage.o: usage.c

# Additional dependencies for relinking.
$(srcdir)/tangleboot:
	cd $(srcdir) && $(MAKE) tangleboot
$(srcdir)/tangle:
	cd $(srcdir) && $(MAKE) tangle
$(srcdir)/tie:
	cd $(srcdir) && $(MAKE) tie
$(cwebdir)/fixwrites:
	cd $(cwebdir) && $(MAKE) fixwrites
$(cwebdir)/splitup:
	cd $(cwebdir) && $(MAKE) splitup
$(cwebdir)/web2c:
	cd $(cwebdir) && $(MAKE) web2c
$(proglib):
	cd $(srcdir)/lib && $(MAKE) lib.a
$(kpathsea):
	cd $(kpathsea_dir) && $(MAKE) libkpathsea.a

# 
# Making formats and bases.
formats=$(euc-formats) $(sjis-formats)
formats: $(formats)

texmf.cnf: $(kpathsea_dir)/texmf.cnf
	$(SHELL) $(thisdir)/mkconf $(kpathsea_dir)/texmf.cnf > texmf.cnf

dumpenv = TEXMFCNF=$(thisdir)

ptex-euc.fmt: ptex texmf.cnf
	$(dumpenv) $(MAKE) progname=ptex files="ptex.tex min10.tfm" prereq-check
	$(dumpenv) ./ptex --ini --kanji=euc --progname=ptex --jobname=ptex-euc \\input ptex.tex \\dump </dev/null

ptex-sjis.fmt: ptex texmf.cnf
	$(dumpenv) $(MAKE) progname=ptex files="ptex.tex min10.tfm" prereq-check
	$(dumpenv) ./ptex --ini --kanji=sjis --progname=ptex --jobname=ptex-sjis \\input ptex.tex \\dump </dev/null

platex-euc.fmt: ptex texmf.cnf
	$(dumpenv) $(MAKE) progname=platex files="platex.ltx" prereq-check
	$(dumpenv) ./ptex --ini --kanji=euc --progname=platex --jobname=platex-euc \\input platex.ltx </dev/null

platex-sjis.fmt: ptex texmf.cnf
	$(dumpenv) $(MAKE) progname=platex files="platex.ltx" prereq-check
	$(dumpenv) ./ptex --ini --kanji=sjis --progname=platex --jobname=platex-sjis \\input platex.ltx </dev/null

# This is meant to be called recursively, with $(files) set.
prereq-check: $(kpathsea_dir)/kpsewhich
	$(dumpenv) $(kpathsea_dir)/kpsewhich --progname=$(progname) $(files) >/dev/null || $(MAKE) prereq-lose >/dev/null

prereq-lose:
	@echo "You seem to be missing input files necessary to make the" >&2
	@echo "basic formats (some or all of: $(files))." >&2
	@echo "Perhaps you've defined the default paths incorrectly, or" >&2
	@echo "perhaps you have environment variables set pointing" >&2
	@echo "to an incorrect location.  See $(srcdir)/kpathsea/BUGS." >&2
	@echo >&2
	@echo "If you simply do not have the files, you can" >&2
	@echo "retrieve a minimal set of input files from" >&2
	@echo "ftp://ftp.tug.org/tex/lib.tar.gz, mirrored on" >&2
	@echo "CTAN hosts in systems/web2c." >&2
	false

$(kpathsea_dir)/kpsewhich: $(kpathsea)

# 
install: install-exec install-data
	$(top_srcdir)/kpathsea/mktexlsr
install-exec:: install-links
install-data:: install-formats

# Installation directories.
$(bindir)::
	$(SHELL) $(top_srcdir)/mkinstalldirs $(bindir)
$(web2cdir)::
	$(SHELL) $(top_srcdir)/mkinstalldirs $(web2cdir)

# The actual binary executables and pool files.
install-programs: $(programs) $(bindir)
	for p in $(programs); do $(INSTALL_LIBTOOL_PROG) $$p $(bindir); done

# The links to ptex for each format.
install-links: install-programs $(bindir)
	cd $(bindir) && (rm -f iniptex virptex platex; $(LN) ptex platex)

# Always do plain.*, so examples from the TeXbook (etc.) will work.
install-formats: $(formats) $(web2cdir)
	for f in $(formats); do $(INSTALL_DATA) $$f $(web2cdir)/$$f; done
	cd $(web2cdir) && (rm -f ptex-jis.fmt platex-jis.fmt ;\
		$(LN) ptex-euc.fmt ptex-jis.fmt ;\
		$(LN) platex-euc.fmt platex-jis.fmt)
	cd $(web2cdir) && (rm -f ptex.fmt platex.fmt ;\
		$(LN) ptex-$(configkcode).fmt ptex.fmt ;\
		$(LN) platex-$(configkcode).fmt platex.fmt )

# Auxiliary files.
install-data::
	$(SHELL) $(top_srcdir)/mkinstalldirs $(web2cdir)
	$(INSTALL_DATA) ptex.pool $(web2cdir)/ptex.pool
	if [ -f $(web2cdir)/texmf.cnf ]; then \
		mv -f $(web2cdir)/texmf.cnf $(web2cdir)/texmf.cnf.orig ;\
	else true; fi
	$(INSTALL_DATA) texmf.cnf $(web2cdir)/texmf.cnf
# 
# Testing, including triptrap. The `x' filenames are for output.
check: pltotf-check tftopl-check pooltype-check ptex-check 

triptrap: trip
tripdir = $(srcdir)/triptrap
ptripdir = ./triptrap
testdir = $(srcdir)/tests
testenv = TEXMFCNF=$(ptripdir)
dvitype_args = -output-level=2 -dpi=72.27 -page-start='*.*.*.*.*.*.*.*.*.*'
trip: ptex pltotf tftopl $(srcdir)/dvitype
	@echo ">>> See $(tripdir)/trip.diffs for example of acceptable diffs." >&2
	./pltotf $(tripdir)/trip.pl trip.tfm
	./tftopl ./trip.tfm trip.pl
	-diff $(tripdir)/trip.pl trip.pl
	rm -f trip.tex; $(LN) $(tripdir)/trip.tex . # get same filename in log
	-$(SHELL) -c '$(testenv) ./ptex -ini <$(tripdir)/trip1.in >tripin.fot'
	mv trip.log tripin.log
	-diff $(tripdir)/tripin.log tripin.log
# May as well test non-ini second time through.
	-$(SHELL) -c '$(testenv) ./ptex <$(tripdir)/trip2.in >trip.fot'
	-diff $(tripdir)/trip.fot trip.fot
# We use $(DIFF) instead of `diff' only for those files where there
# might actually be legitimate numerical differences.
	-$(DIFF) $(DIFFFLAGS) $(tripdir)/trip.log trip.log
	$(SHELL) -c '$(testenv) $(srcdir)/dvitype $(dvitype_args) trip.dvi >trip.typ'
	-$(DIFF) $(DIFFFLAGS) $(tripdir)/trip.typ trip.typ

# Ad hoc tests.
pltotf-check: pltotf
	./pltotf -verbose $(testdir)/cmr10 $(testdir)/xcmr10
	./pltotf -verbose $(testdir)/min10 $(testdir)/xmin10

tftopl-check: tftopl
	./tftopl -verbose $(testdir)/cmr10 $(testdir)/xcmr10
	./tftopl -verbose $(testdir)/min10 $(testdir)/xmin10

pooltype-check: $(srcdir)/pooltype
	$(srcdir)/pooltype ptex.pool > $(testdir)/xtexpool.typ

ptex-check: trip ptex-euc.fmt
# Test truncation (but don't bother showing the warning msg).
	./ptex -output-comment="`cat PROJECTS`" $(testdir)/hello 2>/dev/null \
	  && $(srcdir)/dvitype hello.dvi | grep kb@mail.tug.org >/dev/null
# \openout should show up in \write's.
	./ptex $(testdir)/openout && grep xfoo openout.log
# one.two.tex -> one.two.log
	./ptex $(testdir)/one.two && ls -l one.two.log
# uno.dos -> uno.dos.log
	./ptex $(testdir)/uno.dos && ls -l uno.dos.log
	./ptex $(testdir)/just.texi && ls -l just.log
	-./ptex $(testdir)/batch.tex
	./ptex -shell $(testdir)/write18 | grep echo
# tcx files are a bad idea.
#	./ptex -translate-file=$(srcdir)/share/isol1-t1.tcx $(testdir)/eight \
#	  && ./pdvitype eight.dvi >eigh.typ
	./ptex -mltex -ini $(testdir)/mltextst
	-./ptex </dev/null
	-PATH=$(kpathsea_dir):$$PATH ./ptex '\nonstopmode\font\foo=nonesuch\end'

installcheck:
	cd $(testdir) && ptex '\nonstopmode \tracingstats=1 \input story \bye'

# 
# Cleaning.
mostryclean::
	rm -f *.o

clean:: mostryclean
	rm -f texcoerce.h texd.h jbibd.h jbibtex.h
	rm -f $(ptex_c) $(ptex_o) $(plib_o) $(programs)
	rm -f tftopl.h tftopl.c tftopl.o
	rm -f pltotf.h pltotf.c pltotf.o
	rm -f pdvitype.h pdvitype.c pdvitype.o
	rm -f jbibtex.c jbibtex.o jbibextra.o
distclean:: clean
	rm -fr failure
	rm -f ptex.web ptex.ch ptex.p ptex.pool ptex.log ptex-euc.fmt ptex-sjis.fmt
	rm -f platex.log platex-euc.fmt platex-sjis.fmt texsys.aux platex
	rm -f tftopl.web tftopl.p
	rm -f pltotf.web pltotf.p
	rm -f pdvitype.web pdvitype.p
	rm -f jbibtex.web jbibtex.p
	rm -f Makefile kanji.h texmf.cnf config.sed
extraclean:: distclean
	rm -f *.aux *.bak *.bbl *.blg *.dvi *.log *.pl *.tfm *.vf *.vpl
	rm -f *.*pk *.*gf *.mpx *.i *.s *~ *.orig *.rej *\#*
	rm -f CONTENTS.tex a.out core mfput.* texput.* mpout.*
	rm -f trip.tex trap.mf mtrap.mp trap.mp trap.mpx
	rm -f trip.* tripin.* tripos.tex 8terminal.tex
	rm -f trap.* mftrap.* mftrapin.* mptrap.* mptrapin.*
	rm -f trapf.* mtrap.* writeo* missfont.log

# Makefile.
Makefile: configure $(srcdir)/config.status $(thisdir)/Makefile.in
	$(SHELL) configure $(configopt)

kanji.h: kanji.h.in
	$(SHELL) configure $(configopt)
