#!/bin/bash

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

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

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

Usage: eresolve [options] xmlfile

The eresolve program resolves the entities in an xml file by looking
up entity declarations in the specified catalogs.

Options:

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

 -d <debuglevel>        Verbosity level. NUM in [0-3], default is 2
 -c <catalogfile>       Parse <catalogfile>, can have multiple -c entries
 -s                     Load system catalogs (after -c catalogs, if given)
 -r                     Retry

Example:  eresolve -s yourfile.xml

 Arbortext 'eresolve' 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;
}

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

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

catalogfile=''
catfilelist=''
debuglevel=2
retry=''

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

arbortext_jars=$java_rep/catalog.jar:$java_rep/catalog-apps.jar
xerces_jar=$java_rep/xerces.jar

## ----------------------------------------------------------------------
## get command line options
options=":hc:d:rsgj:"
while getopts $options opt
  do
  case $opt
      in
      h  ) usage_message ;;
      c  ) catfilelist="$OPTARG $catfilelist" ;;
      s  ) load_system_catalogs="true" ;;
      d  ) debuglevel="$OPTARG" ;;
      r  ) retry=true  ;;
      s  ) load_system_catalogs=true ;;
      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
for catalogfile in $catfilelist
  do
  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 URL by prefixing it with your current directory: \n  \"$PWD\"" 
#       echo
      catalogfile=${PWD}/$catalogfile 
#       echo -e " Modified catalog name: \n \"$catalogfile\""
#       echo
  fi
  OPTS=$OPTS"-c $catalogfile "
  done

## ----------------------------------------------------------------------
## collect other options

# put system cat AFTER user-specified catalogs
[ $load_system_catalogs = "true" ] && OPTS=$OPTS" -c $system_catalog "

[ ! -z $retry ] && OPTS="-r "$OPTS
OPTS="-d $debuglevel "$OPTS 

## ----------------------------------------------------------------------
## set classpath

set -a
CLASSPATH=$arbortext_jars:$xerces_jar
set +a 

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

$java eresolve $OPTS $*

exit 0


