DVIPS = dvips
DVIPSOPTS = -D 600 -t a4
GPIC = gpic
HACHA = hacha
HACHAOPTS = -tocbis
HEVEA = hevea
HEVEAOPTS = -fix -exec xxdate.exe -O
IMAGEN = imagen
LATEX = latex
LATEXOPTS = -file-line-error -interaction nonstopmode -halt-on-error
MAKEINDEX = makeindex
MAKEINDEXOPTS =
PDFLATEX = pdflatex
PDFLATEXOPTS = $(LATEXOPTS) # -synctex=15

HTMLDIR = $(HOME)/public_html/hevea
DISTRIDIR = $(HTMLDIR)/distri
DOCDIR = $(HTMLDIR)/doc


.PHONY: all
all: doc doc/index.html thai

.PHONY: dvi
dvi: manual.dvi

.PHONY: html
html: doc/index.html

.PHONY: info
info: manual.info

.PHONY: pdf
pdf: manual.pdf

.PHONY: ps
ps: manual.ps

.PHONY: text
text: manual.txt


doc:
	mkdir -p $@
	cp fddl.html $@

.PHONY: docclean
docclean::
	$(RM) ./doc/manual.h{tml,aux,ind,toc} ./doc/manual.image.tex

opt:
	HEVEADIR=.. $(MAKE) $(MFLAGS) HEVEAOPTS="$(HEVEAOPTS)" HACHAOPTS="$(HACHAOPTS)" HEVEA=../hevea.opt HACHA=../hacha.opt IMAGEN=../imagen all

infoopt:
	HEVEADIR=.. $(MAKE) $(MFLAGS) HEVEAOPTS="$(HEVEAOPTS)" HACHAOPTS="$(HACHAOPTS)" HEVEA=../hevea.opt HACHA=../hacha.opt IMAGEN=imagen.opt info

byte:
	HEVEADIR=.. $(MAKE) $(MFLAGS) HEVEAOPTS="$(HEVEAOPTS)" HACHAOPTS="$(HACHAOPTS)" HEVEA=../hevea.byte HACHA=../hacha.byte IMAGEN=../imagen all

tmp.tex: text.tex
	$(GPIC) -t < $< > $@

MANUAL_DEPENDENCIES = manual.tex macros.tex tmp.tex version.tex # "manual.tex" MUST be first!

doc/manual.html: doc manual.hva $(MANUAL_DEPENDENCIES)
	$(HEVEA) $(HEVEAOPTS) -o $@ manual.hva manual.tex

doc/index.html: doc/manual.html
	$(HACHA) $(HACHAOPTS) -o $@ $<

LATEX_LABEL_WARNING = 'LaTeX Warning: Label(s) may have changed'

manual.dvi: $(MANUAL_DEPENDENCIES)
	$(LATEX) $(LATEXOPTS) $<
	$(LATEX) $(LATEXOPTS) $< > /dev/null 2>&1
	@ITER=2; MAXITER=10;  \
        while test -e $*.log && grep -q $(LATEX_LABEL_WARNING) $*.log; do  \
            test $$ITER -le $$MAXITER || { printf 'Excessive number of iterations (%d) to reach fixpoint.  Aborted!\n' "$$ITER" 1>&2; exit 1; };  \
            printf 'Run LaTeX again.\n' ; \
            $(LATEX) $(LATEXOPTS) $< > /dev/null 2>&1;  \
            : $$((ITER += 1));  \
	done;  \
        printf 'Fixpoint reached after %d iteration(s).\n' "$$ITER"

manual.ps: manual.dvi
	$(DVIPS) $(DVIPSOPTS) -o $@ $<

manual.pdf: $(MANUAL_DEPENDENCIES)
	$(PDFLATEX) $(PDFLATEXOPTS) $<
	$(PDFLATEX) $(PDFLATEXOPTS) $< > /dev/null 2>&1
	@ITER=2; MAXITER=10;  \
        while test -e $*.log && grep -q $(LATEX_LABEL_WARNING) $*.log; do  \
            test $$ITER -le $$MAXITER || { printf 'Excessive number of iterations (%d) to reach fixpoint.  Aborted!\n' "$$MAXITER" 1>&2; exit 1; };  \
            printf 'Run PDFLaTeX again.\n' ; \
            $(PDFLATEX) $(PDFLATEXOPTS) $< > /dev/null 2>&1;  \
            : $$((ITER += 1));  \
	done;  \
        printf 'Fixpoint reached after %d iteration(s).\n' "$$ITER"

manual.info: $(MANUAL_DEPENDENCIES)
	$(HEVEA) $(HEVEAOPTS) -info -o $@ $<

manual.txt: $(MANUAL_DEPENDENCIES)
	$(HEVEA) $(HEVEAOPTS) -text -o $@ $<

.PHONY: thai
thai: doc/thaihevea.html

doc/thaihevea.html: doc thai/thaihevea.ttex
	TEXINPUTS="thai:" $(HEVEA) $(HEVEAOPTS) -o $@ thai/thaihevea.ttex

docclean::
	$(RM) doc/thaihevea.h{aux,ind,toc} doc/thaihevea.image.tex

############# Release stuff
VERSIONFILE=../version.ml

version.tex: $(VERSIONFILE)
	printf '%%%%%%  Automatically generated by doc/Makefile.\n' > $@
	sed -n -e 's/^let real_version = "\(.*\)".*$$/\\def\\heveaversion{\1}/p' < $(VERSIONFILE) >> $@
	sed -n -e 's/^let release_date = "\([0-9\-]*\)".*$$/\\def\\releasedate{\1}/p' < $(VERSIONFILE) >> $@
	printf '\\newif\\ifdevrelease\\devreleasefalse\n' >> $@
	sed -n -e '/let real_version = ".*+.*"/s//\\devreleasetrue/p' < $(VERSIONFILE) >> $@


.PHONY: clean
clean:
	$(RM) ./*.aux ./*.idx ./*.ilg ./*.ind ./*.log ./*.synctex ./*.toc
	$(RM) ./*.haux ./*.hidx ./*.hind ./*.hrf ./*.htoc

.PHONY: cleanall
cleanall: clean
	$(RM) manual.{dvi,pdf,ps,txt} manual.info manual.info-[0-9]
	$(RM) faq.html hevea.spec pdfmanual.pdf tmp.tex version.tex
	$(RM) -r doc
