## Define the configuration variables.

MODULES = \
	string

Build ?= build
Macros ?= ../../LaTeXmacros
PLBib ?= ../../bibliography

MODULE_PHONIES = ${addsuffix .module,$(MODULES)}
MODULE_BUILDDIRS = ${addprefix $(Build)/,$(MODULES)}

TeXLIB = .:${Macros}:${Build}:${MODULE_BUILDDIRS}:${PLBib}:
LaTeX  = TEXINPUTS=${TeXLIB} && export TEXINPUTS && latex -halt-on-error -output-directory=${Build}
pdfLaTeX  = TEXINPUTS=${TeXLIB} && export TEXINPUTS && pdflatex -halt-on-error -output-directory=${Build}
BibTeX = BIBINPUTS=${TeXLIB} && export BIBINPUTS && bibtex

MAKEFLAGS = --no-print-directory # --silent



## Define the documents that need to be made.
all: thesis.pdf
thesis.pdf: ${MODULE_PHONIES} thesis.tex glossary.tex $(Macros)/common.tex $(Macros)/common.sty  # local.bib

DOCUMENT = thesis.pdf
BASE = ${basename ${DOCUMENT}}

# Directives #

.NOTPARALLEL:						# cannot make in parallel

.PHONY : all clean %.module			# not file names

all : ${DOCUMENT}

clean :
	@rm -frv ${DOCUMENT} ${BASE}.ps ${Build}

# File Dependencies #

# %.pdf : build/%.ps | ${Build}
# 	ps2pdf $<

# build/%.ps : build/%.dvi | ${Build}
# 	dvips $< -o $@

# build/%.dvi : %.tex Makefile | ${Build}
# 	# Conditionally create an empty *.ind (index) file for inclusion until makeindex is run.
# 	if [ ! -r ${basename $@}.ind ] ; then touch ${basename $@}.ind ; fi
# 	# Must have *.aux file containing citations for bibtex
# 	if [ ! -r ${basename $@}.aux ] ; then ${LaTeX} $< ; fi
# 	-${BibTeX} ${basename $@}
# 	# Some citations reference others so run again to resolve these citations
# 	${LaTeX} $<
# 	-${BibTeX} ${basename $@}
# 	# Make index from *.aux entries and input index at end of document
# 	-makeglossaries -q -s ${basename $@}.ist ${basename $@}
# 	# Make index from *.aux entries and input index at end of document
# 	-makeindex ${basename $@}.idx
# 	# Run again to finish citations
# 	${LaTeX} $<

%.pdf : %.tex | ${Build}
	${pdfLaTeX} $<
	cp ${Build}/$@ $@

${Build}:
	mkdir -p ${Build}

%.module: content/%
	$(MAKE) -C $< BuildBase=../../$(Build)
