#! /bin/sh
#
#     @(#)java_wrapper.sh	1.29 97/05/14
#
#===================================================================
# THIS SCRIPT AND JAVA WILL NOT RUN UNDER SUNOS4.X, AKA SOLARIS 1.X.  
#===================================================================

# Set up default variable values if not supplied by the user.

PRG=`type -p $0` >/dev/null 2>&1

while [ -L "$PRG" ]
do
    newprg=`expr "\`/bin/ls -l "$PRG"\`" : ".*$PRG -> \(.*\)"`
    expr "$newprg" : / >/dev/null || newprg="`dirname $PRG`/$newprg"
    PRG="$newprg"
done

J_HOME=`dirname $PRG`/..
ARCH=`arch`
progname=`basename $0`

# The default THREADS_TYPE is "green_threads".  To change the default change
# the setting of the DEFAULT_THREADS_FLAG variable.  The only valid values 
# of that variable are 'green' and 'native'. 
# 
# This introduces a dependency of this wrapper on the policy used to do builds.
# e.g. the usage of the name "green_threads" here is dependent on the build
# scripts which use the same name. Since this is somewhat analogous to the
# wrapper already depending on the build scripts putting the executable in
# a specific place (JAVA_HOME/bin/`uname -p`), the new dependency does not
# seem all that bad.

DEFAULT_THREADS_FLAG=green

if [ ${THREADS_FLAG-foo} = native ] ; 
	then THREADS_TYPE=native_threads
else
    THREADS_TYPE=green_threads
fi
export THREADS_TYPE
#echo "Using executables built for $THREADS_TYPE"

#
# If the -noenv argument is specified, we set JAVA_HOME
# and CLASSPATH to nothing, effectively ignoring those
# environment variables.
#
# If one of java's '-*' options was specified on the command line,
# then ksh insists on trying to interpret the option when
# we do a set.  Worse, it swallows the option, refusing
# to pass it on to (binary) java.  So we keep track of
# all options and pass them on to eval by hand.
#
# Search for '-native' or '-green' flags, and remove them from the
# arguments if found.  Also if found, set THREADS_TYPE to either
# 'native_threads' or 'green_threads', as appropriate.  This is an
# alternative to using the THREADS_FLAG environment variable to 
# specify the threads package for Solaris.
# On the off chance someone wants to pass -green or -native to
# a java program, we stop searching on the first arg that doesn't
# start with a runtime-option-specifying hyphen, '-'.
# 

for a in "$@"; do
    case $a in
	-native)
	    THREADS_TYPE=native_threads
	    ;;
	-green)
	    THREADS_TYPE=green_threads
	    ;;
	-*) ;;
        *) break;
    esac
done

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

CLASSPATH="${CLASSPATH-.}"
if [ -z "${CLASSPATH}" ] ; then
    CLASSPATH="$JAVA_HOME/classes:$JAVA_HOME/lib/classes.jar:$JAVA_HOME/lib/rt.jar:$JAVA_HOME/lib/i18n.jar:$JAVA_HOME/lib/classes.zip"
else
    CLASSPATH="$JAVA_HOME/classes:$JAVA_HOME/lib/classes.jar:$JAVA_HOME/lib/rt.jar:$JAVA_HOME/lib/i18n.jar:$JAVA_HOME/lib/classes.zip:$CLASSPATH"
fi

export CLASSPATH

# LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$JAVA_HOME/lib/`uname -p`/$THREADS_TYPE"
LD_LIBRARY_PATH="$JAVA_HOME/lib/`uname -m`/$THREADS_TYPE:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH

      
prog=$JAVA_HOME/bin/`uname -m`/${THREADS_TYPE}/${progname}

if [ -f $prog ]
then
     exec $DEBUG_PROG $prog $opts "$@"
else
    echo >&2 "$progname was not found in ${prog}"
    exit 1
fi
