#!/bin/sh
#
#     @(#)java_wrapper.sh	1.35 98/12/23
#
#===================================================================
# 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=`whence $0` >/dev/null 2>&1
DIR=`dirname $0`
progname=`basename $0`
PRG=`cd $DIR; pwd `/$progname
J_HOME=`cd $DIR/..; pwd`

# 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 -m`), 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 Solaris.
#
if [ X"$1" = X"-native" -o X"$1" = X"-green" ]; then
    if [ X"$1" = X"-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

proc=`/usr/bin/uname -m`
LD_LIBRARY_PATH="$JAVA_HOME/lib/$proc/$THREADS_TYPE:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH

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

# By default, the real program is the same as the called program
execname=${progname}

# If an 'X' version exists and DISPLAY is set, then use the X version.
if [ -x $JAVA_HOME/bin/$proc/${THREADS_TYPE}/${progname}_X \
     -a "X${DISPLAY}" != "X" ]
then
   execname=${progname}_X
fi

prog=$JAVA_HOME/bin/$proc/${THREADS_TYPE}/${execname}

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