#!/bin/sh

#
# java_wrapper_linux.sh	$Revision: 1.17 $ $Date: 2000/05/03 00:41:49 $
#
# 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.
#

PRG=$0
progname=`basename $0`

case "`uname -m`" in
    i[3-7]86 | ia32 | ia64)
        proc=i386
        ;;
    sparc*)
        proc=sparc
        ;;
    *)
        proc="`uname -m`"
        ;;
esac

# Resolve symlinks. See 4152645.
while [ -L "$PRG" ]; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/' > /dev/null; then
	PRG="$link"
    else
	PRG="`dirname $PRG`/$link"
    fi
done

APPHOME=`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

# Get the canonicalized absolute pathnames
jre="`$jre/bin/realpath $jre`"
APPHOME="`${jre}/bin/realpath $APPHOME`"
JREHOME="`${jre}/bin/realpath $JREHOME`"
export APPHOME JREHOME

# Select vm type (if classic vm, also select thread type).
unset vmtype
unset ttype
DEFAULT_THREADS_FLAG=native
if [ "x$1" = "x-hotspot" ]; then
    vmtype=hotspot
    ttype=native_threads
    shift 1
else
    if [ "x$1" = "x-classic" ]; then
	vmtype=classic
	ttype=${DEFAULT_THREADS_FLAG}_threads
	shift 1
    else
	if [ "x$1" = "x-green" ]; then
	    vmtype=classic
	    ttype=green_threads
	    shift 1
	else
	    if [ "x$1" = "x-native" ]; then
		vmtype=classic
		ttype=native_threads
		shift 1
	    else 
		if [ -d ${jre}/lib/${proc}/hotspot ]; then
		    vmtype=hotspot
		    ttype=native_threads
		else
		    vmtype=classic
		    if [ ${THREADS_FLAG:-${DEFAULT_THREADS_FLAG}} = native ]
		    then 
			ttype=native_threads
		    else
			ttype=green_threads
		    fi
		fi
	    fi
	fi
    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

# Set LD_LIBRARY_PATH for hotspot VM.
ld="${jre}/lib/${proc}/${ttype}:${jre}/lib/${proc}/${vmtype}:${jre}/lib/${proc}"
if [ -z "$LD_LIBRARY_PATH" ]; then
    LD_LIBRARY_PATH="$ld"
else
    LD_LIBRARY_PATH="$ld:$LD_LIBRARY_PATH"
fi
export LD_LIBRARY_PATH

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

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

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