############################################################
# Makefile for building documentation
############################################################

aspect-files      = $(shell echo ../include/aspect/*.h \
                            ../include/aspect/*/*.h     \
                            ../include/aspect/*/*/*.h   \
                            modules/*.h)
aspect-manual-pix = $(shell find manual/cookbooks/ -name \*.png 2>/dev/null)

# List all changelog files including the relative path and make sure to ignore
# the .gitkeep file. Use find instead of ls because the directory might have
# no changes in it:
change-files = $(shell find modules/changes/ -type f -not -name '.*')

aspect-prms = $(shell find manual/cookbooks/ -name \*.prm 2>/dev/null)
aspect-prms-out = $(aspect-prms:=.out)

default:
	@echo "make targets:"
	@echo "  aspect.tag  - generates doxygen documentation into doxygen/"
	@echo "  manual.pdf  - generates manual.pdf"
	@echo "  all         - generates both"

all: aspect.tag manual.pdf

# Generate .prm.out from .prm using the annotate.pl script and then process
# index entries to contain at most three levels (by replacing the fourth
# separator marker ! by /). This is repeated 10 times because only one nesting
# level is removed in each call to sed. The replacement is necessary as
# makeindex only allows for three levels of nesting.
%.prm.out: %.prm Makefile
	@echo "generating '$@' from '$<'..."
	@perl manual/cookbooks/annotate.pl $< >$@
	@for i in `seq 1 10`; do sed -i.bak 's/{\([^!]*\)!\([^!]*\)!\([^!]*\)!\([^}]*\)}/{\1!\2!\3\/\4}/' $@; rm $@.bak; done

# Generate changes.h from the bits in modules/changes/
modules/changes.h: $(change-files) modules/create_changes.sh modules/current_changes_*
	@cd modules && bash create_changes.sh

# build the documentation. see if we can download the latest
# deal.II deal.tag file generated by doxygen and if so
# use it to cross-reference the deal.II documentation from the
# aspect documentation
aspect.tag: $(aspect-files) $(change-files) modules/changes.h options.dox Makefile
	@if test ! -x "`which doxygen`" ; then \
	  echo "-----------------------------------------" ; \
	  echo "Can't find 'doxygen'." ; \
	  echo "-----------------------------------------" ; \
	  false ; \
	 fi
	@echo "=====aspect=================== Generating documentation"
	@echo '@INCLUDE = options.dox'                                > aspect.dox
	@echo 'DOT_IMAGE_FORMAT = png'                               >> aspect.dox
	@if wget -N http://www.dealii.org/developer/doxygen/deal.tag ; then \
	  echo 'Cross-linking deal.II documentation from the ASPECT documentation' ; \
	  echo 'TAGFILES = deal.tag=http://www.dealii.org/developer/doxygen/deal.II'          >> aspect.dox ; \
	 fi
	@echo 'IMAGE_PATH = manual'                                  >> aspect.dox
	@echo 'PROJECT_NAME = "ASPECT"'                              >> aspect.dox
	@echo 'INPUT = $(aspect-files)'                              >> aspect.dox
	@echo 'HTML_OUTPUT      = doxygen'                           >> aspect.dox
	@echo 'GENERATE_TAGFILE = $@'                                >> aspect.dox
	@doxygen aspect.dox || echo "Running doxygen to generate documentation failed."

# make target for generating the pdf manual. The goal is to have the return
# value of the sequence of bash commands reflect if building the manual was
# successful. For this matter we need to catch errors from pdflatex, but we
# need to ignore them the first time we run pdflatex, because of missing label
# definitions, etc..
manual.pdf: manual/manual.tex manual/manual.bib manual/parameters.tex \
            $(aspect-manual-pix) $(aspect-prms-out)
	@if test -x "`which pdflatex`" \
                 -a -x "`which bibtex`" \
                 -a -x "`which makeindex`" ; then \
	   (cd manual ; \
		rm -f manual.{aux,bbl,blg,log,out,pdf,toc} prm*.{idx,ilg,ind} ; \
		pdflatex --interaction=batchmode manual.tex;      \
		bibtex manual                                  && \
		makeindex prmindex                             && \
		pdflatex --interaction=batchmode manual.tex    && \
		pdflatex --interaction=batchmode manual.tex    && \
		pdflatex --interaction=batchmode manual.tex)   && \
	   mv manual/manual.pdf . || \
	   (echo "Error compiling manual.pdf, check manual/manual.log" && false); \
         else \
	  echo "------------------------------------------------------" ; \
	  echo "Can't find either 'pdflatex', 'bibtex' or 'makeindex'." ; \
	  echo "------------------------------------------------------" ; \
	  false ; \
	 fi

# This rule will only trigger if the file is missing, which happens if the
# user has a release tarball.
manual/manual.tex:
	@echo "------------------------------------------------------"
	@echo "Error: Can not generate manual.pdf without the source"
	@echo "files in manual/. If you are currently using a release"
	@echo "of ASPECT, download the latest development version of"
	@echo "ASPECT instead."
	@echo "------------------------------------------------------"
	@false

clean:
	-rm -f aspect.dox aspect.tag
	-rm -rf doxygen
	-rm -rf manual/manual.blg manual/manual.toc manual/manual.log manual/manual.out manual/manual.bbl manual/manual.aux
