TEXBASE=main

TEXFILE=$(TEXBASE).tex
PDFFILE=$(TEXBASE).pdf
REVISION=64c64649d6eb373dea5b0a7c35a9a5fb8fc173f6
TODAYREV=$(shell git log | grep -A 5 "`date | sed -e 's/  / /g; s/..:..:.. .* /.* /'`" | sed -n -e '/^commit/{s/^commit //; p}' | tail -n 1)

all: bib

bib: $(TEXBASE).bbl
$(TEXBASE).bbl: $(TEXFILE)
	pdflatex $(TEXFILE)
	bibtex $(TEXBASE)
	pdflatex $(TEXFILE)
	pdflatex $(TEXFILE)
	pdflatex $(TEXFILE)
	@echo
	@echo "BibTeX log:"
	@sed -n -e '1,/You.ve used/p' $(TEXBASE).blg

pdf: $(PDFFILE)
$(PDFFILE) : $(TEXFILE)
	pdflatex -interaction=scrollmode $< | tee /dev/stderr | fgrep -q "Rerun to get cross-references right" && pdflatex -interaction=scrollmode $< || true

again :
	pdflatex $(TEXFILE)


diffrev :
	#latexdiff-git -r $(REVISION) --pdf $(TEXFILE)
	latexdiff-git -r $(REVISION) $(TEXFILE)
	-pdflatex $(TEXBASE)-diff$(REVISION)
	-bibtex $(TEXBASE)-diff$(REVISION)
	-pdflatex $(TEXBASE)-diff$(REVISION)
	-pdflatex $(TEXBASE)-diff$(REVISION)
	-pdflatex $(TEXBASE)-diff$(REVISION)
	mv $(TEXBASE)-diff$(REVISION).pdf $(TEXBASE)-diff.pdf
	rm $(TEXBASE)-diff$(REVISION).*

difftoday :
	latexdiff-git -r $(TODAYREV) --pdf $(TEXFILE)
	mv $(TEXBASE)-diff$(TODAYREV).pdf $(TEXBASE)-diff.pdf
	rm $(TEXBASE)-diff$(TODAYREV).*
	echo $(TODAYREV)

diff :
	-latexdiff-git -r --pdf $(TEXFILE)
	rm $(TEXBASE)-diff.tex $(TEXBASE)-diff.blg $(TEXBASE)-diff.out


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

clean:
	rm -rf auto *~ *.aux *.blg *.bbl *.dvi *.log *.out

cleanall: clean
	rm -f $(PDFFILE)
