EMACS=emacs
BATCH_EMACS=$(EMACS) --batch -Q -l init.el

PAPER:=replace-with-paper-file-name

all: $(PAPER).pdf $(FIG_SVG)
.PHONY: all publish clean

# Export
%.html: %.org init.el
	$(BATCH_EMACS) $*.org -f org-html-export-to-html

%.tex: %.org init.el org-paper-class.tex
	$(BATCH_EMACS) $*.org -f org-latex-export-to-latex

%.txt: %.org init.el
	$(BATCH_EMACS) $*.org -f org-export-as-utf8

%.pdf: %.tex $(FIG_TEX)
	rm -f dissertation.aux
	pdflatex \\nonstopmode\\input $*.tex
# Uncomment if using Bibtex
# if [ -f $*.bib ];then bibtex --min-crossref=100 $*;fi
	while grep "Rerun to get" $*.log; do \
		pdflatex \\nonstopmode\\input $*.tex; \
	done
# Uncomment if using a Glossary
# if [ -f glossary.tex ];then makeglossaries $*;fi
	pdflatex \\nonstopmode\\input $*.tex;

%.ps: %.pdf
	pdf2ps $*.pdf

%.svg: %.tex
	./bin/tex2svg $<

# Publish
publish/index.html: $(PAPER).html
	cp $< $@

publish/%: %
	cp $< $@

publish: publish/index.html publish/$(PAPER).pdf
	chmod a+r $<

clean:
	rm -f *~ *.aux *.log *.dvi *.blg *.bbl *.toc *.out *.html *.vrb *.snm *.nav *.ps *.lof *.lol *.lot *.glg *.glo *.gls *.glsdefs *.xdy
	rm -f $(PAPER).tex $(PAPER).pdf $(PAPER).html $(PAPER).txt publish/index.html publish/$(PAPER).pdf
