.PHONY: backup
.PHONY: clean
.PHONY: comparison
.PHONY: distclean
.PHONY: suite
.PHONY: text
.PHONY: tidy
.PHONY: true

help:
	@echo "Targets: help all check backup clean tidy suite"
	@echo
	@echo "    All: this target will build new .tex and .pdf from XML/XSL files."
	@echo "  Check: the same as 'all', but prompts when LaTeX errors occur."
	@echo " Backup: puts current .pdf and .tex files aside so that they"
	@echo "         can later be compared to newly-generated files."
	@echo "  Clean: remove temporary files generated by latex/pdflatex."
	@echo "   Tidy: use xmllint."
	@echo "  Suite: descends into all test_* directories and builds"
	@echo "         the SUITE_TARGET target ('all' by default)."
	@echo "         Example: gmake SUITE_TARGET=check suite"
	@echo
	@echo "  Environment variables: TEST_NAME DB2LATEX_PDFLATEX_FLAGS FORCE_TARGET"
	@echo "                         SUITE_TARGET"
	@echo "              TEST_NAME: e.g. 'test' or 'minimal', corresponding to"
	@echo "                         the XML/XSL filenames."
	@echo "DB2LATEX_PDFLATEX_FLAGS: flags for pdflatex."
	@echo "           FORCE_TARGET: set to 'true' to force the generation of"
	@echo "                         .tex and .pdf files even when the XML/XSL"
	@echo "                         files haven't changed."
	@echo "           SUITE_TARGET: the sub-directory target for 'suite'."

TEST_NAME?=test
SAMPLE_PREFIX?=test_
SUITE_TARGET?=all
DB2LATEX_PDFLATEX_FLAGS?=-interaction=nonstopmode
DB2LATEX_LATEX_FLAGS?=$(DB2LATEX_PDFLATEX_FLAGS)
EXTRA_CLEAN?=
EXTRA_DISTCLEAN?=
TEST_EXTENSION?=pdf
SAMPLE_SOURCE_NESTING?=
SAMPLE_HTDOCS_NESTING?=
VIEWER?=xpdf

check : override DB2LATEX_PDFLATEX_FLAGS=
check : override DB2LATEX_LATEX_FLAGS=
suite : FORCE_TARGET?=true

# Tests expected to be expressed using multiple .xsl files, not multiple .xml files.
pdfs ?= $(patsubst %.xsl,%.$(TEST_EXTENSION),$(wildcard *.xsl))

# Local XSLT
-include ../Makefile.local

# Default XSLT
XSLT ?= xsltproc $4 -o $1 $2 $3
PARAM ?= --stringparam $1 $2
MAKEINDEX ?= makeindex $1

.SECONDARY: $(patsubst %.$(TEST_EXTENSION),%.tex,$(pdfs))

# Default target
all: $(pdfs) clean
	@[ -z "$(pdfs)" ] && echo No results to display. || ( for p in $(pdfs); do \
		echo $(VIEWER) $$p; $(VIEWER) $$p; done )

#txts ?= $(patsubst %.pdf.bak,%.pdf.bak.txt,$(wildcard *.pdf.bak)) \
#	$(patsubst %.pdf.bak,%.pdf.txt,$(wildcard *.pdf.bak))

pdf: $(pdfs)

text:
	for i in *.pdf *.pdf.bak; do \
		( set -x; pdftotext $$i $$i.txt ); \
	done

comparison:
	for i in *.pdf.bak.txt; do \
		vimdiff $$i $${i%.pdf.bak.txt}.pdf.txt; \
		echo "Continue?"; \
		read; \
	done

# Stop for LaTeX errors
check: $(pdfs) clean
	@[ -z "$(pdfs)" ] && echo No results to display. || ( for p in $(pdfs); do \
		echo $(VIEWER) $$p; $(VIEWER) $$p; done )

# Save a copy of the existing output so that we can later do comparison
backup: $(addsuffix .bak,$(pdfs)) $(patsubst %.$(TEST_EXTENSION),%.tex.bak,$(pdfs))

.SILENT: %.bak

%.bak: true
	-test -f $* && mv $* $*.bak

%.pdf: %.tex
	-pdflatex $(DB2LATEX_PDFLATEX_FLAGS) $< && \
	pdflatex $(DB2LATEX_PDFLATEX_FLAGS) $< && \
	( test -s $*.idx && $(call MAKEINDEX, $*.idx) || true ) && \
	pdflatex $(DB2LATEX_PDFLATEX_FLAGS) $<

%.dvi: %.tex
	latex $(DB2LATEX_LATEX_FLAGS) $<
	latex $(DB2LATEX_LATEX_FLAGS) $<
	test -s $*.idx && $(call MAKEINDEX, $*.idx) || true
	latex $(DB2LATEX_LATEX_FLAGS) $<

%.ps: %.dvi
	dvips -Pwww -f $< -o $@

%.tex: %.xsl $(TEST_NAME).xsl $(TEST_NAME).xml $(FORCE_TARGET)
	$(call XSLT, $@, $<, $(TEST_NAME).xml ) 2>&1 | tee $*.msg

clean:
	rm -rf *.log *.out *.brf *.aux *.glo *.idx *.toc *~ *.ind *.ilg *.lot *.lof *.dvi *.blg $(EXTRA_CLEAN)

distclean: clean
	rm -rf *.pdf.txt *.bak *.pdf.bak.txt $(EXTRA_DISTCLEAN)

tidy: $(patsubst %.xml,%.new.xml,$(wildcard *.xml))

suite:
	for dir in $(wildcard $(SAMPLE_PREFIX)*); do \
		if test -s $$dir/Makefile; then \
			${MAKE} \
				FORCE_TARGET=$(FORCE_TARGET) \
				DB2LATEX_PDFLATEX_FLAGS=$(DB2LATEX_PDFLATEX_FLAGS) \
				-C $$dir $(SUITE_TARGET); \
		else \
			echo Skipping $$dir >&2; \
		fi \
	done

%.new.xml: %.xml $(FORCE_TARGET)
	xmllint --format $< > $@

%.check: %.xml $(FORCE_TARGET)
	xmllint --valid --noout $<

website:
	@echo Clearing and re-creating sample directory...
	rm -rf sample
	mkdir sample
	@echo Creating symlinks...
	for dir in $(SAMPLE_PREFIX)*; do \
		COUNT=`ls -1 $$dir/*.pdf 2>/dev/null | wc -l`; \
		if [ $$COUNT -eq 1 ]; then \
			for file in $$dir/*.pdf; do \
				( set -x; ln -s ../$$file "sample/$$dir.pdf" ); \
			done; \
		elif [ $$COUNT -gt 1 ]; then \
			for file in $$dir/*.pdf; do \
				DIR=`dirname $$file`; \
				TEST=`basename $$file .pdf`; \
				case "$(SAMPLE_PREFIX)$$TEST" in \
				$$DIR|$(SAMPLE_PREFIX)) \
					( set -x; ln -s "../$$file" "sample/$$DIR.pdf" ); \
				;; \
				*) \
					( set -x; ln -s "../$$file" "sample/$${DIR}_$$TEST.pdf" ); \
				;; \
				esac; \
			done; \
		fi; \
	done
	@echo Setting permissions...
	cd sample && chmod a+rx,g+ws . && for file in *; do \
		chmod a+r,g+w `readlink $$file`; \
	done
	@echo Commencing scp transfer...should use rsync instead'?'...
	scp -pr sample$(SAMPLE_SOURCE_NESTING) db2latex.sourceforge.net:/home/groups/d/db/db2latex/htdocs/$(SAMPLE_HTDOCS_NESTING)

