GRAPHICS_FILES = $(shell bin/get-auto-graphics)

.PHONY = open clean clean-all clean-images

main.pdf: main.latex $(GRAPHICS_FILES)
	pdflatex -shell-escape -output-directory="./" main.latex

with-notes.pdf: with-notes.latex
	pdflatex -shell-escape -output-directory="./" with-notes.latex

with-notes.latex: main.latex $(GRAPHICS_FILES)
	sed 's/\%showNotes\%//' main.latex > with-notes.latex

only-notes.pdf: only-notes.latex
	pdflatex -shell-escape -output-directory="./" only-notes.latex

only-notes.latex: main.latex $(GRAPHICS_FILES)
	sed 's/\%onlyNotes\%//' main.latex > only-notes.latex

auto-images/auto-xkcd-%.png:
	mkdir -p auto-images
	curl https://xkcd.com/$*/info.0.json 2> /dev/null | jq '.img' | sed 's/.png/_2x.png/g' | xargs curl -s -o $@

auto-images/auto-shell-%.pdf: auto-images/auto-shell-%.svg
	mkdir -p auto-images
	inkscape auto-images/auto-shell-$*.svg --export-area-drawing --batch-process --export-type=pdf --export-filename=$@

auto-images/auto-shell-%.svg: auto-images/%.out
	mkdir -p auto-images
	cat auto-images/$*.out | bin/ansi2svg > $@

auto-images/%.out: shell/%
	mkdir -p auto-images
	shell/$* | bin/tidy | bin/prompt > $@

auto-images/auto-download-%:
	mkdir -p auto-images
	bin/download-image $@


open: main.pdf
	setsid zathura main.pdf &

open-with-notes: with-notes.pdf
	setsid zathura with-notes.pdf &

clean:
	- rm *.aux *.log *.out *.toc *.nav *.snm *.pdf *.vrb
	-rm -rf _minted-main
	-rm -rf _minted-with-notes
	-rm -rf _minted-only-notes
	- rm with-notes.latex
	- rm only-notes.latex
	- rm -rf /tmp/demo

clean-images:
	- rm -rf auto-images/

clean-all: clean clean-images