# Makefile for creating readable docs from the xml files.  For this to
# work, the current directory needs to have a symbolic link called
# 'bin' that points to the directory where the executable lives and a
# link called 'oof2dir' pointing to the main OOF2 directory.

# Build in a temp directory, so that the existing files aren't wiped
# out until the last minute.

XMLFILES = CH_graphics.xml CH_tasks.xml CH_windows.xml SN_skel.xml     \
man_oof2.xml oof2_api.xml CH_overview.xml CH_concepts.xml SN_micro.xml

DVI2BITMAP = dvi2bitmap --magnification=4 --scale=4 --output-type=gif

TMPDIR = tmpdir

publish: $(TMPDIR) saxonize.web texify figs copy_css
	-mkdir html
	rm -rf html/*
	CTCMS/ctcmsWeb.py --from=$(TMPDIR) --to=html --template=CTCMS/template.html --exclude=.tex,.dvi,.aux,.log
	touch publish

oof2man.tgz: $(TMPDIR) saxonize.ext texify figs copy_css
	-mkdir oof2man
	CTCMS/ctcmsWeb.py --from=$(TMPDIR) --to=oof2man --template=exporttemplate.html --exclude=.tex,.dvi,.aux,.log
	tar -czf oof2man.tgz oof2man
	-rm -rf oof2man

saxonize.web: $(XMLFILES)
	mkdir $(TMPDIR)/equations
	(cd $(TMPDIR); rm -f *.html; java -jar ../xsl/java/saxon.jar ../man_oof2.xml ../xsl/oofchunk.xsl nist.exit.script=1)

saxonize.ext: $(XMLFILES)
	(cd $(TMPDIR); rm -f *.html; java -jar ../xsl/java/saxon.jar ../man_oof2.xml ../xsl/oofchunk.xsl)

texify:
	(cd $(TMPDIR); latex tex-math-inlines.tex && $(DVI2BITMAP) --verbose=quiet --query=bitmaps tex-math-inlines | awk '{printf "img[src=\"%s\"] {margin-bottom:%dpx;}\n",$$2,$$6-$$4}' > inline.css)
	(cd $(TMPDIR); latex tex-math-equations.tex && $(DVI2BITMAP) tex-math-equations)

copy_css:
	cp -f -r CTCMS $(TMPDIR)
	cp -f templates/*.html $(TMPDIR)
	cp -f oof2.css $(TMPDIR)

oof2_api.xml: always
	(cd oof2dir; ./oof2-build dist)
	bin/oof2 --script xmldump.py --batch --debug
	sed s/Graphics_1/Graphics_n/g oof2_api.xml > tmp
	mv -f tmp oof2_api.xml

$(TMPDIR): always
	-rm -rf $(TMPDIR)
	mkdir $(TMPDIR)

figs: $(TMPDIR)
	-mkdir $(TMPDIR)/FIGURES
	-mkdir $(TMPDIR)/IMAGES
	rsync -r -C --delete --delete-excluded --exclude "*.eps" --exclude "*.graffle" FIGURES $(TMPDIR)/
	rsync -r -C --delete --delete-excluded --exclude "*.eps" --exclude "*.graffle" IMAGES $(TMPDIR)/

always:

# The "equations" target is used when it's necessary to re-run latex
# by hand.  This is useful when debugging the equation formatting by
# editing tex-math-*.tex.  Remember that changes in tex-math-*.tex are
# not permanent.  They'll be overwritten the next time saxon is run.
equations:
	(cd html; latex tex-math-inlines.tex && $(DVI2BITMAP) --verbose=quiet --query=bitmaps tex-math-inlines | awk '{printf "img[src=\"%s\"] {margin-bottom:%dpx;}\n",$$2,$$6-$$4}' > inline.css)
	(cd html; latex tex-math-equations.tex && $(DVI2BITMAP) tex-math-equations)


#pdf: $(XMLFILES)
#	#docbook2pdf -l /sw/share/sgml/dsssl/docbook-dsssl-nwalsh/dtds/decls/xml.dcl man_oof2.xml 
#	xsltproc -o man_oof2.fo --stringparam fop.extensions 1 /sw/share/xml/xsl/docbook-xsl/fo/docbook.xsl man_oof2.xml
#	fop man_oof2.fo -pdf man_oof2.pdf

#rtf: $(XMLFILES)
#	docbook2rtf -l dtd/xml.dcl man_oof2.xml 

test.html: test.xml
	docbook2html  -l dtd/xml.dcl test.xml

test2.html: test.xml
	java -jar /sw/share/java/saxon/saxon.jar -o test2.html test.xml /sw/share/xml/xsl/docbook-xsl/html/docbook.xsl

sandbox.html: sandbox.xml oof2.css
	-mkdir sandbox
	(cd sandbox; rm -f *.html; java -jar ../xsl/java/saxon.jar ../sandbox.xml ../xsl/oofchunk.xsl)
	cp -f oof2.css sandbox
	cp -f templates/*.html sandbox
	/Library/WebServer/Documents/CSS/ctcmsWeb.py --from=sandbox --to=/Users/langer/Sites/sandbox --machine=localhost
	touch sandbox.html

sandboxtex.html: sandboxtex.xml oof2.css
	-mkdir sandboxtex
	(cd sandboxtex; rm -f *.html; java -jar ../xsl/java/saxon.jar ../sandboxtex.xml ../xsl/oofchunk.xsl)
	(cd sandboxtex; latex tex-math-inlines.tex && $(DVI2BITMAP) --verbose=quiet --query=bitmaps tex-math-inlines | awk '{printf "img[src=\"%s\"] {margin-bottom:%dpx;}\n",$$2,$$6-$$4}' > inline.css)
	(cd sandboxtex; latex tex-math-equations.tex && $(DVI2BITMAP) tex-math-equations)
	cp -f oof2.css sandboxtex
	cp -f templates/*.html sandboxtex
	/Library/WebServer/Documents/CSS/ctcmsWeb.py --from=sandboxtex --to=/Users/langer/Sites/sandboxtex --machine=localhost --exclude=.tex,.dvi,.log,.aux
	touch sandboxtex.html
