#
# General makefile for AsciiDoc-based documents.  Please put specific
# customizations into a Makefile.local
#

# Where to keep our working files (autogenerated dependencies and
# temporary files)
DEPdir = .deps/
# Separator for anchor labels in included files
ANC=@
# How deep should the Table of Contents go?
TOC_DEPTH=2

KEEP_ARTIFACTS=$(system test `a2x --help | grep -- -k | wc -l` -eq 0 && echo -k)

BASE_OS:=$(shell uname | cut -d'-' -f1)
ifeq ($(BASE_OS),Darwin)
  SED = sed -E
else
  SED = sed -r
endif

ASCIIDOC_OPTS=--verbose  \
	-a toc -a docinfo -a numbered -a latexmath -a icons \
	-d book $(KEEP_ARTIFACTS)

A2X_OPTS=$(ASCIIDOC_OPTS) --no-xmllint \
	--dblatex-opts "$(DBLATEX_OPTS)" \
	--xsltproc-opts "$(XSLT_OPTS)"

DBLATEX_OPTS=-P latex.output.revhistory=0 \
	-P doc.collab.show=1 \
	-P toc.section.depth=$(TOC_DEPTH)

XSLT_OPTS=--stringparam toc.section.depth $(TOC_DEPTH) \
	--stringparam generate.section.toc.level 1

.PHONY: all pdf html epub
all: pdf html epub

#####################################################################
# Local customizations & special auto-generated targets:
ifneq ($(wildcard Makefile.local),)
  include Makefile.local
  MAKEDEPS=Makefile Makefile.local
else
  MAKEDEPS=Makefile
endif
#####################################################################

pdf:  $(patsubst %,%.pdf,$(TARGETS))
html: $(patsubst %,%.html,$(TARGETS))
epub: $(patsubst %,%.epub,$(TARGETS))

%.pdf: %.txt $(DEPdir)%.prebuild
	@echo
	@echo \#\#\# Building $@
	@echo
	export XML_CATALOG_FILES="catalog.xml"; \
	a2x $(A2X_OPTS) -f pdf $<

%.epub: %.txt $(DEPdir)%.prebuild
	@echo
	@echo \#\#\# Building $@
	@echo
	export XML_CATALOG_FILES="catalog.xml"; \
	a2x $(A2X_OPTS) -f epub $<

%.html: %.txt $(DEPdir)%.prebuild
	@echo
	@echo \#\#\# Building $@
	@echo
	export XML_CATALOG_FILES="catalog.xml"; \
	asciidoc $(ASCIIDOC_OPTS) -b html -f html.conf $<

.PHONY: clean
clean:
	@if [ ! -z $(DEPdir) ]; then \rm -frv $(DEPdir); fi
	@\rm -fv $(TARGETS:%=%.xml)
	@\rm -fv $(TARGETS:%=%.fo)

.PHONY: distclean
distclean: clean
	@\rm -fv $(TARGETS:%=%.pdf)
	@\rm -fv $(TARGETS:%=%.epub)
	@\rm -fv $(TARGETS:%=%.html)

# Include dependency makefiles, provided that they are needed
ifneq ($(strip $(TARGETS)),)
  include $(patsubst %,$(DEPdir)%.d,$(TARGETS))
endif

# A target (AsciiDoc output) file depends on:
#   0) the corresponding .txt file (handled by the implicit rule)
#   1) any files it imports through 'include::' or image(:|::)
#   2) (recursively) any dependencies for included .txt files
#   3) any autogenerated code fragments (excerpts of Python scripts)
$(DEPdir)%.d: %.txt $(MAKEDEPS)
	@echo Rebuilding dependencies: $<
	@if test ! -z `dirname $@`; then mkdir -p `dirname $@`; fi
	@echo "#" >> $@
	@echo "# source files for $<" > $@
	@echo "#" >> $@
	@echo `echo $@ | $(SED) 's/\.d$$/.prebuild/'` "" \\ >> $@
	@echo : $< \\ >> $@
	@for TAG in include:: image:: image:; do \
	 TEX=`egrep $$TAG $< | egrep -v '^\s*//' \
		| egrep -v $$TAG: \
		| $(SED) "s/.*$$TAG([^\[]*)\[.*/ \1 /g" \
		| $(SED) 's/ *(.*)\.txt/ $$(DEPdir)\1.prebuild/g' \
		| $(SED) 's/ *(.*)\.\{epspng\}/ \1.eps \1.png/g' \
		| $(SED) 's/ *(.*)\.\{epsjpg\}/ \1.eps \1.jpg/g'`;\
		echo $$TEX \\ >> $@; \
	 done
	@echo >> $@
	@echo "	touch "`echo $@ | $(SED) 's/\.d$$/.prebuild/'` >> $@
	@echo >> $@
	@echo "#" >> $@
	@echo "# dependencies" >> $@
	@echo "#" >> $@
	@TEX=`grep include:: "$<" | egrep -v '^\s*//' \
		| $(SED) 's/.*include::(.*)\[\].*/\1/g' \
		| grep '\.txt' \
		| $(SED) 's/ *(.*)\.txt/$$(DEPdir)\1.d/g'`; \
		for f in $$TEX; do echo "include $$f" >> $@; done
	@echo "#" >> $@
	@echo "# autogenerated code fragments" >> $@
	@echo "#" >> $@
	@TEX=`grep include:: "$<" | egrep -v '^\s*//' \
		| $(SED) 's/.*include::(.*)\[\].*/\1/g' \
		| egrep '.+\$(ANC).*\..+' | grep -v '\.txt'`;\
	 	for f in $$TEX; do \
			echo "$$f: $< " \\ >> $@; \
			echo $$f | $(SED) 's/\$(ANC)[^\$(ANC)]*(\.[^\.]+)/\1/' >> $@; \
			echo "	@python include_anchors.py -- $$f" >> $@; \
			echo >> $@; \
			echo clean: clean_$$f >> $@; \
			echo clean_$$f: >> $@ ; \
			echo "	@rm -fv $$f" >> $@; \
			echo >> $@; \
		done
