.PHONY: all clean genreadme

DOCS_ALL=$(wildcard **/**/README.md)
WRITEUP_PDF=writeup.pdf

all: README.md $(WRITEUP_PDF)

clean:
	$(RM) -r $(WRITEUP_PDF) build/

README.md: $(DOCS_ALL)
	@echo "GEN    README.md"
	@(printf "# BLAHAJ Hack-a-Sat 2020 Writeups\n\n"; for file in $(DOCS_ALL); do \
		printf -- "- [%s](%s)\n" "$$(dirname $$file)" "$$(dirname $$file)"; done) > README.md

$(WRITEUP_PDF): $(DOCS_ALL) fonts.tex misc.tex top.md
	@echo "GEN    writeup.md"
	@mkdir build/ 2>/dev/null || true
	@find . -mindepth 3 -iname '*.png' -exec cp {} build/ \;
	@find . -mindepth 3 -iname '*.py' -exec cp {} build/ \;
	@cp fonts.tex misc.tex jork.png build/
	@for md in top.md $(DOCS_ALL); do cat $$md; printf "\n\n\\\\newpage\n\n"; done > build/writeup.md
	@echo "PDF    writeup.pdf"
	@cd build && \
		pandoc --filter pandoc-include-code --toc --toc-depth 1 -f markdown-implicit_figures \
		-i writeup.md --pdf-engine=lualatex -o writeup.pdf
	@cp build/writeup.pdf $@
