#!/bin/bash
#
# Script for multiplexing between different docbook tools for
# generating a specified format.
#
# It seems only realistic to try the various wrapper programs.
#
# $Id: make-manual,v 1.3 2002/12/30 15:57:01 jonas Exp $
#

format=${1}
target=${2}
doctools=${3}
doctool=""
options=""
htmldir="`basename ${target} .xml`.html"

case ${format} in
	html)
	options="-o ${htmldir}"
	rm -rf ${htmldir}
	;;
	ps | pdf | txt )
	# Fall through
	;;
	*)
	echo "Usage: `basename $0` [format] [target] [doctool]"
	echo "  format	- which format to generate (dvi, ps, html, etc.)"
	echo "  target	- xml file to process"
	echo "  doctool	- which tool to process with (xmlto, db, docbook)"
	exit -1
	;;
esac

if [[ ${doctools} == "" ]];
then
	doctools="docbook2${format} db2${format} jw xmlto"
fi

echo "Checking for processing tools"

for doctool in ${doctools};
do
	if [[ "`which ${doctool}`" != "" ]];
	then
		break
	fi
done

case ${doctool} in
	xmlto)
	options="${format} ${options}"
	;;
	jw)
	options="--backend ${format} ${options}"
	;;
	docbook2* | db2*)
	# Fall through
	;;
	*)
	echo No docbook processing tools found
	exit -1
	;;
esac

echo "Using ${doctool} for generating ${format} format"

${doctool} ${options} ${target}

if [[ ${format} == "html" ]];
then
	tidy=`which tidy`
	if [[ ${tidy} != "" && -e ${htmldir} ]];
	then
		scripts/installcss ${htmldir}
		cd ${htmldir}
		${tidy} -latin1 -f err -im *.html
		rm -f err
		cd ..
	fi
fi
