#!/bin/ksh -p
# Modifications Copyright 2002 Compaq Computer Corporation

#
# Copyright 2002 Sun Microsystems, Inc. All rights reserved.
# SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
#

#===================================================================
# THIS SCRIPT AND JAVA WILL NOT RUN UNDER SUNOS4.X, AKA SOLARIS 1.X.  
#===================================================================

## DEC Java: cds - don't redirect output to null
#PRG=`whence $0` >/dev/null 2>&1
PRG=`whence $0`
progname=`/usr/bin/basename $0`
## DEC Java: HDS change 'uname -p' to 'uname -m'
proc=`/usr/bin/uname -m`

# Resolve symlinks. See 4152645.
while [[ -h "$PRG" ]]; do
    ls=`/usr/bin/ls -ld "$PRG"`
    link=`/usr/bin/expr "$ls" : '^.*-> \(.*\)$'`
    if /usr/bin/expr "$link" : '^/' > /dev/null; then
	prg="$link"
    else
	prg="`/usr/bin/dirname $PRG`/$link"
    fi
## DEC Java: HDS - don't redirect output to null
##    PRG=`whence "$prg"` > /dev/null 2>&1
    PRG=`whence "$prg"`
done

APPHOME=`/usr/bin/dirname "$PRG"`/..
JREHOME=$APPHOME/jre

# Where is JRE?
unset jre
if [[ -f "${JREHOME}/lib/${proc}/libjava.so" ]]; then
    jre="${JREHOME}"
fi
if [[ -f "${APPHOME}/lib/${proc}/libjava.so" ]]; then
    jre="${APPHOME}"
fi
if [[ "x${jre}" = "x" ]]; then
    echo "Error: can't find libjava.so."
    exit 1
fi

# Select vm type
ttype=native_threads
# DEC Java FLG - fast, fast32, & fast64 are now in the jvm.cfg file
#                also, don't redirect output to null
#if /usr/bin/grep "^$1\$" ${jre}/lib/jvm.cfg > /dev/null; then
if /usr/bin/grep -q "^$1\$" ${jre}/lib/jvm.cfg; then
    # User specified some VM type known to jvm.cfg.
    vmtype=`echo $1 | /usr/bin/cut -c 2-`
else
    # DEC Java FLG - added support for the JAVA_FAST64_VM environment variable
    if [ ! -z "$JAVA_FAST64_VM" ]
    then
      vmtype=fast64
      fast64_vm_flag=true
    else
      # DEC Java FLG - added support for the JAVA_CLASSIC_VM env variable
      if [ ! -z "$JAVA_CLASSIC_VM" ]
      then
        vmtype=classic
        classic_vm_flag=true
      else
        # User didn't specify a VM type, see if default is classic.
        default=`/usr/bin/grep -v '^#' "${jre}/lib/jvm.cfg" | /usr/bin/head -1`
        vmtype=`echo ${default} | /usr/bin/cut -c 2-`
      fi
    fi
fi

## DEC Java: HDS - add separate loop for -taso that does not stop searching
##                 on the first arg that doesn't start with a
##                 runtime-opeion-specifying hyphen, '-'.
for a in "$@"; do
   case $a in
       -taso)
           if [ "$progname" = java ] ; then
              progname=java-taso
           elif [ "$progname" = java_g ] ; then
              progname=java-taso_g
           fi
           ;;
   esac
done


# If jre is in a jre subdir, include parent dir libraries in lib path.  This
# needs to be cleaned up because -Xrun libraries are the ones that need it.
if [ "${jre}" = "$JREHOME" ]; then
   JAVA_LIBRARY_PATH=":${jre}/../lib/${proc}"
fi

# Set LD_LIBRARY_PATH to find libawt.so etc.
JAVA_LIBRARY_PATH="${jre}/lib/${proc}:${JAVA_LIBRARY_PATH}"

# DEC Java FLG - set LD_LIBRARY_PATH to find libhpi.so
JAVA_LIBRARY_PATH="${jre}/lib/${proc}/${ttype}:${JAVA_LIBRARY_PATH}"

# Set LD_LIBRARY_PATH for libjvm.so (necessitated by ld.so bugid 4176579)
# proc/vmtype needs to be in front of just proc (4345224)
JAVA_LIBRARY_PATH="${jre}/lib/${proc}/${vmtype}:$JAVA_LIBRARY_PATH"

LD_LIBRARY_PATH="${JAVA_LIBRARY_PATH}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH

prog="$APPHOME/bin/${proc}/${ttype}/${progname}"

# Run.
if [[ -x "$prog" ]]
then
     # DEC Java FLG - special condition for the JAVA_FAST64_VM,
     # JAVA_CLASSIC_VM environment variables as well as a special
     # condition for jdb.
     if [ "$fast64_vm_flag" = "true" ]
     then
        exec $DEBUG_PROG "$prog" -fast64 "$@"
     else
        if [ "$classic_vm_flag" = "true" ]
	then
           if [[ ("$progname" = "jdb") || ("$progname" = "jdb_g") ]]
           then
             exec $DEBUG_PROG "$prog" -tclassic "$@"
           else
	     exec $DEBUG_PROG "$prog" -classic "$@"
           fi
	else
           # Calling: exec $DEBUG_PROG "$prog" "$@"
           exec $DEBUG_PROG "$prog" "$@"
	fi
     fi
else
    echo >&2 "$progname was not found in ${prog}"
    exit 1
fi
