PYTHON = python

PYDIR=$(shell $(PYTHON) -c 'import os; print os.path.dirname(os.__file__)')

DOC_ROOT = $(PYDIR)/doc
MKHOWTO = $(DOC_ROOT)/tools/mkhowto
ICONS = $(DOC_ROOT)/html/icons

.PHONY: html

PAPER = a4

DOCFILES = albatross.tex copyright.tex installation.tex introduction.tex \
	tempuser.tex tags.tex customtags.tex mixins.tex packaged.tex

METHODFILES = SimpleContext-methods.tex AppContext-methods.tex \
	SimpleAppContext-methods.tex SessionAppContext-methods.tex \
	SessionFileAppContext-methods.tex Application-methods.tex \
	SimpleApp-methods.tex SimpleSessionApp-methods.tex \
	SimpleSessionFileApp-methods.tex ModularApp-methods.tex \
	ModularSessionApp-methods.tex ModularSessionFileApp-methods.tex \
	RandomModularApp-methods.tex RandomModularSessionApp-methods.tex \
	RandomModularSessionFileApp-methods.tex \
	BranchingSessionContext-methods.tex

FIGURES = simplecontext.pdf simpleappcontext.pdf simpleapp.pdf \
	twolayer.pdf twolayerctx.pdf mvc.pdf albmvc.pdf pagemap.pdf \
	toolkit.pdf appcontext.pdf application.pdf sessionappcontext.pdf \
	sessionfileappcontext.pdf simplesessapp.pdf simplesessfileapp.pdf \
	modularapp.pdf modularsessapp.pdf modularsessfileapp.pdf \
	dataflow.pdf AlbatrossObjects.pdf randmodapp.pdf randmodsessapp.pdf \
	randmodsessfileapp.pdf branchingsessioncontext.pdf

ALLFILES = $(DOCFILES) $(METHODFILES) $(FIGURES)

all:	pdf

test:
	PYTHONPATH=.. $(PYTHON) test_examples.py

pdf:	$(ALLFILES) version
	$(MKHOWTO) --pdf --$(PAPER) albatross.tex

html:	$(ALLFILES) version
	$(MKHOWTO) --image-type png --html albatross.tex
	mkdir -p albatross/icons
	cp $(ICONS)/* albatross/icons/
# the iconserver option of mkhowto is broken since it writes
# it to the end if the init_file where they aren't useful anymore,
# so we work around it:
	for f in `find albatross -type f`; do \
	  cat $$f | sed s/\.\.\\/icons/icons/g > $${f}2; \
	  mv $${f}2 $$f; \
	done
	-rm albatross/albatross2
	-rm albatross/icons/icons2
	-rm -f albatross/images.*
	-rm -f albatross/*.old

world:	pdf html

pdf-$(PAPER).tar.gz: pdf
	tar cf - *.pdf | gzip -9 >$@

html.tar.gz:	html
	tar cf - albatross | gzip -9 >$@

# build method files
$(METHODFILES):
	$(PYTHON) doc_methods.py -o $@ albatross.$(subst -methods.tex,,$@)

# convert .obj images to .eps and then to .pdf
# Some versions of dia require --export-to-format=eps-builtin, rather than
# --filter=eps-builtin.
%.pdf: %.dia
	dia --nosplash --filter=eps-builtin \
		--export=$(subst .dia,.eps,$<) $<
	epstopdf $(subst .dia,.eps,$<)

clean:
	rm -f *~ *.aux *.idx *.ilg *.ind *.log *.toc *.bkm *.syn \
	*.pla *.eps *.pdf *.lof *.l2h *.tex2 *.html *.pyc $(METHODFILES)

# Version substitution
version: ../albatross/__init__.py
	DATE="`date +'%B %d, %Y'`"; \
	VERSION="`awk '/__version__/ {print $$3}' ../albatross/__init__.py`"; \
	VERSION="`echo $$VERSION | sed s/\\'//g`"; \
	cat albatross.tex | sed s/\\release.*/\\release\{$$VERSION\}/ >albatross.tex2; \
	cat albatross.tex2 | sed s/\\date.*/\\date\{"$$DATE"\}/ >albatross.tex
