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

VERSION_STRING := $(shell git describe --always --tags --long)
# chapter subdir cwd, thesis repo root, system tex distribution
TEXINPUTS := .:..:
export TEXINPUTS

all: clean chapter.pdf

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

.PHONY: preview
preview:
	rm chapter.aux # make sure biblatex defernumbers works properly
	pdflatex -shell-escape -jobname chapter '\def\thesispreviewmode{}\input{chapter-template.tex}'
	biber --input-directory=.. chapter || true
	pdflatex -shell-escape -jobname chapter '\def\thesispreviewmode{}\input{chapter-template.tex}'
	pdflatex -shell-escape -jobname chapter '\def\thesispreviewmode{}\input{chapter-template.tex}'

version.tex: chapter.tex
	echo "${VERSION_STRING}" > $@

.PHONY: update-figures
update-figures:
	@python ../update_figures.py figures

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

