
SHELL := bash
.ONESHELL:
.SHELLFLAGS := -eu -o pipefail -c
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules

CHAPTERS := $(shell find -maxdepth 1 -type d -name 'chapter-*')

VERSION_STRING := $(shell git describe --always --tags --long)

all: thesis.pdf

# We need three runs for biblatex's defernumbers
%.pdf: %.tex common-packages.tex common-defs.tex main.bib version.tex
	pdflatex -shell-escape $<
	biber $*
	pdflatex -shell-escape $<
	pdflatex -shell-escape $<
	echo
	echo "Undefined biblatex references:"
	grep -A2 'Package biblatex Warning: The following entry could not be found' thesis.log | sed -n '3~4{s/(biblatex) *//;p}' || echo "<None>"

#.PHONY: preview
#preview:
#	pdflatex -shell-escape -jobname thesis '\def\thesispreviewmode{}\input{thesis.tex}'
#	biber thesis
#	pdflatex -shell-escape -jobname thesis '\def\thesispreviewmode{}\input{thesis.tex}'
#	pdflatex -shell-escape -jobname thesis '\def\thesispreviewmode{}\input{thesis.tex}'
#	pdflatex -shell-escape -jobname thesis '\def\thesispreviewmode{}\input{thesis.tex}'

version.tex: thesis.tex $(addsuffix /chapter.tex,${CHAPTERS})
	echo "${VERSION_STRING}" > $@

.PHONY: update-figures
update-figures:
	@for ch in ${CHAPTERS};\
	do \
		test -d $$ch/figures &&\
		python update_figures.py $$ch/figures || true;\
	done

.PHONY: clean
clean:
	rm -f **.aux **.bbl **.bcf **.log **.blg **.lof **.lot
	rm -f **.mtc **.mtc* **.maf **.toc
	rm -f **.out **.run.xml **/texput.log 

