BASENAME=pointprocesses

TEXFILE=$(BASENAME).tex
DVIFILE=$(BASENAME).dvi
PSFILE=$(BASENAME).ps
PDFFILE=$(BASENAME).pdf

FOILSFILE=foils.pdf
THUMBNAILSFILE=thumbnails.pdf

HTMLBASENAME=$(BASENAME)h
HTMLTEXFILE=$(BASENAME)h.tex
HTMLDIR=$(BASENAME)h

GPTFILES=$(wildcard *.gpt)
GPTTEXFILES=$(GPTFILES:.gpt=.tex)


all: ps pdf talk again watchps watchpdf foils thumbs html html1 epsfigs clean cleanup cleanplots help
.PHONY: epsfigs


# thumbnails:
thumbs: $(THUMBNAILSFILE)
$(THUMBNAILSFILE): $(TEXFILE) $(GPTTEXFILES)
	sed -e 's/setboolean{presentation}{true}/setboolean{presentation}{false}/; s/usepackage{crop}/usepackage[frame]{crop}/' $< > thumbsfoils.tex
	pdflatex thumbsfoils | tee /dev/stderr | fgrep -q "Rerun to get cross-references right" && pdflatex thumbsfoils || true
	pdfnup --nup 2x4 --no-landscape --paper a4paper --trim "-1cm -1cm -1cm -1cm" --outfile $@ thumbsfoils.pdf '1-19'
	rm thumbsfoils.*

# transparencies:
foils: $(FOILSFILE)
$(FOILSFILE): $(TEXFILE) $(GPTTEXFILES)
	sed -e 's/setboolean{presentation}{true}/setboolean{presentation}{false}/' $< > tfoils.tex
	pdflatex tfoils | tee /dev/stderr | fgrep -q "Rerun to get cross-references right" && pdflatex tfoils || true
	pdfnup --nup 1x2 --orient portrait --trim "-1mm -1mm -1mm -1mm" --frame true --delta "1cm 1cm" --paper a4paper --outfile tfoils2.pdf tfoils.pdf
	pdfnup --nup 1x1 --orient portrait --trim "-2cm -2cm -2cm -2cm" --paper a4paper --outfile $@ tfoils2.pdf 
	rm tfoils.* tfoils2.pdf

# talk:
talk: $(PDFFILE)
pdf: $(PDFFILE)
$(PDFFILE): $(TEXFILE) $(GPTTEXFILES)
	pdflatex -interaction=scrollmode $< | tee /dev/stderr | fgrep -q "Rerun to get cross-references right" && pdflatex -interaction=scrollmode $< || true
# batchmode (no output, no stop on error)
# nonstopmode / scrollmode (no stop on error)
# errorstopmode (stop on error)


again :
	pdflatex $(TEXFILE)

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

# html
html : $(HTMLTEXFILE) $(GPTTEXFILES)
	rm -f $(HTMLDIR)/*
	htlatex $<
	mkdir -p $(HTMLDIR)
	mv $(HTMLBASENAME).html $(HTMLDIR)
	mv $(HTMLBASENAME)*.* $(HTMLDIR)
	mv z*.gif $(HTMLDIR)
	cd $(HTMLDIR); for i in *.gif; do convert -page +0+0 $$i tmp.gif; mv tmp.gif $$i; done; rmtex $(HTMLBASENAME)

#$(HTMLTEXFILE) : $(TEXFILE) Makefile
#	sed 's/setboolean{html}{false}/setboolean{html}{true}/; s/\\colorbox{white}{\(.*\)}/\1/g' $< > $@

html1 : $(HTMLTEXFILE) $(GPTTEXFILES)
	latex2html -dir $(HTMLDIR) -mkdir -subdir -nonavigation -noinfo -image_type png -notransparent -white -split 0 $<
	sed 's-<I>Date:</I>--' $(HTMLDIR)/$(HTMLDIR).html > tmp.html
	cp tmp.html $(HTMLDIR)/index.html
	mv tmp.html $(HTMLDIR)/$(HTMLDIR).html

$(HTMLTEXFILE) : $(TEXFILE)
	sed '/^%nohtml/,/^%endnohtml/d; s/\\colorbox{white}{\(.*\)}/\1/g' $< > $@


# eps of all figures:
epsfigs:
	mkdir -p epsfigs; \
	for i in $(GPTFILES); do \
	  { sed -n -e '1,/\\begin{document}/p' $(TEXFILE); echo "\texpicture{$${i%%.*}}"; echo "\end{document}"; } > tmp.tex; \
	  latex tmp.tex; \
	  dvips tmp.dvi; \
	  ps2eps tmp.ps; \
	  mv tmp.eps epsfigs/$${i%%.*}.eps; \
	  rm tmp.*; \
	done


# plots:
%.tex: %.gpt whitestyles.gp
	gnuplot whitestyles.gp $<
	epstopdf $*.eps


clean :
	rm -f *~
	rmtex $(BASENAME)
	rm -f $(GPTTEXFILES)

cleanup :
	rm -f *~
	rmtex $(BASENAME)
	rm -f $(PSFILE) $(PDFFILE) $(FOILSFILE) $(THUMBNAILSFILE)
	rm -f $(GPTTEXFILES)
	rm -f -r $(HTMLDIR)


cleanplots :
	sed -n -e '/\\begin{document}/,/\\end{document}/p' $(TEXFILE) | fgrep '\input{' | grep -v '^%' | sed 's/.*input{\(.*\).tex}.*/\1.gpt/' > plot.fls
	mkdir -p unusedplots
	for i in *.gp*; do \
	  grep -q $$i plot.fls || { grep -q $$i $$(<plot.fls) && echo $$i || mv $$i unusedplots; }; \
	done >> plot.fls
	for i in $$(<plot.fls); do \
	  sed "s/\([^'\" ]*\.dat\)/\n\1\n/g;" $$i | fgrep .dat; \
	done | sort | uniq > dat.fls
	mkdir -p unuseddata 
	for i in *.dat; do \
	  grep -q $$i dat.fls || mv $$i unuseddata; \
	done
	rm dat.fls plot.fls


help : 
	@echo -e \
	"make pdf:    make the pdf file of the talk.\n"\
	"make foils:  make black&white postscript foils of the talk.\n"\
	"make thumbs: make color thumbnails of the talk.\n"\
	"make again: run latex and make the pdf file of the talk,\n"\
        "            no matter whether you changed the .tex file or not.\n\n"\
	"make watchpdf:   make the pdf file of the talk\n"\
        "                 whenever the tex file is modified.\n"\
	"make html:       make a html version of the paper (in $(HTMLDIR)).\n\n"\
	"make clean:      remove all intermediate files,\n"\
        "                 just leave the source files and the final .ps and .pdf files.\n"\
	"make cleanup:    remove all intermediate files as well as\n"\
        "                 the final .ps and .pdf files.\n"\
	"make cleanplots: move all unused .gpt and .dat files\n"\
        "                 into unusedplots/ and unuseddata/, respectively."