
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: clean 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 $<
	biber $*
	pdflatex -shell-escape $<

.PHONY: preview
preview:
	biber --input-directory=.. chapter || true
	pdflatex -shell-escape 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
	rm -f **.mtc **.mtc* **.maf **.toc
	rm -f **.out **.run.xml **/texput.log 

