#!/bin/sh 
#	$Id: pia_wrapper,v 1.14 1999-12-14 18:29:00 steve Exp $
# Pia launcher.  Also handles other classes in the PIA hierarchy.
#	Always tacks on -p propertyFile, which may be a misfeature.
#
# Usage: 
#	pia_wrapper [-v] [class [arg]... ]

### find out where java is.
#	Resort to a kludge to get around the fact that "type foo"
#	returns something like "foo is xxx", but "type -p foo" is
#	not supported on Solaris.

if [ "$1" = "--kludge" ] ; then
    echo $4
    exit
fi

JJ=`type java`
JAVA_BIN=`$0 --kludge $JJ` >/dev/null 2>&1

if [ ! -x "$JAVA_BIN" ] ; then
   echo "Trying jre."
  JJ=`type jre`
  JAVA_BIN=`$0 --kludge $JJ` >/dev/null 2>&1
fi

if [ ! -x "$JAVA_BIN" ] ; then
    echo "Cannot locate java or jre." 1>&2
    echo "Please put java's binary directory in your $PATH" 1>&2
    exit 1
fi
export JAVA_BIN

JAVA_BIN_DIR=`dirname $JAVA_BIN`
J_HOME=$JAVA_BIN_DIR/..
[ -d $J_HOME ] && J_HOME=`(cd $J_HOME && pwd)`

if [ -z "$JAVA_HOME" ] ; then
    export JAVA_HOME
    JAVA_HOME=$J_HOME
fi

### Find out where this command is:

if [ -z "$PIA_HOME" ] ; then
  MY=`type $0`
  MY_BIN=`$0 --kludge $MY` >/dev/null 2>&1
  MY_BIN_DIR=`dirname $MY_BIN`
  if [ ! -x "$MY_BIN" ] ; then
    echo "Cannot locate binary directory for $0." 1>&2
    echo "Please put the PIA's binary directory in your shell's PATH" 1>&2
    exit 1
  fi
else
  MY_BIN_DIR=$PIA_HOME/bin
fi

### Process the "-v" flag specially 
#	If it occurs before the class, make Java verbose as well.

if [ "$1" = "-v" ] ; then
    VERBOSE=1
    V="-v"
    shift
elif [ "$2" = "-v" ] ; then
    VERBOSE=2
fi

if [ "$1" = "-debug" ] ; then
    D="-debug"
    shift
fi
### Find out where the PIA is.
#	The most likely candidate is the parent directory of $0.

PIA_HOME="${PIA_HOME-$MY_BIN_DIR/..}"
[ -d $PIA_HOME ] && PIA_HOME=`(cd $PIA_HOME && pwd)`
export PIA_HOME

#The user's .pia directory:
PIA_ROOT="${PIA_ROOT-$HOME/.pia}"
export PIA_ROOT

### Build an environment string:

ENV="PIA_HOME=$PIA_HOME PIA_ROOT="$PIA_ROOT" HOME=$HOME USER=$USER"

for key in DISPLAY http_proxy ftp_proxy wais_proxy gopher_proxy no_proxy
do
    v=`eval echo \$"$key"`
    [ -z "$v" ] || ENV="$ENV $key=$v"
done

### Build a CLASSPATH if we are not in the PIA's code directory

# The pia source directory.  (Later we will test for their existence)
PIA_SRC=${PIA_SRC-$PIA_HOME/src}

if [ `/bin/pwd` = `(cd $PIA_HOME/src/java; /bin/pwd)` ]; then
    true
else
    old_classpath=$CLASSPATH
    [ -z "$CLASSPATH" ] || CLASSPATH=":$CLASSPATH"
    CLASSPATH=$PIA_HOME/lib:$PIA_CLASSES$CLASSPATH
    [ -d $PIA_SRC ] && CLASSPATH=$PIA_SRC/java:$CLASSPATH
    export CLASSPATH
    if [ -z "$1" ] ; then
	PIA_CLASS=org.risource.pia.Pia
    else
	PIA_CLASS=$1
	shift
    fi
    [ -z "$old_classpath" ] && echo "CLASSPATH not set; using $CLASSPATH" 1>&2
    [ -z "$VERBOSE" ] || echo CLASSPATH=$CLASSPATH 1>&2
    export CLASSPATH
    exec $JAVA_BIN $D $V $PIA_CLASS $V $ENV $*
fi

### Identify our main program.

if [ -z "$1" ] ; then
    PIA_CLASS=org.risource.pia.Pia
else
    PIA_CLASS=$1
    shift
fi

[ -z "$VERBOSE" ] || echo "$JAVA_BIN $V $PIA_CLASS $V $ENV $*" 1>&2
exec $JAVA_BIN $D $V $PIA_CLASS $V $ENV $*

