

# Author:
# Juhapekka Tolvanen
# http://iki.fi/juhtolv
# juhtolv (at) iki (dot) fi

#  This Makefile is public domain. There is no warranty.

BASENAME = example
CM-SUPER-DIR = /usr/share/texmf/fonts/type1/public/cm-super 

help :
	@echo 'usage: make <targer>'
	@echo 'Available targets:'
	@echo ''
	@echo 'all                       : All. (Not cleaning. Purging only when necessary.)'
	@echo ''
	@echo 'html                      : Compile all html-files.'
	@echo 'tex4ht-onepage            : Compile HTML in one page.'
	@echo 'tex4ht-splitpageone       : Compile split HTML (level 1).'
	@echo 'tex4ht-splitpagetwo       : Compile split HTML (level 2).'
	@echo ''
	@echo 'dvi                       : Compile all DVI-files.'
	@echo ''
	@echo 'postscript                : Compile all PostScript-files (Makes DVI-file, too.).'
	@echo ''
	@echo 'pdf                       : Compile all PDF-files.'
	@echo 'pdf-print                 : Compile all PDF-files for print. (Makes DVI- and PostScript-files, too).'
	@echo 'pdf-screen                : Compile all PDF-files for screen.'
	@echo ''
	@echo 'swf                       : Compile Macromedia... Umm... Err... Adobe Flash -file.'
	@echo ''
	@echo 'hnb                       : Convert outline of hnb (http://hnb.sf.net/) to other formats.'
	@echo ''
	@echo 'purge-dvi                 : Remove auxiliary-files of DVIs.'
	@echo 'clean-dvi                 : Remove all DVIs and their auxiliary files.'
	@echo 'clean-ps                  : Remove all PostScript-files. (Do not remove DVI-files or their auxiliary files).'
	@echo 'purge-screen-pdf          : Remove auxiliary files made during screen-pdf.'
	@echo 'clean-screen-pdf          : Remove PDF-file for screen and all its auxiliary files.'
	@echo 'purge-print-pdf           : Remove auxiliary files made during print-pdf.'
	@echo 'clean-print-pdf           : Remove PDF-file for screen and all its auxiliary files. (Do not remove PostScript- or DVI-files).'
	@echo 'clean-all-pdf             : Remove all pdf-files and their auxiliary files (Do not remove PostScript- or DVI-files).'
	@echo 'clean-swf                 : Remove Macromedia Flash -files.'
	@echo 'purge-html                : Remove all auxiliary files of HTML'
	@echo 'clean-html-onepage        : Remove one-page-HTML and all auxiliary files.'
	@echo 'clean-html-splitpageone   : Remove split-page-HTML (Level 1) and their auxiliary files.'
	@echo 'clean-html-splitpagetwo   : Remove split-page-HTML (Level 2) and their auxiliary files.' 
	@echo 'clean-html                : Remove HTML-files and all their auxiliary files.'
	@echo 'purge hnb                 : Remove all converted files of hnb.'
	@echo ''
	@echo 'purge-emacs               : Remove backups of emacsen'
	@echo 'purge                     : Remove all auxiliary files and other such crap and backups of emacsen.'
	@echo 'clean                     : Remove both auxiliary files and final viewable and printable files.'

#all : clean pdf postscript html swf hnb
#all : clean postscript pdf html hnb
all : clean postscript pdf hnb

# HTML

tex4ht-onepage : purge-html
	htlatex $(BASENAME).tex "xhtml,uni-html" " -cunihtf -cmozhtf"
	htlatex $(BASENAME).tex "xhtml,uni-html" " -cunihtf -cmozhtf"
	htlatex $(BASENAME).tex "xhtml,uni-html" " -cunihtf -cmozhtf"
	- mv -f $(BASENAME)*.html $(BASENAME).html *.css $(BASENAME)*.png html.tex4ht.dir/

tex4ht-onepage-alt : purge-html
	latex -src-special -file-line-error-style $(BASENAME).tex
	htlatex $(BASENAME).tex "xhtml,uni-html" " -cunihtf -cmozhtf"
	htlatex $(BASENAME).tex "xhtml,uni-html" " -cunihtf -cmozhtf"
	- mv -f $(BASENAME)*.html $(BASENAME).html *.css $(BASENAME)*.png html.tex4ht.dir/

tex4ht-splitpageone : purge-html
	htlatex $(BASENAME).tex "xhtml,uni-html,1" " -cunihtf -cmozhtf"
	htlatex $(BASENAME).tex "xhtml,uni-html,1" " -cunihtf -cmozhtf"
	htlatex $(BASENAME).tex "xhtml,uni-html,1" " -cunihtf -cmozhtf"
	- mv -f $(BASENAME)*.html $(BASENAME).html *.css $(BASENAME)*.png html.tex4ht.split1.dir/

