lectures = 1 2 3 4 5 6 7 8 9 10 11 12 13 14
pdfnotes = $(foreach lecture,$(lectures),note$(lecture).pdf)
pdffiles = $(pdfnotes) forms.pdf
pngfiles = $(pdffiles:.pdf=.png)

OS := $(shell uname -o)
ifeq ($(OS),Android)
	LATEX = tectonic #-Z shell-escape
else
	LATEX = pdflatex -shell-escape
endif
	

default: $(pdffiles) #$(pngfiles)
clean:
	$(RM) $(pdffiles) *.png *.log *.aux

forms.pdf:forms.tex
	$(LATEX) $<
	$(LATEX) $<

%.pdf: %.latex
	$(LATEX) $*.latex
	$(LATEX) $*.latex
	chmod 644 $*.pdf

%.latex: %.tex #Makefile
	echo '\documentclass[a4paper]{article}' > $*.latex
#       echo '\documentclass[a4paper,twocolumn]{article}' > $*.latex
	echo '\usepackage[utf8]{inputenc}' >> $*.latex
#	echo '\usepackage[english,danish]{babel}' >> $*.latex
	echo '\usepackage[english]{babel}' >> $*.latex
	echo '\usepackage{a4wide}' >> $*.latex
#	echo '\usepackage{fullpage}' >> $*.latex
#       echo \
        '\usepackage[a4paper,twoside,bindingoffset=10mm,top=20mm,bottom=15mm,hmargin=10mm]{geometry}' \
        >> $*.latex
	echo '\usepackage{graphicx}' >> $*.latex
#       echo '\usepackage{graphics}' >> $*.latex
#	echo '\usepackage{amsmath}' >> $*.latex
	echo '\usepackage{mathtools}' >> $*.latex
	echo '\usepackage{amssymb}' >> $*.latex
	echo '\usepackage{fancyhdr}' >> $*.latex
	#echo '\usepackage{gnuplottex}' >> $*.latex
	#echo '\usepackage{tikz}' >> $*.latex
	echo '\usepackage{pgfplots}' >> $*.latex
#       echo '\pagestyle{myheadings}' >> $*.latex
#       echo '\pagestyle{empty}' >> $*.latex
	/bin/echo '\begin{document}' >> $*.latex
	/bin/echo '\pagestyle{fancy}' >> $*.latex
	/bin/echo '\lhead{$*}' >> $*.latex
	/bin/echo '\rhead{[\today]}' >> $*.latex
#       echo '\markright{$* [\\today]}' >> $*.latex
#	cat $*.tex >> $*.latex
#	cat $*.tex | sed \
#                -e 's/\\subsubsection/\\paragraph/' \
#                -e 's/\\subsection/\\subsubsection/' \
#                -e 's/\\section/\\subsection/' \
#                -e 's/\\chapter/\\section/' >> $*.latex
#
	cat $*.tex | sed \
               -e 's/\\subsubsection/\\subsubsection*/' \
               -e 's/\\subsection/\\subsection*/' \
               -e 's/\\section/\\section*/' \
               -e 's/\\chapter/\\chapter*/' >> $*.latex
	/bin/echo '\end{document}' >> $*.latex

note10.tex: perihelion.pdf
perihelion.pdf: perihelion.gpi
	gnuplot $<

#note1.tex: Notes/prologue.tex Notes/str.tex
#	if [ -f $@ ]; then cp -f $@ bak; fi
#	cat $^ > $@
#
#note2.tex: Notes/equiv.tex
#	if [ -f $@ ]; then cp -f $@ bak; fi
#	cat $^ > $@
#
#note3.tex: Notes/covariantd.tex
#	if [ -f $@ ]; then cp -f $@ bak; fi
#	cat $^ > $@
#
#note4.tex: Notes/geodesic.tex
#	if [ -f $@ ]; then cp -f $@ bak; fi
#	cat $^ > $@
#
#note5.tex: Notes/action.tex
#	if [ -f $@ ]; then cp -f $@ bak; fi
#	cat $^ > $@
#
#note6.tex: Notes/emtensor.tex
#	if [ -f $@ ]; then cp -f $@ bak; fi
#	cat $^ > $@
#
#note7.tex: Notes/hilbert.tex
#	if [ -f $@ ]; then cp -f $@ bak; fi
#	cat $^ > $@
#
#note8.tex: Notes/newton.tex
#	if [ -f $@ ]; then cp -f $@ bak; fi
#	cat $^ > $@
#
#note9.tex: Notes/schwarz.tex
#	if [ -f $@ ]; then cp -f $@ bak; fi
#	cat $^ > $@
#
#note10.tex: Notes/ctests.tex perihelion.tex
#	if [ -f $@ ]; then cp -f $@ bak; fi
#	cat $< > $@
#
#note11.tex: Notes/lemaitre.tex
#	if [ -f $@ ]; then cp -f $@ bak; fi
#	cat $< > $@
#
#note12.tex: Notes/friedman.tex
#	if [ -f $@ ]; then cp -f $@ bak; fi
#	cat $< > $@
#
#note13.tex: Notes/bbang.tex figure-bbang.tex #figure-bbang.pdf
#	if [ -f $@ ]; then cp -f $@ bak; fi
#	cat $< > $@
#
#note14.tex: Notes/length_time.tex
#	if [ -f $@ ]; then cp -f $@ bak; fi
#	cat $^ > $@
#
#figure-bbang.pdf: Notes/figure-bbang.ppl
#	pyxplot $<
#figure-bbang.tex: Notes/figure-bbang.gpi
#	gnuplot $<
#perihelion.tex: Notes/perihelion.gpi
#	gnuplot $<

%.png : %.pdf
	rm -f $*-*.png
	gs -sDEVICE=pngalpha -r120x120 -sOutputFile=$*-%d.png \
        -dBackgroundColor='16#ffffff' -sPAPERSIZE=a4 -dBATCH -dNOPAUSE $*.pdf
#       gs -sDEVICE=jpeggray -r110x110 -sOutputFile=$*.jpeg \
#       -sPAPERSIZE=a4 -dBATCH -dNOPAUSE $*.ps
	cp $*-1.png $*.png

