EMACS=emacs
BATCH_EMACS=$(EMACS) --batch -Q -l init.el
MAINDOC	= paper
RM = rm

%.tex:
	$(BATCH_EMACS) $*.org -f org-latex-export-to-latex

%.pdf: %.tex
	if pdflatex $*.tex </dev/null; then \
		true; \
	else \
		stat=$$?; touch $*.pdf; exit $$stat; \
	fi
	bibtex $*
	while grep "Rerun to get" $*.log; do \
		if pdflatex $*.tex </dev/null; then \
			true; \
		else \
			stat=$$?; touch $*.pdf; exit $$stat; \
		fi; \
	done

all: pdf
tex: $(MAINDOC).tex
pdf: $(MAINDOC).pdf
html:
	$(BATCH_EMACS) $(MAINDOC).org -f org-html-export-as-html

clean:
	$(RM) -f *.aux *.bbl *.blg *.log

cleanall : clean
	$(RM) -f *.dvi *.cfg *.idx *.ilg *.ind *.toc *.lot *.lof *.pdf *.out
	$(RM) -rf $(MAINDOC)

.PHONY: clean cleanall
