#
# PostGIS documentation build Makefile
#
# Written by Mark Cave-Ayland
#
# NOTE: We don't use a standard PGXS Makefile here since the DOCS target
# only allows files to be stored within contrib/ and we currently
# store documentation under contrib/postgis due to the possibility
# that we could produce a large number of files (think chunked HTML)
#

POSTGIS_MAJOR_VERSION=@POSTGIS_MAJOR_VERSION@
POSTGIS_MINOR_VERSION=@POSTGIS_MINOR_VERSION@
POSTGIS_MICRO_VERSION=@POSTGIS_MICRO_VERSION@

XSLTPROC=@XSLTPROC@
XSLBASE=@XSLBASE@

XSLTPROC_COMMONOPTS= \
	--param section.autolabel 1 \
	--param section.label.includes.component.label 1 \
	--param chunk.section.depth 0 \
	--param generate.section.toc.level 1 \
	--param funcsynopsis.style ansi \
	--param admon.graphics 1 \
	--param admon.textlabel 0 \
	--param simplesect.in.toc 0 \
	--param use.id.as.filename 1 \
	--param chunk.quietly 1

XSLTPROC_HTMLOPTS= \
	--stringparam html.stylesheet style.css \

HTML_DOCBOOK_XSL=$(XSLBASE)/html/docbook.xsl
CHUNK_HTML_DOCBOOK_XSL=$(XSLBASE)/html/chunk.xsl

# DBLatex's dblatex script for PDF generation from DocBook
DBLATEX=@DBLATEX@

# Imagemagick's convert utility program for generated images used in the documentation
IMAGEMAGICK=@IMAGEMAGICK@

# Directories for postgresql subdirectories
PGSQL_DOCDIR=@PGSQL_DOCDIR@
PGSQL_MANDIR=@PGSQL_MANDIR@
PGSQL_SHAREDIR=@PGSQL_SHAREDIR@

# If XSLTPROC or XSLBASE were not found during configure, we cannot
# build the documentation
ifeq ($(XSLTPROC),)
all: requirements_not_met_xsltproc
else
ifeq ($(XSLBASE),)
all: requirements_not_met_xslbase
else
ifeq ($(IMAGEMAGICK),)
all: requirements_not_met_imagemagick
else
all: images html/postgis.html postgis_comments.sql
endif
endif
endif

postgis_aggs_mm.xml: ./xsl/postgis_aggs_mm.xml.xsl postgis.xml
	$(XSLTPROC) ./xsl/postgis_aggs_mm.xml.xsl postgis.xml > $@

postgis_comments.sql: ./xsl/postgis_comments.sql.xsl postgis.xml postgis_aggs_mm.xml
	$(XSLTPROC) ./xsl/postgis_comments.sql.xsl postgis.xml > $@

postgis-out.xml: postgis.xml introduction.xml installation.xml faq.xml using_postgis_dataman.xml  using_postgis_app.xml performance_tips.xml reference.xml reference_management.xml reference_constructor.xml reference_accessor.xml reference_editor.xml reference_output.xml reference_operator.xml reference_measure.xml reference_processing.xml reference_lrs.xml reference_transaction.xml reference_misc.xml reference_exception.xml postgis_aggs_mm.xml reporting.xml release_notes.xml ../Version.config
	cat $< | sed "s/@@LAST_RELEASE_VERSION@@/${POSTGIS_MAJOR_VERSION}.${POSTGIS_MINOR_VERSION}.${POSTGIS_MICRO_VERSION}/g" > $@

chunked-html: postgis-out.xml images
	$(XSLTPROC) $(XSLTPROC_COMMONOPTS) $(XSLTPROC_HTMLOPTS) \
		--output html/ \
		--stringparam saxon.character.representation decimal \
		$(CHUNK_HTML_DOCBOOK_XSL) \
		$<

html/postgis.html: postgis-out.xml images
	$(XSLTPROC) $(XSLTPROC_COMMONOPTS) $(XSLTPROC_HTMLOPTS) \
		--output html/postgis.html \
		$(HTML_DOCBOOK_XSL) \
		$<


