# partly autogenerated, that's why it's so ugly!

# say 'make V=1' to see the full build output
ifndef V
Q = @
Q_LATEX = sh -c 'echo -e "   " `echo $$(basename "$$0") | tr a-z A-Z`"\t$${!\#/..\/..\//}"; ! "$$0" "$$@" | grep -A5 -E "^[^ ]+:[0-9]+:"'
Q_OTHER = sh -c 'echo -e "   " `echo $$(basename "$$0") | tr a-z A-Z`"\t$${!\#/..\/..\//}"; "$$0" "$$@" >/dev/null'
endif

LATEX = latex
PDFLATEX = pdflatex
BIBTEX = bibtex
DVIPS = dvips
DVIPDF = dvipdf
PDFTOEPS = pdftops -eps
PDFTOPS = pdf2ps
EPSTOPDF = epstopdf
FIGTODEV = fig2dev
MPOST = mpost

.PHONY: all clean archive

all:  thesis

texfiles = $(wildcard *.tex)

.PHONY: thesis
thesis: thesis.pdf thesis.ps

thesis.pdf: $(texfiles) thesis.bbl
	$(Q) rm -rf build/pdf/; mkdir -p build/pdf/
	$(Q) cd build/pdf/ && TEXINPUTS="../../:$$TEXINPUTS" $(Q_LATEX) $(PDFLATEX) -interaction=nonstopmode -file-line-error ../../thesis.tex
	$(Q) cd build/pdf/ && TEXINPUTS="../../:$$TEXINPUTS" $(Q_LATEX) $(PDFLATEX) -interaction=nonstopmode -file-line-error ../../thesis.tex
	$(Q) ln -f build/pdf/thesis.pdf thesis.pdf

thesis.dvi: $(texfiles) thesis.bbl
	$(Q) rm -rf build/dvi/; mkdir -p build/dvi/
	$(Q) cd build/dvi/ && TEXINPUTS="../../:$$TEXINPUTS" $(Q_LATEX) $(LATEX) -interaction=nonstopmode -file-line-error ../../thesis.tex
	$(Q) cd build/dvi/ && TEXINPUTS="../../:$$TEXINPUTS" $(Q_LATEX) $(LATEX) -interaction=nonstopmode -file-line-error ../../thesis.tex
	$(Q) ln -f build/dvi/thesis.dvi thesis.dvi

thesis.ps: thesis.dvi
	$(Q)$(Q_OTHER) $(DVIPS) -q thesis

thesis.aux: $(texfiles)
	$(Q) $(Q_LATEX) $(LATEX) -interaction=nonstopmode -file-line-error thesis.tex

thesis.bbl: thesis.aux
	$(Q)$(Q_OTHER) $(BIBTEX) thesis

clean:
	rm -rf build/
	rm -f *.aux
	rm -f thesis.dvi thesis.idx thesis.ind thesis.ilg thesis.out thesis.toc thesis.pdf thesis.log thesis.ps *.blg *.bbl *.thm

archive: thesis-template.zip

thesis-template.zip: *.tex Makefile USIlogo.* refs.bib
	mkdir -p thesis
	cp `git ls-files "*.tex" Makefile "USIlogo.*" refs.bib` thesis/
	perl -i -pe 's/\$$VERSION\$$/'"$$(git describe)/" thesis/*.tex
	rm -f thesis-template.zip
	zip -r thesis-template.zip thesis/
