.PHONY: force clean veryclean official commit book all mitpress indexcounts target %.preflight preflight chapters comparepdfs remake-on-change
.PHONY: xpdf %.xpdf %.onlyxpdf
.PRECIOUS: %.pdf

# export TEXMFHOME := /home/sanjoy/texmf-recentcontext-texlive2008
# PATH  := /usr/local/texlive/2008/bin/i386-linux:$(PATH)

# to pass to texexec
TARGS    ?= $(EXTRA) --nonstopmode --passon="-file-line-error -recorder -interaction nonstopmode"
texexec  := texexec $(TARGS)

bookdefault := test

target  : xpdf
xpdf    : $(bookdefault).xpdf
onlyxpdf: $(bookdefault).onlyxpdf

test.pdf: env.tex project.bib

%.xpdf: %.pdf
	xpdf -remote $(abspath $(*)) -raise $(abspath $(*).pdf) &
	sleep 0.2 ; xpdf -remote $(abspath $(*)) -exec goBackward &
%.onlyxpdf: 
	xpdf -remote $(abspath $(*)) -raise $(abspath $(*).pdf) &
%.pdf: %.tex
	cd $(dir $*) && $(texexec) $(notdir $*.tex) > all.log || ( $(CURDIR)/util/tex-errors.sh < all.log ; false )

gen_clean := *~ *-fig:* *~ *.[0-9] *.[0-9][0-9] *.[0-9][0-9][0-9]
gen_clean += *-mpgraph*.keep *.aux *.bbl *.blg auto/*.el mptextmp.*
gen_clean += fig-[0-9].pdf fig-[0-9][0-9].pdf fig-[0-9][0-9][0-9].pdf mpxerr.* texexec.tex texexec.pdf

clean:
	-ctxtools --purgefiles --all --recurse > /dev/null
	rm -f $(gen_clean) *.pdf *.fls
