#
# 	Copyright (c) 2007 Peter V. Saveliev
#
# 	This file is part of Connexion docs module.
#
# 	Connexion is free software; you can redistribute it and/or modify
# 	it under the terms of the GNU General Public License as published by
# 	the Free Software Foundation; either version 3 of the License, or
# 	(at your option) any later version.
#
# 	Connexion is distributed in the hope that it will be useful,
# 	but WITHOUT ANY WARRANTY; without even the implied warranty of
# 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# 	GNU General Public License for more details.
#
# 	You should have received a copy of the GNU General Public License
# 	along with Connexion; if not, write to the Free Software
# 	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

name=connexion-docs
version=$(shell realpath .. | sed 's|.*/||')
docdir ?= /usr/share/doc/${name}

all: html

html:
#
#	1. fetch the list of DocBook documents
#	2. create index.xml
#	3. convert all to html
#	4. remove index.xml
#
#	we can not use a variable to process index.xml with toc off, cause gmake
#	evaluates \" strings with bugs :|
#
	@docs=`ls -1 *xml | sed s/.xml//`;                                                             \
	cat _index.xml | sed '/----/ {Q}' >index.xml;                                                  \
	for i in $$docs; do {                                                                          \
		[ "$$i" = "schemas" ] && continue;                                                     \
		[ "$$i" = "index" ] && continue;                                                       \
		[ "$$i" = "_index" ] && continue;                                                      \
		a=`cat $$i.xml | sed -n '/title/ {s/^.*>\([^<]\+\).*$$/\1/;p;q}'`;                     \
		echo "<listitem><para>" >>index.xml;                                                   \
		echo "<ulink url='./$$i.html'>$$a</ulink>" >>index.xml;                                \
		echo "</para></listitem>" >>index.xml;                                                 \
	} done;                                                                                        \
	cat _index.xml | sed -n '/----/ {s/.*/ /g;bala}; :ala {//,$$p}' >>index.xml;                   \
	for i in $$docs; do {                                                                          \
		echo $$i;                                                                              \
		[ "$$i" = "schemas" ] && continue;                                                     \
		[ "$$i" = "_index" ] && i="index";                                                     \
		cat $$i.xml |                                                                          \
		if [ "$$i" = "index" ]; then {                                                         \
			xsltproc                                                                       \
			--stringparam html.stylesheet   '../default.css'                                  \
			--stringparam generate.toc "article nop"                                       \
			--param id.warnings 0                                                          \
			--nonet /usr/share/xml/docbook/xsl-stylesheets/html/docbook.xsl - >$$i.html ;  \
		} else {                                                                               \
			xsltproc                                                                       \
			--stringparam html.stylesheet   '../default.css'                                  \
			--param       toc.section.depth 3                                              \
			--param       id.warnings 0                                                    \
			--stringparam generate.toc "article toc,title,example"                                \
			--nonet /usr/share/xml/docbook/xsl-stylesheets/html/docbook.xsl - >$$i.html ;  \
		} fi;                                                                                  \
	} done;                                                                                        \
	rm -f index.xml

clean:
	rm -f *.html

