# plots:
plots : pythonplots

# python plots:
PYFILES=$(wildcard *.py)
PYFILES := $(filter-out dattomat.py, $(PYFILES))
PYPDFFILES=$(PYFILES:.py=.pdf)

pythonplots : $(PYPDFFILES)

$(PYPDFFILES) : %.pdf: %.py ../../plotstyle.py
	PYTHONPATH="../../" python3 $<
#ps2pdf $@ $(@:.pdf=-temp.pdf)  # strip fonts, saves only about 1.5MB
#mv $(@:.pdf=-temp.pdf) $@

cleanpythonplots :
	rm -f $(PYPDFFILES)

# 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; q}' $(BASENAME).aux) - 1 )); \
	  PAGE=$$(sed -n -e '/contentsline {chapter}/{s/.*numberline {.*}.*}{\(.*\)}{chapter.*/\1/; p; q}' $(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

watchplots :
	while true; do ! make -q plots && make plots; sleep 0.5; done

cleantex:
	rm -f *~ 
	rm -f $(BASENAME).aux $(BASENAME).log *.idx
	rm -f $(BASENAME)-chapter.aux $(BASENAME)-chapter.log $(BASENAME)-chapter.out $(BASENAME)-chapter.idx $(BASENAME)-chapter-solutions.tex $(BASENAME)-solutions.tex

cleanplots: cleanpythonplots

cleanchapter : cleanplots cleantex


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