targets := $(shell find -name '*.tex' -not -name 'main.tex')
extensions := *.aux *.lof *.log *.lot *.fls *.out *.toc *.fmt *.fot *.cb *.cb2 .*.lb

.PHONY: all
all: clean build postclean

.PHONY: build
build: $(targets)

$(targets): %.tex: %_statuten.pdf

%_statuten.pdf:
	sed -i 's/ksv/$*/g' main.tex
	pdflatex main.tex
	pdflatex main.tex
	sed -i 's/$*/ksv/g' main.tex
	mv main.pdf $@

.PHONY: postclean
postclean:
	rm -f $(extensions)

.PHONY: clean
clean: $(targets:%=clean_%)

$(targets:%=clean_%): clean_%.tex: %_clean

%_clean:
	rm -f $*_statuten.pdf

.PHONY: cleanandbuild
cleanAndBuild: clean build postclean