tex4ht-splitpageone-alt : purge-html
	latex -src-special -file-line-error-style $(BASENAME).tex
	htlatex $(BASENAME).tex "xhtml,uni-html,1" " -cunihtf -cmozhtf"
	htlatex $(BASENAME).tex "xhtml,uni-html,1" " -cunihtf -cmozhtf"
	- mv -f $(BASENAME)*.html $(BASENAME).html *.css $(BASENAME)*.png html.tex4ht.split1.dir/

tex4ht-splitpagetwo : purge-html
	htlatex $(BASENAME).tex "xhtml,uni-html,2" " -cunihtf -cmozhtf"
	htlatex $(BASENAME).tex "xhtml,uni-html,2" " -cunihtf -cmozhtf"
	htlatex $(BASENAME).tex "xhtml,uni-html,2" " -cunihtf -cmozhtf"
	- mv -f $(BASENAME)*.html $(BASENAME).html *.css $(BASENAME)*.png html.tex4ht.split2.dir/

# This does not work for me:
tex4ht-splitpagethree : purge-html
	htlatex $(BASENAME).tex "xhtml,uni-html,3" " -cunihtf -cmozhtf"
	htlatex $(BASENAME).tex "xhtml,uni-html,3" " -cunihtf -cmozhtf"
	htlatex $(BASENAME).tex "xhtml,uni-html,3" " -cunihtf -cmozhtf"
	mv -f $(BASENAME)*.html  $(BASENAME).html *.css $(BASENAME)*.png html.tex4ht.split3.dir/

#html : tex4ht-onepage tex4ht-splitpageone tex4ht-splitpagetwo tex4ht-splitpagethree

html : tex4ht-onepage tex4ht-splitpageone tex4ht-splitpagetwo


# DocBook (Buggy!)
#tex4ht-docbook : purge-html
#	htlatex $(BASENAME).tex "xhtml,docbook" " -cunihtf"
#	htlatex $(BASENAME).tex "xhtml,docbook" " -cunihtf"
#	mv -f $(BASENAME)*.xml html.tex4ht.docbook.dir/
#	cp -f mv12pt_lapinakyva.eps html.tex4ht.docbook.dir/

# Docbook: other try
tex4ht-docbook : purge-html
	mk4ht dbmlatex $(BASENAME).tex
	mk4ht dbmlatex $(BASENAME).tex
	mv -f $(BASENAME)*.xml html.tex4ht.docbook.dir/


# OpenOffice.org (Buggy!)
tex4ht-ooo : purge-html
	htlatex $(BASENAME).tex "xhtml,ooffice" "ooffice/! -cmozhtf" "-coo"
	htlatex $(BASENAME).tex "xhtml,ooffice" "ooffice/! -cmozhtf" "-coo"
	htlatex $(BASENAME).tex "xhtml,ooffice" "ooffice/! -cmozhtf" "-coo"
	mv -f $(BASENAME)*.sxw html.tex4ht.ooo.dir/

# MS Word -compatible HTML
tex4ht-msword : purge-html
	htlatex $(BASENAME).tex "html,word" "symbol/!" "-cvalidate"
	htlatex $(BASENAME).tex "html,word" "symbol/!" "-cvalidate"
	htlatex $(BASENAME).tex "html,word" "symbol/!" "-cvalidate"
	mv -f $(BASENAME)*.html  $(BASENAME).html *.css $(BASENAME)*.png html.tex4ht.msword.dir/

# DVI, PS and PDF

dvi : purge-dvi
	latex -src-special -file-line-error-style -enc $(BASENAME).tex
	latex -src-specials -file-line-error-style -enc $(BASENAME).tex
	while grep 'Rerun to get' $(BASENAME).log > /dev/null; do latex -src-specials -file-line-error-style -enc $(BASENAME).tex ; done
	latex -src-specials -file-line-error-style -enc $(BASENAME).tex
#	cp -f mv12pt_lapinakyva.eps dvi/mv12pt_lapinakyva.eps
	mv -f $(BASENAME).dvi dvi/$(BASENAME).dvi

# Uses recorder-option of tex. Files read by LaTeX-sourcefile have extension ".fls" by default.
dvi-recorder : purge-dvi
	latex -src-special -file-line-error-style -enc -recorder $(BASENAME).tex
	latex -src-specials -file-line-error-style -enc -recorder $(BASENAME).tex
	while grep 'Rerun to get' $(BASENAME).log > /dev/null; do latex -src-specials -file-line-error-style -enc -recorder $(BASENAME).tex ; done
	latex -src-specials -file-line-error-style -enc -recorder $(BASENAME).tex