postgis-${POSTGIS_MAJOR_VERSION}.${POSTGIS_MINOR_VERSION}.${POSTGIS_MICRO_VERSION}.pdf: postgis-out.xml images
	@if test x"$(DBLATEX)" = x; then \
	   echo "Error: dblatex not found, can't build pdf"; \
	   echo "       try installing dblatex and then re-run configure"; \
	   false; \
	else \
		dblatex -T native -t pdf \
		   -I "${PWD}/html" \
		   -P doc.collab.show=0 \
		   -P figure.note="${PWD}/html/images/note" \
		   -P figure.tip="${PWD}/html/images/tip" \
		   -P figure.important="${PWD}/html/images/important" \
		   -P figure.warning="${PWD}/html/images/warning" \
		   -P figure.caution="${PWD}/html/images/caution" \
		   -P latex.output.revhistory=0 \
		   -o postgis-${POSTGIS_MAJOR_VERSION}.${POSTGIS_MINOR_VERSION}.${POSTGIS_MICRO_VERSION}.pdf $<; \
	fi

pdf: postgis-${POSTGIS_MAJOR_VERSION}.${POSTGIS_MINOR_VERSION}.${POSTGIS_MICRO_VERSION}.pdf

doxygen.cfg: doxygen.cfg.in
	cat $< | sed "s/@@LAST_RELEASE_VERSION@@/${POSTGIS_MAJOR_VERSION}.${POSTGIS_MINOR_VERSION}.${POSTGIS_MICRO_VERSION}/g" > $@

doxygen: doxygen.cfg
	doxygen $<

images: 
	$(MAKE) -C html/image_src images
	
clean:
	$(MAKE) -C html/image_src clean
	@rm -f postgis-out.xml postgis_aggs_mm.xml

maintainer-clean: clean
	$(MAKE) -C html/image_src clean
	@rm -f html/*.html \
		postgis-${POSTGIS_MAJOR_VERSION}.${POSTGIS_MINOR_VERSION}.${POSTGIS_MICRO_VERSION}.pdf

comments: postgis_comments.sql

comments-install: comments
	$(MAKE) -f Makefile.comments install

comments-uninstall:
	$(MAKE) -f Makefile.comments uninstall

install: html/postgis.html man/shp2pgsql.1 man/pgsql2shp.1
	@mkdir -p $(DESTDIR)$(PGSQL_DOCDIR)/postgis
	@mkdir -p $(DESTDIR)$(PGSQL_MANDIR)/man1
	cp html/postgis.html $(DESTDIR)$(PGSQL_DOCDIR)/postgis/postgis.html
	cp ../README.postgis $(DESTDIR)$(PGSQL_DOCDIR)/postgis/README.postgis
	cp man/pgsql2shp.1 $(DESTDIR)$(PGSQL_MANDIR)/man1/pgsql2shp.1
	cp man/shp2pgsql.1 $(DESTDIR)$(PGSQL_MANDIR)/man1/shp2pgsql.1

uninstall:
	rm -f $(DESTDIR)$(PGSQL_DOCDIR)/postgis/postgis.html
	rm -f $(DESTDIR)$(PGSQL_DOCDIR)/postgis/README.postgis
	rm -f $(DESTDIR)$(PGSQL_MANDIR)/man1/shp2pgsql.1
	rm -f $(DESTDIR)$(PGSQL_MANDIR)/man1/pgsql2shp.1

requirements_not_met_xsltproc:
	@echo
	@echo "configure was unable to find 'xsltproc' which is required to build the documentation."
	@echo "To build the documentation, install xsltproc and then re-run configure. Alternatively "
	@echo "refer to online manual:"
	@echo
	@echo "  http://postgis.refractions.net/docs"
	@echo

requirements_not_met_xslbase:
	@echo
	@echo "configure was unable to find the Docbook XSL stylesheet directory which is required to build the documentation."
	@echo "To build the documentation, install the Docbook XSL stylesheets and/or re-run configure with the --with-xsldir option. Alternatively "
	@echo "refer to online manual:"
	@echo
	@echo "  http://postgis.refractions.net/docs"
	@echo

requirements_not_met_imagemagick:
	@echo 
	@echo "configure was unable to find the ImageMagick's 'convert' utility program."
	@echo "To build the documentation, install ImageMagick and then re-run configure. Alternatively "
	@echo "refer to online manual:"
	@echo
	@echo "  http://postgis.refractions.net/docs"
	@echo 
	
.PHONY: html

