.PHONY: default clean

COPY_PDF=$(shell find . -name '*.pdf' -not -path './build/*')
COPY_TARGETS=$(COPY_PDF:%.pdf=build/%.pdf)

COPY_JPG=$(shell find . -name '*.jpg' -not -path './build/*')
COPY_TARGETS_JPG=$(COPY_JPG:%.jpg=build/%.jpg)

MD_SOURCES=$(shell find . -name '*.md' -not -path "./templates/*")
HTML_TARGETS=$(MD_SOURCES:%.md=build/%.html)
PDF_TARGETS=$(MD_SOURCES:%.md=build/%.pdf)

DOT_SOURCES=$(shell find . -name '*.dot' -not -path "./templates/*")
DOT_TARGETS=$(DOT_SOURCES:%.dot=build/%.png)

TEX_SOURCES=$(shell find . -not -path "./templates/*" -name '*.tex')
TEX_TARGETS=$(TEX_SOURCES:%.tex=build/%.pdf)

COMPRESS_TARGETS=$(HTML_TARGETS:%=%.gz)
COMPRESS_TARGETS+=$(PDF_TARGETS:%=%.gz)
COMPRESS_TARGETS+=$(TEX_TARGETS:%=%.gz)

default: $(DOT_TARGETS) $(HTML_TARGETS) $(PDF_TARGETS) $(TEX_TARGETS) $(COPY_TARGETS) $(COPY_TARGETS_JPG)

compress: default $(COMPRESS_TARGETS)

build/%.html: %.md
	@mkdir -p $$(dirname $@)
	pandoc -s --mathjax --template ./templates/mindoc.html --toc --toc-depth=2 -o $@  $<

build/%.pdf: %.md
	@mkdir -p $$(dirname $@)
	pandoc --resource-path=$$(dirname $@) --template ./templates/eisvogel.tex --toc --toc-depth=2 -o $@  $<

build/%.png: %.dot
	@mkdir -p $$(dirname $@)
	dot -Tpng -o $@ $<

build/%.pdf: %.tex
	@mkdir -p $$(dirname $@)
	latexmk -shell-escape -pdf -outdir=$$(dirname $@) $<

%.gz: %
		gzip -k -f -9 $<

build/%.pdf: %.pdf
	@mkdir -p $$(dirname $@)
	cp $< $@

build/%.jpg: %.jpg
	@mkdir -p $$(dirname $@)
	cp $< $@

clean:
	rm -r build