#	cp -f mv12pt_lapinakyva.eps dvi/mv12pt_lapinakyva.eps
	mv -f $(BASENAME).dvi dvi/$(BASENAME).dvi

postscript : dvi
	cp -f dvi/$(BASENAME).dvi ./$(BASENAME).dvi
	dvips -z -Ppdf -Pwww -Pcmz -Pamz -Poutline -Pcm-super -Phfbright -G0 -j0 -f -t a4 -Z -o $(BASENAME).ps $(BASENAME).dvi
	mv -f $(BASENAME).ps ps/
	rm -f $(BASENAME).dvi

# pdf(La)TeX has bugs! Sometimes I need to disable this. Bugs may be in hyperref, too:
pdf-screen : purge-dvi
	pdflatex -src-specials -file-line-error-style -enc $(BASENAME).tex
	pdflatex -src-specials -file-line-error-style -enc $(BASENAME).tex
	while grep 'Rerun to get' $(BASENAME).log > /dev/null; do pdflatex -src-specials -file-line-error-style -enc $(BASENAME).tex ; done
	pdflatex -src-specials -file-line-error-style -enc $(BASENAME).tex
	mv -f $(BASENAME).pdf pdf/$(BASENAME).screen.pdf

pdf-print : postscript
	cp -f ps/$(BASENAME).ps ./
	ps2pdf14 -dMaxSubsetPct=100 -dSubsetFonts=true -dEmbedAllFonts=true $(BASENAME).ps
	mv -f $(BASENAME).pdf pdf/$(BASENAME).print.pdf
	rm -f $(BASENAME).ps

pdf : pdf-print pdf-screen

#pdf : pdf-print

swf : pdf-screen
	cp -f pdf/$(BASENAME).screen.pdf ./
	pdf2swf -f -F $(CM-SUPER-DIR) -t $(BASENAME).screen.pdf
	mv -f ./$(BASENAME).screen.swf ./swf/
	rm -f $(BASENAME).screen.pdf

hnb : purge-hnb
	hnb gradu.hnb -e 'go root' 'export_opml gradu.opml' 'export_html gradu.html' 'export_ascii gradu.txt' 'export_htmlcss gradu.css.html'

# purge and clean

purge-dvi :
	rm -vf $(BASENAME).dvi $(BASENAME).aux $(BASENAME).bbl $(BASENAME).blg $(BASENAME).log $(BASENAME).toc $(BASENAME).lot
	rm -vf *.aux *.bbl *.blg *.brf *.tmp *.log *.toc *.lot *.out

clean-dvi : purge-dvi
	rm -vf dvi/$(BASENAME).dvi
	rm -vf dvi/mv12pt_lapinakyva.eps

clean-ps :
	rm -vf ps/$(BASENAME).ps

purge-screen-pdf :
	rm -vf *.aux *.bbl *.blg *.brf *.tmp *.log *.toc *.lot *.out

clean-screen-pdf : purge-screen-pdf
	rm -vf pdf/$(BASENAME).screen.pdf $(BASENAME).screen.pdf

purge-print-pdf :
	rm -vf *.aux *.bbl *.blg *.brf *.tmp *.log *.toc *.lot *.out

clean-print-pdf : purge-print-pdf
	rm -vf pdf/$(BASENAME).print.pdf $(BASENAME).print.pdf

clean-all-pdf : clean-screen-pdf clean-print-pdf

clean-swf :
	rm -vf ./swf/$(BASENAME).screen.swf

purge-html : purge-dvi
	rm -vf $(BASENAME).idv $(BASENAME).lg $(BASENAME).tmp $(BASENAME).xref $(BASENAME).4ct $(BASENAME).4tc $(BASENAME).html $(BASENAME)*.html $(BASENAME)*.css *.4og

clean-html-onepage : purge-html
	rm -vf html.tex4ht.dir/*

clean-html-splitpageone : purge-html
	rm -vf html.tex4ht.split1.dir/*

clean-html-splitpagetwo : purge-html
	rm -vf html.tex4ht.split2.dir/*

clean-html-splitpagethree : purge-html
	rm -vf html.tex4ht.split3.dir/*

clean-html : clean-html-onepage clean-html-splitpageone clean-html-splitpagetwo clean-html-splitpagethree

#clean-html : clean-html-onepage clean-html-splitpageone clean-html-splitpagetwo

purge-hnb :
	rm -vf gradu.opml gradu.html gradu.css.html gradu.txt

purge-emacs :
	rm -vf *~

purge : purge-dvi purge-screen-pdf purge-print-pdf purge-html purge-emacs

clean : clean-dvi clean-ps clean-all-pdf clean-swf clean-html purge-hnb purge-emacs
