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

aspect-files      = $(shell echo ../include/aspect/*.h \
                            ../include/aspect/*/*.h     \
                            ../include/aspect/*/*/*.h   \
                            modules/*.h)
aspect-manual-pix = $(shell find -L 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 '.*')

# Find all parameter files to be included in the manual and generate their
# output targets (.prm.out). There are two types of targets: Every file inside
# the /doc/ folders generates the output inside the same folder. Every file
# outside the /doc/ folder generates a target inside the doc folder. For
# that we separate file and directory strings, attach 'doc/' to the directory
# and '.out' to the file name and join them afterwards.
aspect-doc-prms = $(shell find -L manual/cookbooks/ -path \*/doc/\* -name \*.prm 2>/dev/null)
aspect-doc-prms-out = $(aspect-doc-prms:=.out)

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

all: aspect.tag

# 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.
#
# There are two rules for .prm.out files, the first one is executed if the
# corresponding .prm file is found in the same folder as the .prm.out target.
# This is the case for all *.part.prm files that are already in the doc/
# subfolder of cookbooks and benchmarks. The second rule is triggered if the
# corresponding .prm file is found in the parent folder of the .prm.out file
# (in this case the first rule does not trigger, because the dependency is
# not found). The second case applies to the full .prm files of the cookbook or
# benchmark, for which the .prm file lies in the parent folder of the
# doc/*.prm.out target folder.
%.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

%.prm.out: $(dir %.prm)../$(notdir %.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 https://www.dealii.org/developer/doxygen/deal.tag ; then \
	  echo 'Cross-linking deal.II documentation from the ASPECT documentation' ; \
	  echo 'TAGFILES = deal.tag=https://www.dealii.org/developer/doxygen/deal.II'          >> aspect.dox ; \
	 fi
	@echo 'IMAGE_PATH = logo 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."

clean:
	-rm -f aspect.dox aspect.tag
	-rm -rf doxygen
