#!/bin/sh

# Where docbook.xsl resides on your system (change this variable accordingly).
# On Debian Jessie, it is installed by package docbook-xsl.
xsldir="/usr/share/xml/docbook/stylesheet/docbook-xsl/xhtml"

if [ ! -f "$xsldir/docbook.xsl" ] ; then
	echo "No docbook.xsl found in $xsldir"
	exit 1
fi

# CSS to use to render the HTML documents.
# To use the local stylesheet (i.e. without accessing the web server each time
# the generated html is reloaded), use the "gazette.css" value.
# The html page that is copied to the web server must have been generated with
# the full URL to the stylesheet residing on the server.
#css="gazette.css"
css="http://ftp.traduc.org/projets/gazette-linux/outils-docbook/gazette.css"

# Transformation stylesheet to use to convert the document.
style="gazette.xsl"

if [ ! -f "$style" ] ; then
	echo "The transformation stylesheet \"$style\" was not found"
	exit 1
fi

# Get the DocBook file to convert.
if [ -z $1 ] ; then
	echo "Usage: $0 lgXXX-Y.xml"
	echo "Produce a file named lgXXX-Y.html where XXX is the gazette issue number and Y is the article identification letter."
	echo "The file passed as argument must be a DocBook document. It is validated by this command before the html is generated."
	exit 1
fi

source=$1
if [ ! -f "$source" ] ; then
	echo "File \"$source\" not found"
	exit 1
fi

target="${source%.xml}.html"

ARGS="--path $xsldir"
ARGS="$ARGS --stringparam html.stylesheet $css"

if ( ! xmllint --nonet --valid --noout "$source" ) ; then
	# There is an error in $source
	exit 1
fi

if ( ! xsltproc $ARGS "$style" "$source" > "$target" ) ; then
	exit 1
fi

exit 0
