BASENAME=sachbeihilfe

TEXFILE=$(BASENAME).tex
PDFFILE=$(BASENAME).pdf
TXTFILE=$(BASENAME).txt
REVISION=77a9cf8ccd2e6ed502b8b033af070b2aaa2108

all: bib

bib :
	-pdflatex $(TEXFILE)
	-bibtex $(BASENAME)
	#-sed -i 's/{Author1}/{\\bibemph{Author1}}/;s/{Author2}/{\\bibemph{Author2}}/;s/{Author3}/{\\bibemph{Author4}}/' $(BASENAME).bbl
	-pdflatex $(TEXFILE)
	-pdflatex $(TEXFILE)
	-pdflatex $(TEXFILE)
	@echo
	@echo "BibTeX log:"
	@sed -n -e '1,/You.ve used/p' $(BASENAME).blg

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

again :
	pdflatex $(TEXFILE)

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

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


# statistics #########################################################
stats: $(PDFFILE)
# use \pagestyle{empty} and don't include any pictures!
	@pdftotext -nopgbrk $(PDFFILE) - | fold -s > tmp.txt
	@echo "     words: " `wc -w tmp.txt 2> /dev/null | cut -d ' ' -f 1` 
	@echo "characters: " `wc -c tmp.txt 2> /dev/null | cut -d ' '  -f 1`
	@rm tmp.txt


# clean up ############################################################
clean :
	rm -f *~ $(BASENAME).aux $(BASENAME).bbl $(BASENAME).blg $(BASENAME).log $(BASENAME).out $(BASENAME)-blx.bib $(BASENAME).run.xml $(BASENAME).rtf
cleanall: clean
	rm -f $(PDFFILE) $(BASENAME)-diff.pdf


# help ################################################################
help : 
	@echo \
	"make pdf:          make the pdf file of the proposal.\n"\
	"make bib:          run bibtex and make the pdf file of the proposal.\n"\
	"make again:        run latex and make the pdf file of the proposal,\n"\
        "                  no matter whether you changed the .tex file or not.\n"\
	"make watchpdf:     make the pdf file of the paper\n"\
        "                  whenever the tex file is modified.\n"\
	"make diff:         make a diff file since REVISION.\n"\
	"make clean:        remove all intermediate files,\n"\
        "                  just leave the source files and the final .pdf files.\n"\
	"make cleanall:     clean and remove the final .pdf files."
