#!/bin/ksh -p

#
# @(#)java_wrapper_solaris.sh	1.63 00/06/23
# and java_wrapper_hp-ux.sh
#
# Copyright 2000 Sun Microsystems, Inc. All rights reserved.
# Copyright 2000 Sun Microsystems, Inc. Tous droits rservs.
#
# This software is the proprietary information of Sun Microsystems, Inc.
# Use is subject to license terms.
#
#===================================================================
#
# This script has been modified to support JDK 1.2 on PA-RISC systems
# running HPUX 10.x and 11.0 and on IA64 systems.
#
#===================================================================
#
PRG=`whence $0` >/dev/null 2>&1
progname=`/usr/bin/basename $0`
PROC=`getconf CPU_VERSION`

case ${PROC} in
768)
   #
   # IA64
   #
   suffix=so
   proc=IA64
   ;;
532)
   #
   # PA-RISC 2.0
   #
   suffix=sl
   proc=PA_RISC2.0
   ;;
*)
   #
   # PA-RISC 1.1
   #
   suffix=sl
   proc=PA_RISC
   ;;
esac

# JAGad58707 and JAGad74892: clean up help message, synch with java.c
function usage_pa {
   echo "Usage: $progname [-usage] [-classic|-server|-hotspot|-client] [-pa11] [VM_options] class [args...] "
   echo " "
   echo "  -server          run the HotSpot VM, tuned for long-lived,"
   echo "                   server applications (default)"
   echo "  -hotspot         synonymous to the -server option"
   echo "  -client          run the HotSpot VM, tuned for short-lived,"
   echo "                   GUI applications"
   echo "  -classic         select the \"classic\" VM instead of the HotSpot VM"
   echo "                   If present, the option to select the VM must be first."
   echo "                   The default VM is -server."
   echo "  -pa11            force the use of PA1.1 libraries on a PA2.0 system."
   echo "  -usage           print this message."
   echo " "
   echo "To list VM_options invoke \"java -help\"."
}

function usage_ia {
   echo "Usage: $progname [-usage] [-classic|-server|-hotspot|-client] [VM_options] class [args...] "
   echo " "
   echo "  -server          run the HotSpot VM, tuned for long-lived,"
   echo "                   server applications (default)"
   echo "  -hotspot         synonymous to the -server option"
   echo "  -client          run the HotSpot VM, tuned for short-lived,"
   echo "                   GUI applications"
   echo "  -classic         select the \"classic\" VM instead of the HotSpot VM"
   echo "                   If present, the option to select the VM must be first."
   echo "                   The default VM is -server."
   echo "  -usage           Prints this message."
   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
done

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

# Where is JRE?

# if libjava is not found, check for libjava_g
if [ -f "${JREHOME}/lib/${proc}/libjava.${suffix}" ]; then
    jre="${JREHOME}"
elif [ -f "${APPHOME}/lib/${proc}/libjava.${suffix}" ]; then
    jre="${APPHOME}"
elif [ -f "${JREHOME}/lib/${proc}/libjava_g.${suffix}" ]; then
    jre="${JREHOME}"
elif [ -f "${APPHOME}/lib/${proc}/libjava_g.${suffix}" ]; then
    jre="${APPHOME}"
elif [[ "${proc}" = "PA_RISC2.0" && -f "${JREHOME}/lib/PA_RISC/libjava.${suffix}" ]]; then
        proc=PA_RISC
        jre="${JREHOME}"
elif [[ "${proc}" = "PA_RISC2.0" && -f "${JREHOME}/lib/PA_RISC/libjava_g.${suffix}" ]]; then
        proc=PA_RISC
        jre="${JREHOME}"
#
# following two conditions added to allow a PA jdk to execute on an IA system
#
elif [[ "${proc}" = "IA64" && -f "${APPHOME}/lib/PA_RISC2.0/libjava.sl" ]]; then
     proc=PA_RISC2.0
     jre="${APPHOME}"
elif [[ "${proc}" = "IA64" && -f "${JREHOME}/lib/PA_RISC2.0/libjava.sl" ]]; then
     proc=PA_RISC2.0
     jre="${JREHOME}"
elif [[ "${proc}" = "IA64" && -f "${APPHOME}/lib/PA_RISC2.0/libjava_g.sl" ]]; then
     proc=PA_RISC2.0
     jre="${APPHOME}"
elif [[ "${proc}" = "IA64" && -f "${JREHOME}/lib/PA_RISC2.0/libjava_g.sl" ]]; then
     proc=PA_RISC2.0
     jre="${JREHOME}"
else
    # libjava[_g] is still not found, exit with error
    echo >&2 "Error: can't find libjava.${suffix}."
    exit 1
fi

# Interpret THREADS_FLAG environment variable, if set.

#
# Default threads for 1.3.x are native.
#
DEFAULT_THREADS_FLAG=native
ttype=${DEFAULT_THREADS_FLAG}_threads

if [[ "${proc}" = "PA_RISC2.0" && ! -f "${jre}/lib/PA_RISC2.0/classic/libjvm.${suffix}" ]]; then
	proc=PA_RISC
fi

vmtype=server
specify=implicit
set -A options

if [ "${proc}" = "IA64" ]; then
   while [[ $# -ne 0 ]] ; do
      case "$1" in
         -usage)
            usage_ia; exit 1
            ;;
         -classic)
            vmtype=classic
            specify=explicit
            shift 1
            ;;
         -hotspot)
            vmtype=hotspot
            specify=explicit
            shift 1
            ;;
         -server)
	        vmtype=server
	        specify=explicit
	        shift 1
	        ;;
         -client)
	        vmtype=server
	        specify=explicit
            set -A options -- "${options[@]}" "-XX:+ClientApp"
	        shift 1
	        ;;
         -*)
             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
