# plots:
plots : pythonplots gnuplots

# python plots:
PYFILES=$(wildcard *.py)
PYPDFFILES=$(PYFILES:.py=.pdf)

pythonplots : $(PYPDFFILES)

$(PYPDFFILES) : %.pdf: %.py
	python $<

cleanpythonplots :
	rm -f $(PYPDFFILES)


# gnuplot plots:
GPTFILES=$(wildcard *.gpt)
GPTTEXFILES=$(GPTFILES:.gpt=.tex)

gnuplots : $(GPTTEXFILES)

$(GPTTEXFILES) : %.tex: %.gpt whitestyles.gp
	gnuplot whitestyles.gp $<
	epstopdf $*.eps

cleangnuplots :
	rm -f $(GPTTEXFILES)


# script:
chapter : $(BASENAME)-chapter.pdf

$(BASENAME)-chapter.pdf : $(BASENAME)-chapter.tex $(BASENAME).tex $(wildcard $(BASENAME).aux) $(PYPDFFILES) $(GPTTEXFILES) ../../header.tex
	if test -f $(BASENAME).aux; then \
	  CHAPTER=$$(( $$(sed -n -e '/contentsline {chapter}/{s/.*numberline {\([0123456789]*\)}.*/\1/; p}' $(BASENAME).aux) - 1 )); \
	  PAGE=$$(sed -n -e '/contentsline {chapter}/{s/.*numberline {.*}.*}{\(.*\)}{chapter.*/\1/; p}' $(BASENAME).aux); \
	fi; \
	{ echo $${PAGE:=1}; echo $${CHAPTER:=0}; } | pdflatex -interaction=scrollmode $< | tee /dev/stderr | fgrep -q "Rerun to get cross-references right" && { echo $${PAGE:=1}; echo $${CHAPTER:=0}; } | pdflatex -interaction=scrollmode $< || true

watchchapter :
	while true; do ! make -q chapter && make chapter; sleep 0.5; done


cleanchapter : cleanpythonplots cleangnuplots
	rm -f *~ 
	rm -f $(BASENAME).aux $(BASENAME).log
	rm -f $(BASENAME)-chapter.aux $(BASENAME)-chapter.log $(BASENAME)-chapter.out $(BASENAME)-chapter.idx


cleanallchapter : cleanchapter
	rm -f $(BASENAME)-chapter.pdf