#! /bin/sh
#
#  $Revision: 1.2 $ $Date: 1999/07/19 19:15:08 $ $Author: srh $
#  Based on:   
#     @(#)java_wrapper.sh	1.35 98/12/23


# This script has been altered to make it POSIX-sh compliant.

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

PRG=`type $0`
PRG=${PRG##* }
# If PRG is a symlink, trace it to the real home directory

while [ -L "$PRG" ]
do
    newprg=$(ls -l ${PRG})
    newprg=${newprg##*-> }
    [ ${newprg} = ${newprg#/} ] && newprg=${PRG%/*}/${newprg}
    PRG="$newprg"
done

PRG=${PRG%/*}
J_HOME=${PRG%/*}
progname=${0##*/}
ARCH=`/bin/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.

# Actually, the default THREADS_TYPE is now "native_threads", assuming
# it's available.  If not, "green_threads" will have to do. :) - srz

if [ -x ${J_HOME}/bin/${ARCH}/native_threads/${progname} ]
then
    DEFAULT_THREADS_FLAG=native
else
    DEFAULT_THREADS_FLAG=green
fi

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 Solaris.
#
case "$1" in
-native)
      THREADS_TYPE=native_threads
      shift
      ;;

-green)
      THREADS_TYPE=green_threads
      shift
      ;;

esac

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

for class in \
    /usr/share/java/repository \
    $JAVA_HOME/classes \
    $JAVA_HOME/lib/classes.jar \
    $JAVA_HOME/lib/rt.jar \
    $JAVA_HOME/lib/i18n.jar \
    $JAVA_HOME/lib/classes.zip
do
    if [ -f $class ]; then
	classpath="$classpath:$class"
    fi
    if [ -d $class ]; then
	classpath="$classpath:$class"
    fi
done
classpath=${classpath#:}

CLASSPATH="${CLASSPATH:-${DEFAULT_CLASSPATH}}"
if [ -z "${CLASSPATH}" ] ; then
    CLASSPATH="$classpath"
else
    CLASSPATH="$CLASSPATH:$classpath"
fi

export CLASSPATH

if [ -z "${LD_LIBRARY_PATH}" ]; then
    LD_LIBRARY_PATH="$JAVA_HOME/lib/${ARCH}/${THREADS_TYPE}"
else
    LD_LIBRARY_PATH="$JAVA_HOME/lib/${ARCH}/${THREADS_TYPE}:$LD_LIBRARY_PATH"
fi

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
