PDFFILES=fi-pdflatex.pdf fi-lualatex.pdf sci-pdflatex.pdf sci-lualatex.pdf \
	ped-pdflatex.pdf ped-lualatex.pdf med-pdflatex.pdf med-lualatex.pdf \
	fss-pdflatex.pdf fss-lualatex.pdf fsps-pdflatex.pdf fsps-lualatex.pdf \
	phil-pdflatex.pdf phil-lualatex.pdf law-pdflatex.pdf law-lualatex.pdf \
	econ-pdflatex.pdf econ-lualatex.pdf
PDFLATEX=pdflatex --shell-escape
LUALATEX=lualatex
AUXFILES=*.aux *.log *.out *.toc *.lot *.lof *.bcf *.blg *.run.xml \
		*.bbl *.idx *.ind *.ilg *.markdown.*
AUXDIRS=_markdown-*/ _markdown_*/ _minted-*/
OUTPUT=$(PDFFILES) $(PDFFILES:.pdf=.tex)

.PHONY: all clean
all: $(OUTPUT) clean

# This target extracts TeX files from within a DTX archive
# and removes the autogenerated preambles and postambles.
%-pdflatex.tex %-lualatex.tex: %.ins example.dtx
	xetex $<
	sed -ni '/^%\{3,\}/,/^\\end{document}/p' \
		$(patsubst %-pdflatex.tex,%-,$(subst lua,pdf,$@))*.tex

# This target typesets a pdfLaTeX example.
%-pdflatex.pdf: %-pdflatex.tex example.bib
	$(PDFLATEX) $< # The initial typesetting.
	biber                               $(basename $<).bcf
	$(PDFLATEX) $< # Update the index after the bibliography insertion.
	texindy -I latex -C utf8 -L english $(basename $<).idx
	$(PDFLATEX) $< # The final typesetting, now also with index.
	$(PDFLATEX) $<

# This target typesets a LuaLaTeX example.
%-lualatex.pdf: %-lualatex.tex example.bib
	$(LUALATEX) $< # The initial typesetting.
	biber                               $(basename $<).bcf
	$(LUALATEX) $< # Update the index after the bibliography insertion.
	texindy -I latex -C utf8 -L english $(basename $<).idx
	$(LUALATEX) $< # The final typesetting, now also with index.
	$(LUALATEX) $<

# This target removes any auxiliary files.
clean:
	rm -f $(AUXFILES)
	rm -rf $(AUXDIRS)

# This target removes any auxiliary files
# and the output PDF files.
implode: clean
	rm -f $(OUTPUT)
