######### Latex Command #########

LATEX=pdflatex

######### Source Files #########

# driver files
HANDOUT=handout
PRESENTATION=presentations
SLIDES=slides

######### Generated files to be deleted #########

RUBBISH=*.aux *.dvi *.log *.ps *~ *.*~ *.out *.bak *-driver.* *.nav *.snm *.toc *.tmp *.fdb_latexmk *.fls


######### Dependencies #########

DEPENDENCIES=Makefile content.tex lecturedetails.tex preamble.tex


######### Default Target #########

default: slides
pdf: default

######### Phony Targets #########

.PHONY:	default viewpres viewhandout viewslides clean clean-rubbish


######### Main Targets #########

presentation: presentation.pdf clean-rubbish

slides: slides.pdf clean-rubbish

quick: quick_pres clean-rubbish

handout: handout.pdf clean-rubbish

all: clean presentation slides handout

######### Internal Targets #########

presentation.pdf: $(DEPENDENCIES) presentation.tex

slides.pdf: $(DEPENDENCIES) slides.tex

numbered-slides.pdf: slides.pdf

handout.pdf: numbered-slides.pdf

######### Driver Targets #########

presentation.pdf: $(DEPENDENCIES) presentation.tex
	# run pdflatex as many times as needed to get references correct (at least twice to get PDF bookmarks right)
	$(LATEX) presentation.tex
	$(LATEX) presentation.tex

	# once more if there are still undefined references
	if [ -n "$(shell grep 'There were undefined references' presentation.log)" ]; then $(LATEX) presentation.tex ; fi

	# once more if there are labels that have changed
	if [ -n "$(shell grep 'Label(s) may have changed' presentation.log)" ]; then $(LATEX) presentation.tex ; fi


slides.pdf: $(DEPENDENCIES) slides.tex
	# run pdflatex as many times as needed to get references correct (at least twice to get PDF bookmarks right)
	$(LATEX) slides.tex
	$(LATEX) slides.tex

	# once more if there are still undefined references
	if [ -n "$(shell grep 'There were undefined references' slides.log)" ]; then $(LATEX) slides.tex ; fi

	# once more if there are labels that have changed
	if [ -n "$(shell grep 'Label(s) may have changed' slides.log)" ]; then $(LATEX) slides.tex ; fi

handout.pdf: slides.pdf handout.tex
	$(LATEX) handout.tex

numbered-slides.pdf: slides.pdf numbered-slides.tex
	$(LATEX) numbered-slides.tex

# Only runs latex once - for quick (but not always correct) results
quick_pres: $(DEPENDENCIES) presentation.tex
	$(LATEX) presentation.tex

######### Clean #########

clean-rubbish:
	rm -f $(RUBBISH)

clean: clean-rubbish
	rm -f slides.pdf handout.pdf presentation.pdf numbered-slides.pdf
