#!/bin/bash
#
# saxon9 script
# JPackage Project <http://www.jpackage.org/>

. /usr/share/java-utils/java-functions

# Base class for Saxon9:
MAIN_CLASS=net.sf.saxon.Transform

# Default catalog:
BASE_CATALOG="/etc/xml/catalog"

# Catalog verbosity
CATALOG_VERB=3

# All needed Saxon9 jar files
BASE_JARS="xml-commons-resolver saxon9.jar saxon9-dom.jar saxon9-sql.jar saxon9-s9api.jar saxon9-jdom.jar saxon9-xpath.jar saxon9-xom.jar saxon9-xqj.jar xml-commons-apis.jar jaxp_parser_impl.jar bea-stax-api.jar bea-stax-ri.jar"

# Flags for Java or Saxon
BASE_FLAGS=

# Optional jars; needed to separate them from BASE_JARS to avoid error
# messages printed on stdout
CLASSPATH="$CLASSPATH:"$(build-classpath docbook-xsl-saxon avalon-logkit xml-commons-resolver 2>/dev/null) || :

# 
PARAMS=

# Process command line arguments to support catalogs before Saxon sees it
#
# Inspired by Florent Georges' blog entry, but improved it a bit:
# http://fgeorges.blogspot.com/2007/01/shell-script-to-launch-saxon.html
while [[ ${1:0:1} = '-' ]]; do
   case "$1" in
    --catalogs=*)
      # PREREQUISITES: Each catalog file has to be separated with
      #                colon (:) or semicolon (;)
      # Strips the option:
      CATS=${1#--catalogs=}
      # Replace *all* ~/ with $HOME/ (the ~/ avoids any problems when
      # ~ occurs *after* a file (unlikely, but better safe than sorry):
      CATS=${CATS//\~\//$HOME\/}
      # Replaces *all* : to ;
      CATS=${CATS///;}
      # Replace any occurance of $BASE_CATALOG as we don't want them
      # to occur more than once
      # 1. Replace any / -> \/ to hide "/" for BASE_CATALOG and save it to X
      # 2. Remove BASE_CATALOG from the catalog list ($CATS) 
      # 3. Replace double semicolons (;;) with only one
      X=${BASE_CATALOG//\//\\/}
      CATS=${CATS//$X/}
      CATS=${CATS//;;/;}
      ;;
    --catalog-verbose=*)
      CATALOG_VERB=${1#--catalog-verbose=}
      ;;
    --catalog-verbose)
      # CATALOG_VERB=3
      ;;
   # -h|--help|-?)
   #   echo "Help" 
   #   exit 1
   #   ;;
    --*)
      printf "Unknown option '$1'\n" 
      exit 1
      ;;
    -*)
      # Just leave it as it is, probably Saxon options
      PARAMS="$PARAMS $1"
      ;;
   esac
   shift
done


# If we have resolver, add the CatalogManager.properties dir to CLASSPATH,
# and tweak command line options so that it's used.
args=
if [[ $CLASSPATH = *xml-commons-resolver* ]]; then
  CLASSPATH=".:$CLASSPATH:/etc/java/resolver/"
  # Tune options to use resolver.
  r=org.apache.xml.resolver.tools.ResolvingXMLReader
  for opt in -x -y ; do
    if [[ $opt != '' ]] ; then
      args="$args $opt:$r"
    fi
  done
  r=org.apache.xml.resolver.tools.CatalogResolver
  args="$args -r:$r"
  
  # Extend xml.catalog.files with CATS (see above), but only if it
  # is not empty. If non-empty, add the colon separator before our
  # BASE_CATALOG
  # ${CATS:+$CATS:}$BASE_CATALOG
  BASE_FLAGS="-Dxml.catalog.files=${CATS:+$CATS;}$BASE_CATALOG -Dxml.catalog.verbosity=$CATALOG_VERB"
fi

if [[ ${VERBOSE:-no} != 'no' ]]; then
  echo "Parameter:    $PARAMS"
  echo "Catalogs:     $CATS"
  echo "Catalog Verb: $CATALOG_VERB"
#  echo "BASE_FLAGS:   $BASE_FLAGS"
fi


# Set parameters
set_jvm
set_classpath $BASE_JARS
set_flags $BASE_FLAGS
set_options $BASE_OPTIONS

# Let's start
run $args $PARAMS

