#!/bin/ksh

if [ $# -lt 1 ]; then
	echo "Usage: $0 cfg [doc] ..."
	exit 1
fi
if [ -z "$sdir" ]; then
	sdir=`dirname $0`
fi
sdir=`cd $sdir; pwd`
cfg=$1
shift
docs="$@"
config="$sdir/cfg/$cfg"
if [ ! -e "$config" ]; then
	echo "Configuration not found: $config"
	exit 1
fi
. "$config"

cd $sdir
XSLTPROC_PARAMS="--xinclude --stringparam section.autolabel 1 --stringparam section.label.includes.component.label 1 --stringparam toc.section.depth 3"
DOCS="Overview UtilitiesManual"

if [ -z "$docs" ]; then
	docs=`cd docbook; ls -d *; cd ..`
fi

verbose() {
	echo "$ $@"
	"$@"
}

for doc in $docs; do
	cd "$sdir"
	mkdir -p "html/$doc"
	echo "Generating single file HTML: $doc"
	verbose xsltproc $XSLTPROC_PARAMS --output "html/$doc/$doc.html" "${docbook}/html/docbook.xsl" "docbook/$doc/$doc.xml"
	echo "Generating multiple file HTML: $doc"
	cd "$sdir/html/$doc"
	verbose xsltproc $XSLTPROC_PARAMS --stringparam use.id.as.filename 1 --stringparam chunk.section.depth 0 "${docbook}/html/chunk.xsl" "$sdir/docbook/$doc/$doc.xml"
	cd "$sdir"
	mkdir -p "pdf"
	echo "Generating PDF: $doc"
	verbose xsltproc $XSLTPROC_PARAMS --output "pdf/$doc.fo" "${docbook}/fo/docbook.xsl" "docbook/$doc/$doc.xml"
	cd "$fop/build"
	verbose java -jar "$fop/build/fop.jar" "$sdir/pdf/$doc.fo" "$sdir/pdf/$doc.pdf"
done
