#!/bin/ksh -p

#
# @(#)java_wrapper.sh	1.50 98/09/15
#
# Copyright 1994-1998 by Sun Microsystems, Inc.,
# 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
# All rights reserved.
#
# This software is the confidential and proprietary information
# of Sun Microsystems, Inc. ("Confidential Information").  You
# shall not disclose such Confidential Information and shall use
# it only in accordance with the terms of the license agreement
# you entered into with Sun.
#
#===================================================================
#
# This script has been modified to support JDK 1.2 on PA-RISC systems
# running HPUX 10.x and 11.0
#
#===================================================================
#
PRG=`whence $0` >/dev/null 2>&1
progname=`/usr/bin/basename $0`
proc=PA_RISC
APPHOME=`/usr/bin/dirname "$PRG"`/..

function usage {
   echo "Usage: $progname [-usage] [-classic|-hotspot] [-green|-native] [-pa11] [VM_options] class [args...] "
   echo " "
   echo "  -usage           Prints this message."
   echo "  -classic         Invoke the Classic VM."
   echo "  -hotspot         Invoke the HotSpot VM if available (default)."
   echo "  -green           Use green threads (only with -classic) (default on 10.20)."
   echo "  -native          Use native threads (only with -classic) (default on 11.0)."
   echo "  -pa11            Force the use of PA1.1 libraries on a PA2.0 system."
   echo "  -HPrmi           Use the HP implementation of RMI/IIOP, valid only for tnameserv and java"
   echo "                       (makes RMI/IIOP available)"
   echo " "
   echo "To list VM_options invoke \"java -help\"."
}

# 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
    PRG=`whence "$prg"` > /dev/null 2>&1
    APPHOME=`/usr/bin/dirname "$PRG"`/..
done

JREHOME=$APPHOME/jre

# Where is JRE?
unset jre
if [ -f "${JREHOME}/lib/${proc}/libjava.sl" ]; then
    jre="${JREHOME}"
fi
if [ -f "${APPHOME}/lib/${proc}/libjava.sl" ]; then
    jre="${APPHOME}"
fi
# if libjava is not found, check for libjava_g
if [ "x${jre}" = "x" ]; then
#perhaps a warning here would be appropriate ?
if [ -f "${JREHOME}/lib/${proc}/libjava_g.sl" ]; then
    jre="${JREHOME}"
fi
if [ -f "${APPHOME}/lib/${proc}/libjava_g.sl" ]; then
    jre="${APPHOME}"
fi
fi
# if libjava[_g] is still not found, exit with error
if [ "x${jre}" = "x" ]; then
    echo "Error: can't find libjava.sl."
    exit 1
fi


# Select vm type (if classic vm, also select thread type).
unset vmtype
unset ttype

# The default THREADS_TYPE is "green_threads" for HP-UX 10.x systems
# and "native_threads" for HP-UX 11.0 systems
#
case $(uname -r) in
B.10.*) 
   DEFAULT_THREADS_FLAG=green;;
*)
   DEFAULT_THREADS_FLAG=native;;
esac
ttype=${DEFAULT_THREADS_FLAG}_threads

if [[ "`getconf CPU_VERSION`" = "532" && -f "${jre}/lib/PA_RISC2.0/classic/libjvm.sl" ]]; then
	proc=PA_RISC2.0
else
	proc=PA_RISC
fi
vmtype=hotspot
specify=implicit
set -A options