else
  while [[ $# -ne 0 && "$1" = -* ]] ; do
     case "$1" in
        -usage)
           usage_pa; exit 1
           ;;
        -pa11)
	   proc=PA_RISC
	   shift 1
	   ;;
        -hotspot)
	   vmtype=hotspot
	   specify=explicit
	   shift 1
	   ;;
        -server)
	   vmtype=server
	   specify=explicit
	   shift 1
	   ;;
        -client)
	   vmtype=server
	   specify=explicit
           set -A options -- "${options[@]}" "-XX:+ClientApp"
	   shift 1
	   ;;
        -classic)
	  vmtype=classic
	  specify=explicit
	  # Don't add -classic to options list - it breaks debugging
	  # using the DEBUG_PROG environment variable as GDB refuses
	  # to run .../native_threads/java_g -classic
	  #
      	  # set -A options -- "${options[@]}" "$1"
	  shift 1
	  ;;
        -green)
  	  ttype=${DEFAULT_THREADS_FLAG}_threads
	  shift 1
	  ;;
        -native)
	  ttype=${DEFAULT_THREADS_FLAG}_threads
	  shift 1
	  ;;
        -*)
	  if /usr/bin/grep "^$1\$" ${jre}/lib/jvm.cfg > /dev/null; then
		# User specified some VM type known to jvm.cfg
		vmtype=`\echo $1 | /usr/bin/cut -c 2-`
		specify=explicit
	  fi
	  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
fi

# if the user explicitly specifies to use a VM, then check to see if it
# is present, if not, issue a warning and use the classic VM instead.
if [[ "$vmtype" != "classic" && "$specify" = "explicit" && ! -d ${jre}/lib/${proc}/"$vmtype" && ! -h ${jre}/lib/${proc}/"$vmtype" ]]; then
    echo "Warning: $vmtype Java VM not present in ${jre}/lib/${proc}/${vmtype}"
    echo "- 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" ]]; then
    default=`/usr/bin/grep -v '^#' "${jre}/lib/jvm.cfg" | /usr/bin/head -1`
    vmtype=`echo ${default} | /usr/bin/cut -c 2-`
    if [[ "$vmtype" != "classic" && ! -d ${jre}/lib/${proc}/"$vmtype" ]]; then
    	vmtype=classic
    fi
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 [[ "${proc}" != "IA64" && "${vmtype}" = "classic" ]]; then
    # JAGab75626: Invoke PA_RISC1.1 binary, which is not EXEC_MAGIC.
    vmproc=PA_RISC
else
    vmproc=${proc}
fi

# Set SHLIB_PATH here to include path of chosen libjvm.
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.
    # The launcher expects the VM type as first option.
    exec $DEBUG_PROG "$prog" "${options[@]}" "$@"
else
    echo >&2 "$progname was not found in ${prog}"
    exit 1
fi
