#!/bin/bash

## ----------------------------------------------------------------------
## exit on erroneous subcommand
set -e

## ----------------------------------------------------------------------
## get script name
script=$(basename $0)

## ----------------------------------------------------------------------
## print help message
function usage_message
{
    cat >&2 <<'END'

Usage: catalog [options] command

The catalog program parses one or more Catalog files and performs a
single lookup of a public or system identifier. Running catalog with
no arguments will display a summary of this usage information.

Options:

 -h                    Print this help message
 -E                    Show usage examples
 -g                    Use gij, the GNU java interpreter
 -j <java_interpreter> Specify alternate java interpreter

 -c <catalogfile>      Can be repeated to load several catalogs
 -d <debuglevel>       Parsing verbosity, an integer in [0-3]
 -p <parserClass>      Name of a parser class for reading Cowan XML Catalogs
 -s                    Load system catalogs & give them a higher search precedence
                       than catalogs specified via -c <catalogfile>

  Note: to use the -p option, the relevant class files
        needed by <parserClass> must be in your CLASSPATH

Commands take one of the following forms:

  document
  doctype  name publicid systemid
  entity   name publicid systemid
  notation name publicid systemid
  public        publicid systemid
  system                 systemid

 Arguments are positional, use the string "null" to indicate a null
 value.

Examples: type 'catalog -E' for usage examples

  Arbortext 'catalog' app wrapper script, version 1.01 
  Copyright (C) 2001 Mark Johnson 
  This is free software; see the GPL version 2 or later. NO warranty.

END
exit 0;
}

function usage_examples
{
    cat >&2 <<'END'
'catalog' usage examples:
=========================

 --Input: catalog -s public "-//OASIS//DTD DocBook MathML Module V1.0//EN"

 --Output:
   Loading system catalogs.
   Set debug to: 0
   Resolving public:
	   Public: -//OASIS//DTD DocBook MathML Module V1.0//EN
	   System: null

   Resolved: file:/usr/share/sgml/docbook/custom/mathml/1.0/dbmathml.dtd
 ---------------------------------------------------------------------   

 --Input: catalog -s -d 0 system "http://www.oasis-open.org/docbook/xml/mathml/1.0/dbmathml.dtd"

 --Output:
   Loading system catalogs.
   Set debug to: 0
   Resolving system:
	   Public: null
	   System: http://www.oasis-open.org/docbook/xml/mathml/1.0/dbmathml.dtd

   Resolved: file:/usr/share/sgml/docbook/custom/mathml/1.0/dbmathml.dtd
 --------------------------------------------------------------------- 

 --Input: catalog -c http://oasis-open.org/docbook/xml/4.1.2/docbook.cat \
                  public "-//OASIS//DTD DocBook XML V4.1.2//EN"
 --Output:
   Ignoring system catalogs.
   Set debug to: 0
   Adding catalog: http://oasis-open.org/docbook/xml/4.1.2/docbook.cat
   Resolving public:
   	   Public: -//OASIS//DTD DocBook XML V4.1.2//EN
	   System: null

   Resolved: http://oasis-open.org/docbook/xml/4.1.2/docbookx.dtd

END
exit 0;
}

## ----------------------------------------------------------------------
## print error message
function usage_error
{
    echo -e >&2 "$script: $@";
    exit 2;
}

## ----------------------------------------------------------------------
## set default values

user_catalogs=''
catalogfile=''
debuglevel=0
parserClass=''

use_gij=false
load_system_catalogs=false

OPTS=''

## ----------------------------------------------------------------------
## some variables

java=/usr/bin/java
gij=/usr/bin/gij-3.0
java_rep=/usr/share/java
system_catalog=/etc/sgml/catalog
system_cat_prop="-Dxml.catalog.files=$system_catalog"

arbortext_jars=$java_rep/catalog.jar:$java_rep/catalog-apps.jar
crimson_jar=$java_rep/jaxp.jar:$java_rep/crimson.jar

## ----------------------------------------------------------------------
## get command line options
options=":hEc:d:p:sgj:"
while getopts $options opt
  do
  case $opt
      in
      h  ) usage_message ;;
      E  ) usage_examples ;;
      s  ) load_system_catalogs="true" ;;
      c  ) catalogfile="$OPTARG"$catalogfile ;;
      d  ) debuglevel=$OPTARG ;;
      p  ) parserClass="$OPTARG";;
      g  ) use_gij=true; java=$gij ;;
      j  ) java="$OPTARG" ;;
      \? ) usage_error "unrecognized option \`$OPTARG'" ;;
  esac
done

[ $OPTIND = 1 ] && usage_message # usage message if no input

shift $(($OPTIND - 1))


## ----------------------------------------------------------------------
## if catalogfile has relative path, make it absolute

if [ ! -z $catalogfile ] && [ -z `echo $catalogfile|grep "^/\|ftp://\|http://"` ]; then
#    echo 
#    echo -e " Hmm... your catalog name: \"$catalogfile\" \n \
# looks like a relative path on the local filesystem."
#    echo
#    echo -e " I made the path an absolute path by prefixing it with your current directory: \n  \"$PWD\"" 
#    echo
   catalogfile=${PWD}/$catalogfile 
#    echo -e " Modified catalog name: \n \"$catalogfile\""
#    echo
fi

## ----------------------------------------------------------------------
## collect options for feeding to jvm

[ ! -z $catalogfile ] && OPTS="-c $catalogfile "$OPTS
[ $load_system_catalogs = "true" ] && OPTS="-s "$OPTS
[ ! -z $parserClass ] && OPTS="-p $parserClass "$OPTS
OPTS="-d $debuglevel "$OPTS 

## ----------------------------------------------------------------------
## set classpath
set -a
CLASSPATH=$arbortext_jars
set +a 

## ----------------------------------------------------------------------
## Do it

$java $system_cat_prop catalog $OPTS "$@"

exit 0


