BASENAME=scientificcomputing-script

SUBDIRS=programming debugging plotting codestyle statistics bootstrap regression likelihood pointprocesses designpattern 
SUBTEXS=$(foreach subd, $(SUBDIRS), $(subd)/lecture/$(subd).tex)

all : plots chapters index script exercises

chapters :
	for sd in $(SUBDIRS); do $(MAKE) -C $$sd/lecture chapter; done

script : plots $(BASENAME).pdf


plots :
	for sd in $(SUBDIRS); do $(MAKE) -C $$sd/lecture plots; done


pdf : $(BASENAME).pdf

$(BASENAME).pdf : $(BASENAME).tex header.tex $(SUBTEXS)
	pdflatex -interaction=scrollmode $< | tee /dev/stderr | fgrep -q "Rerun to get cross-references right" && pdflatex -interaction=scrollmode $< || true
	splitindex $(BASENAME).idx

index :
	pdflatex $(BASENAME).tex
	splitindex $(BASENAME).idx
	pdflatex $(BASENAME).tex
	pdflatex $(BASENAME).tex

again :
	pdflatex $(BASENAME).tex

watchpdf :
	while true; do ! make -s -q pdf && make pdf; sleep 0.5; done


watchscript :
	while true; do ! make -s -q script && make script; sleep 0.5; done


exercises:
	for sd in $(SUBDIRS); do if test -d $$sd/exercises; then $(MAKE) -C $$sd/exercises pdf; fi; done


cleanplots:
	for sd in $(SUBDIRS); do $(MAKE) -C $$sd/lecture cleanplots; done

cleantex:
	rm -f *~  $(BASENAME).aux $(BASENAME).log $(BASENAME).out $(BASENAME)-solutions.tex $(BASENAME).toc $(BASENAME).lo?  $(BASENAME).idx $(BASENAME)-term.i* $(BASENAME)-enterm.i* $(BASENAME)-code.i*
	for sd in $(SUBDIRS); do $(MAKE) -C $$sd/lecture cleantex; done

clean : cleantex
	for sd in $(SUBDIRS); do $(MAKE) -C $$sd/lecture clean; done
	for sd in $(SUBDIRS); do if test -d $$sd/exercises; then $(MAKE) -C $$sd/exercises clean; fi; done

cleanall : clean
	rm -f $(BASENAME).pdf
	for sd in $(SUBDIRS); do $(MAKE) -C $$sd/lecture cleanall; done