## Define the configuration variables.

Build = build
Figures = figures
Macros = ../../LaTeXmacros
TeXLIB = .:style:text:annex:${Macros}:${Build}:../../bibliography:
LaTeX  = TEXINPUTS=${TeXLIB} && export TEXINPUTS && latex -halt-on-error -output-directory=${Build}
BibTeX = BIBINPUTS=${TeXLIB} && export BIBINPUTS && bibtex -terse

MAKEFLAGS = --no-print-directory --silent #
VPATH = ${Build} ${Figures}

## Define the text source files.

SOURCES = ${addsuffix .tex, \
thesis \
style/style \
style/cfa-format \
annex/glossary \
text/frontpgs \
text/intro \
text/basics \
text/cforall \
text/concurrency \
text/internals \
text/parallelism \
text/results \
text/together \
text/future \
}

FIGURES = ${addsuffix .tex, \
monitor \
ext_monitor \
int_monitor \
dependency \
}

PICTURES = ${addsuffix .pstex, \
system \
monitor_structs \
}

PROGRAMS = ${addsuffix .tex, \
}

GRAPHS = ${addsuffix .tex, \
}

## Define the documents that need to be made.

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

# Directives #

.PHONY : all clean					# not file names

all : ${DOCUMENT}

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

# File Dependencies #

${DOCUMENT} : ${BASE}.ps
	ps2pdf $<

${BASE}.ps : ${BASE}.dvi
	dvips ${Build}/$< -o $@

${BASE}.dvi : Makefile ${GRAPHS} ${PROGRAMS} ${PICTURES} ${FIGURES} ${SOURCES} \
		${Macros}/common.tex ${Macros}/indexstyle annex/local.bib ../../bibliography/pl.bib | ${Build}
	# Must have *.aux file containing citations for bibtex
	if [ ! -r ${basename $@}.aux ] ; then ${LaTeX} ${basename $@}.tex ; fi
	-${BibTeX} ${Build}/${basename $@}
	# Some citations reference others so run again to resolve these citations
	${LaTeX} ${basename $@}.tex
	-${BibTeX} ${Build}/${basename $@}
	# Make index from *.aux entries and input index at end of document
	makeglossaries -q -s ${Build}/${basename $@}.ist ${Build}/${basename $@}
	-version.sh
	# Run again to finish citations
	${LaTeX} ${basename $@}.tex

## Define the default recipes.

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

%.tex : %.fig ${Build}
	fig2dev -L eepic $< > ${Build}/$@

%.ps : %.fig | ${Build}
	fig2dev -L ps $< > ${Build}/$@

%.pstex : %.fig | ${Build}
	fig2dev -L pstex $< > ${Build}/$@
	fig2dev -L pstex_t -p ${Build}/$@ $< > ${Build}/$@_t

#-----------------------------------------------------------------------------------
# Tools to generate png files
# to create a png we create a pdf and convert it to png
%.png : build/%.pstex figures/%.tex ${Build}
	echo ${basename $@}
	${LaTeX} figures/${basename $@}.tex
	dvips build/${basename $@}.dvi -o build/${basename $@}.ps
	ps2pdf build/${basename $@}.ps
	convert -negate ${basename $@}.pdf $@

# creating a pdf of a figure requires generating some latex that just includes the figure
figures/%.tex: build/%.pstex ${Build}
	echo -n 	"\documentclass[preview]{standalone}\n" 	\
			"\usepackage[T1]{fontenc}\n" 			\
			"\usepackage[usenames]{color}\n" 		\
			"\usepackage{graphicx}\n" 			\
			"\usepackage{listings}\n" 			\
			"\usepackage{xspace}\n" 			\
			"\input{style}\n" 				\
			"\\\\begin{document}\n"				\
			"{\\\\resizebox{3\\\\textwidth}{!}{\input{${basename ${notdir $@}}.pstex_t}}}\n" \
			"\end{document}" > $@

# Local Variables: #
# compile-command: "make" #
# End: #
