#!/bin/sh

#
# @(#)java_wrapper_linux.sh	1.4 99/01/02
#
# 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-6]86)
        proc=i386
        ;;
    sparc*)
        proc=sparc
        ;;
    *)
        proc="`uname -m`"
        ;;
esac

# Resolve symlinks. See 4152645.
while [ -h "$PRG" ]; do
    ls=`/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
done

PPC_JAVA_HOME=/usr/local/jdk/cvs_1.2/jdk1.2/build/linux
export PPC_JAVA_HOME

APPHOME=`dirname "$PRG"`
APPHOME=`dirname "$APPHOME"`
export APPHOME
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 (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.
j=`$jre/bin/realpath $jre`
ld="${j}/lib/${proc}/${ttype}:${j}/lib/${proc}/${vmtype}:${j}/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

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