while [[ $# -ne 0 && "$1" = -* ]] ; do
   case "$1" in
      -usage)
         usage; exit 1
         ;;
      -pa11)
	 proc=PA_RISC
	 shift 1
	 ;;
      -hotspot)
	 vmtype=hotspot
	 specify=explicit
	 shift 1
	 ;;
      -classic)
	vmtype=classic
	shift 1
	;;
      -green)
	ttype=${DEFAULT_THREADS_FLAG}_threads
	shift 1
	;;
      -native)
	ttype=${DEFAULT_THREADS_FLAG}_threads
	shift 1
	;;
      -HPrmi)
        case "$progname" in
                tnameserv)
                        if [ -f ${APPHOME}/lib/wstools.jar ]
                        then
                                echo "Cannot run 1.3 RMI with WebSphere installed!"
                                exit 5
                        fi
			if [ ! -f ${JREHOME}/lib/ext/HPrmi_iiop.jar ]
			then
				echo "Cannot run 1.3 RMI without RMI/IIOP package installed!"
				exit 4
			fi
			set -A options -- "${options[@]}" "-J-Xbootclasspath/p:${JREHOME}/lib/ext/HPrmi_iiop.jar"
                        progname="${progname}.iiop"
                        shift 1
                        ;;
                java)
                        if [ -f ${APPHOME}/lib/wstools.jar ]
                        then
                                echo "Cannot run 1.3 RMI with WebSphere installed!"
                                exit 5
                        fi
			if [ ! -f ${JREHOME}/lib/ext/HPrmi_iiop.jar ]
			then
				echo "Cannot run 1.3 RMI without RMI/IIOP package installed!"
				exit 4
			fi
			set -A options -- "${options[@]}" "-Xbootclasspath/p:${JREHOME}/lib/ext/HPrmi_iiop.jar"
                        shift 1
                        ;;
                *)
                        shift 1
                        ;;
        esac
	;;
      -*)
	set -A options -- "${options[@]}" "$1"
	shift 1
	;;
      *)
        # Collect the first argument unknown to the wrapper which begins with "-" and
	# terminate argument processing.
	break
	;;
   esac
done

# if the user explicitly specifies to use the hotspot VM, then check to see if it
# is present, if not, issue a warning and use the classic VM instead.
if [[ "$vmtype" = "hotspot" && "$specify" = "explicit" && ! -d ${jre}/lib/${proc}/hotspot ]]; then
    echo 'Warning: Hotspot Java VM not present - using classic Java VM instead.'
    vmtype=classic
fi 

# handle the case where the user did not explicitly specify to use any VM, and
# the hotspot VM does not exist, the old wrapper silently used the classic VM
# this check is to produce the same behaviour.
if [[ "$specify" = "implicit" && ! -d ${jre}/lib/${proc}/hotspot ]]; then
    vmtype=classic
fi

# Special handling for classic VM.
if [ "${vmtype}" = "classic" ]; then
    # fix for bug 4032715
    if [[ ${ttype} = green_threads ]] ; then 
	LD_BIND_NOW=yes
	export LD_BIND_NOW
    fi
    # For internal use by classic VM.
    _JVM_THREADS_TYPE="${ttype}"
    export _JVM_THREADS_TYPE
fi

if [ "${vmtype}" = "classic" ]; then
    # JAGab75626: Invoke PA_RISC1.1 binary, which is not EXEC_MAGIC.
    vmproc=PA_RISC
else
    vmproc=${proc}
fi

# Set SHLIB_PATH for hotspot VM.
SHLIB_PATH="${jre}/lib/${proc}/${ttype}:${jre}/lib/${proc}/${vmtype}:${jre}/lib/${proc}:$SHLIB_PATH"
export SHLIB_PATH

# prepend XFILESEARCHPATH with awt Motif default locale resource files.
XFILESEARCHPATH="${jre}/lib/locale/%L/%T/%N%S:$XFILESEARCHPATH"
export XFILESEARCHPATH

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

# JAGad10018 000621 1.2.2.0.5
# Moved fix for JAGac59570 to awt_MToolkit.c. 

# Run.
if [ -x "$prog" ]
then
    # invoke the VM $args contains arguments which begin with "-", $* contains everything else.
    exec $DEBUG_PROG "$prog" "${options[@]}" "$@"
else
    echo >&2 "$progname was not found in ${prog}"
    exit 1
fi
