BASENAME=scientificcomputing-script

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

all : script chapters

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 -interaction=scrollmode $(BASENAME).tex
	splitindex $(BASENAME).idx
	pdflatex -interaction=scrollmode $(BASENAME).tex | tee /dev/stderr | fgrep -q "Rerun to get cross-references right" && pdflatex $(BASENAME).tex || true

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


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

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