# Configuration variables

Build = build
Pictures = pictures
Programs = programs

TeXSRC = ${wildcard *.tex}
PicSRC = ${notdir ${wildcard ${Pictures}/*.png}}
DemoSRC = ${notdir ${wildcard ${Programs}/*-demo.cfa}}
PgmSRC = ${notdir ${wildcard ${Programs}/*.cfa}}
BibSRC = ${wildcard *.bib}

TeXLIB = .:../../LaTeXmacros:${Build}:		# common latex macros
BibLIB = .:../../bibliography			# common citation repository

MAKEFLAGS = --no-print-directory # --silent
VPATH = ${Build} ${Pictures} ${Programs} # extra search path for file names used in document

DOCUMENT = uw-ethesis.pdf
BASE = ${basename ${DOCUMENT}}			# remove suffix

# Commands

LaTeX = TEXINPUTS=${TeXLIB} && export TEXINPUTS && pdflatex -halt-on-error -output-directory=${Build}
BibTeX = BIBINPUTS=${BibLIB} && export BIBINPUTS && bibtex
CFA = cfa

# Rules and Recipes

.PHONY : all clean				# not file names
.ONESHELL :

all : ${DOCUMENT}

clean :
	@rm -frv ${DOCUMENT} ${Build}

# File Dependencies

%.pdf : ${TeXSRC} ${DemoSRC:%.cfa=%.tex} ${PicSRC} ${PgmSRC} ${BibSRC} Makefile | ${Build}
	${LaTeX} ${BASE}
	${BibTeX} ${Build}/${BASE}
	${LaTeX} ${BASE}
	# if needed, run latex again to get citations
	if fgrep -s "LaTeX Warning: Citation" ${basename $@}.log ; then ${LaTeX} ${BASE} ; fi
#	${Glossary} ${Build}/${BASE}
#	${LaTeX} ${BASE}
	cp ${Build}/$@ $@

${Build}:
	mkdir -p $@

%-demo.tex: %-demo | ${Build}
	${Build}/$< > ${Build}/$@

%-demo: %-demo.cfa
	${CFA} $< -o ${Build}/$@

