IN = src/
OUT = dist/
TMP = temp/

sources = $(shell find $(IN) -type f)
sourcedirs = $(shell find $(IN) -type d)
targetdirs = $(subst $(IN),$(OUT),$(sourcedirs))

texsources = $(shell find $(IN) -type f -name *.tex)
textargets = $(subst $(IN),$(OUT),$(texsources:.tex=.pdf))

copysources = $(filter-out $(texsources),$(sources))
copytargets = $(subst $(IN),$(OUT),$(copysources))

all: $(copytargets) $(textargets)

# the | means that if the timestamp of anything on the right changes, no need to rebuild
# the two :'s mean that the implicit rule of "$(list): %.pdf: %.tex" is applied only to the targets in $(list)
$(copytargets): $(OUT)%: $(IN)% | $(targetdirs)
	cp $< $@

$(targetdirs):
	mkdir -p $@

$(textargets): $(OUT)%.pdf: $(IN)%.tex | $(targetdirs)
# $< is the name of the first prerequisit,
# $@ is the name of the target
	mkdir -p $(TMP) && \
	texfot pdflatex -output-directory=$(TMP) $< && \
	texfot pdflatex -output-directory=$(TMP) $< && \
	mv $(TMP)$(@F) $(subst $(IN),$(OUT),$@)



clean:
	rm -rf $(OUT) $(TMP)
