#! /bin/sh
#
#  $Revision: 1.3 $ $Date: 2000/01/07 00:46:44 $ $Author: kreilede $
#  Based on:   
#     @(#)java_wrapper.sh	1.35 98/12/23


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

PRG=`type -p $0` >/dev/null 2>&1
# If PRG is a symlink, trace it to the real home directory

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`/..
progname=`basename $0`
ARCH=`arch`

# 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/${ARCH}), the new dependency does not
# seem all that bad.

DEFAULT_THREADS_FLAG=green

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

#
# If -native or -green is the first argument, override threads type
# based on that. Also, remove it from $@, because this is an argument
# _only_ to this wrapper. This is an alternative to setting
# THREADS_FLAG on Linux and Solaris.
#
if [ "$1" = "-native" -o "$1" = "-green" ]; then
    if [ "$1" = "-native" ]; then
      THREADS_TYPE=native_threads
    else
      THREADS_TYPE=green_threads
    fi
    shift 1
fi

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

#
# For some programs like appletviewer, it is important that "." not be
# in the classpath by default (unless the user set the CLASSPATH
# explicitly). Applications that fit in this list are ones that load
# classes through a ClassLoader, where classes coming off . will end
# up being system classes. For now we know of only appletviewer.
#
NO_DOT_LIST="appletviewer"
DEFAULT_CLASSPATH="."
for excluded in ${NO_DOT_LIST}; do
    if [ ${excluded} = ${progname} ]; then
        DEFAULT_CLASSPATH="";
    fi
done

CLASSPATH="${CLASSPATH:-${DEFAULT_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="$CLASSPATH:$JAVA_HOME/classes:$JAVA_HOME/lib/classes.jar:$JAVA_HOME/lib/rt.jar:$JAVA_HOME/lib/i18n.jar:$JAVA_HOME/lib/classes.zip"
fi

export CLASSPATH

LD_LIBRARY_PATH="$JAVA_HOME/lib/${ARCH}/$THREADS_TYPE:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH

XFILESEARCHPATH="$JAVA_HOME/lib/locale/%L/%T/%N%C%S:$JAVA_HOME/lib/locale/%l/%T/%N%C%S:$JAVA_HOME/lib/locale/%T/%N%C%S:$JAVA_HOME/lib/locale/%L/%T/%N%S:$JAVA_HOME/lib/locale/%l/%T/%N%S:$JAVA_HOME/lib/locale/%T/%N%S:$XFILESEARCHPATH:%D"
export XFILESEARCHPATH

prog=$JAVA_HOME/bin/${ARCH}/${THREADS_TYPE}/${progname}

LD_BIND_NOW=true
export LD_BIND_NOW

if [ -f $prog ]
then
     exec $DEBUG_PROG $prog $opts "$@"
else
    prog=$JAVA_HOME/bin/${ARCH}/${THREADS_TYPE}/${progname}
    if [ -f $prog ]
    then
        exec $DEBUG_PROG $prog $opts "$@"
    fi
fi

echo >&2 "$progname was not found in ${prog}"
exit 1
