#!/bin/bash
# dbxml - creates formatted output from XML documents
# code was tested on WinNT/Cygwin but should run on Linux/Unix as well
# Markus Hoenicka <hoenicka_markus@compuserve.com> 2001-10-10
# $Id: dbxml,v 1.1.1.1 2005/01/26 20:24:04 markus Exp $
# OPTIONS: -d (stylesheet) -h (invoke help), -p (processor), -t (output format)
# relies on these external programs: SUN JRE, xerces/xalan, xp/xt, xsltproc, passivetex, xmltex

### start user-customizable section
# stylesheets; you can define additional driver files here
htmldb="/usr/local/lib/xml/stylesheets/docbook-xsl/html/docbook.xsl"
fodb="/usr/local/lib/xml/stylesheets/docbook-xsl/fo/docbook.xsl"

# the default processor: xalan, xt, saxon, or xsltproc
processor="xalan"

# the path to the Java class repository. This assumes that all necessary .jar
# files are in this directory
classpath_root="/cygdrive/d/Win32App/java"

### end user-customizable section

# some defaults
stylesheet="d"
outformat="html"

# function definitions
# creates fo output from xml. Arguments: input_filename out_filename
process_fo () {
    case $processor in
	xalan    ) java -cp "$classpath" org.apache.xalan.xslt.Process -in $1 -xsl $jfosheet -out $2;;
	xt       ) java -cp "$classpath" com.jclark.xsl.sax.Driver $1 $jfosheet > $2;;
	saxon    ) java -cp "$classpath" com.icl.saxon.StyleSheet $1 $jfosheet -o $2;;
	xsltproc ) xsltproc $fosheet $1 > $2;;
    esac
}

# creates html output from xml. Arguments: input_filename out_filename
process_html () {
    case $processor in
	xalan    ) java -cp "$classpath" org.apache.xalan.xslt.Process -in $1 -xsl $jhtmlsheet -out $2;;
	xt       ) java -cp "$classpath" com.jclark.xsl.sax.Driver $1 $jhtmlsheet > $2;;
	saxon    ) java -cp "$classpath" com.icl.saxon.StyleSheet $1 $jhtmlsheet -o $2;;
	xsltproc ) xsltproc $htmlsheet $1 > $2;;
    esac
}

# read the command line options
while getopts ":d:hi:p:t:" opt; do
  case $opt in
    d  ) stylesheet=$OPTARG;;
    h  ) echo "creates formatted output from a DocBook XML source"
	 echo 'usage: dbxml [-d style] [-h] [-i name] [-p prefix] [-t outformat] file1 [file2...]'
	 echo "Options: -d select stylesheet (d=default, m=m4, s=dbslide)"
	 echo "         -h print this help and exit"
	 echo "         -p specify processor: xalan, xt, saxon, or xsltproc"
	 echo "         -t select the output format. Possible values are html, rtf, pdf."
	 exit 0 ;;
    p  ) processor=$OPTARG;;
    t  ) outformat=$OPTARG;;
    \? ) echo 'usage: dbxml [-d style] [-h] [-p processor] [-t outformat] file1 [file2...]'
	 echo 'type dbxml -h to invoke help'
	 exit 1;;
  esac
done

# correct the index so the filename argument is always $1
shift $(($OPTIND - 1))

# pick stylesheet; the case 'm' shows how you could include your own driver files here
case $stylesheet in
  d  ) htmlsheet=$htmldb
       fosheet=$fodb;;
#  m  ) htmlsheet=$htmlm4
#       fosheet=$printm4;;
  \? ) echo 'usage of the -d switch: d selects stock DocBook'
       exit 1;;
esac

# test for valid arguments
if [ ! $outformat = html ] && [ ! $outformat = rtf ] && [ ! $outformat = pdf ]; then
  echo "specify one of 'html', 'rtf', 'pdf' with the -t option"
  exit 1
fi

if [ ! $processor = "xalan" ] && [ ! $processor = "xt" ] && [ ! $processor = "xsltproc" ] && [ ! $processor = "saxon" ]; then
  echo "specify one of 'xalan', 'xt', 'saxon', 'xsltproc' with the -p option"
  exit 1;
fi

# on Win32-cygwin, the native Win32 tools want the DOS path
if [ $OSTYPE = "cygwin" ]; then
    # get the full dos path of the classpath root
    osclasspath_root=$(cygpath -w $classpath_root)
    classpath="$osclasspath_root\avalon-framework-4.0.jar;$osclasspath_root\batik.jar;$osclasspath_root\fop.jar;$osclasspath_root\jfor-0.5.1.jar;$osclasspath_root\jimi-1.0.jar;$osclasspath_root\logkit-1.0b4.jar;$osclasspath_root\sax.jar;$osclasspath_root\xalan.jar;$osclasspath_root\xerces.jar;$osclasspath_root\xp.jar;$osclasspath_root\xt.jar"
    jfosheet=$(cygpath -w $fosheet)
    jhtmlsheet=$(cygpath -w $htmlsheet)
else
    classpath="$classpath_root/avalon-framework-4.0.jar:$classpath_root/batik.jar:$classpath_root/fop.jar:$classpath_root/jfor-0.5.1.jar:$classpath_root/jimi-1.0.jar:$classpath_root/logkit-1.0b4.jar:$classpath_root/sax.jar:$classpath_root/xalan.jar:$classpath_root/xerces.jar:$classpath_root/xp.jar:$classpath_root/xt.jar"
    jfosheet=$fosheet
    jhtmlsheet=$htmlsheet
fi

for filename in $*; do
    if [ $OSTYPE = "cygwin" ]; then
      # get the full dos path of the file
      mypath=$(cygpath -w $filename)
    else
      mypath=$filename
    fi

    # extract the basename from the argument
    basename=${mypath%.*}

    case $outformat in
	rtf )   java -cp "$classpath" ch.codeconsult.jfor.main.CmdLineConverter $basename.fo $basename.rtf;;
	html)   process_html $mypath $basename.html;;
	pdf )   process_fo $mypath $basename.fo
		if [ $? -ne 0 ]; then
		  exit 1
		fi
		if [ ! -e $basename.aux ]; then
		  touch $basename.aux
		fi
		cp $basename.aux $basename.aux.$$
		pdfxmltex $basename.fo
		if [ $? -ne 0 ]; then
		  exit 1
		fi
		until diff --brief $basename.aux $basename.aux.$$; do
		  cp $basename.aux $basename.aux.$$
		  pdfxmltex $basename.fo
		done
		rm $basename.aux.$$;;
    esac
done


exit 0
