#! /bin/sh -p

#
# $Id: java_wrapper_bsd.sh,v 1.4 2001/10/01 11:44:43 agc Exp $
#
# 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 -s` in
    OpenBSD)
        proc=`/usr/bin/uname -m`
        ;;
    *)
        proc=`/usr/bin/uname -p`
        ;;
esac

# Debug versions of the tools use libjava_g.so
if [ "x`/usr/bin/basename ${PRG}`" = "x`/usr/bin/basename ${PRG} _g`_g" ]; then
    JAVALIB=libjava_g.so
else
    JAVALIB=libjava.so
fi

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

APPHOME=`dirname "$PRG"`
if [ "x." = "x${APPHOME}" ]; then
    APPHOME="../bin"
fi
APPHOME=`dirname "$APPHOME"`
export APPHOME
JREHOME=$APPHOME/jre

# ld.so runtime patching might not be thread safe?
LD_BIND_NOW=true
export LD_BIND_NOW

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

# Select vm type (if classic vm, also select thread type).
unset vmtype
unset ttype
DEFAULT_THREADS_FLAG=green
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

# 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 for thread type, VM type and standard native libraries.
JAVA_LIBRARY_PATH="${jre}/lib/${proc}/${ttype}:${jre}/lib/${proc}/${vmtype}:${jre}/lib/${proc}${JAVA_LIBRARY_PATH}"
if [ -z "$LD_LIBRARY_PATH" ]; then
    LD_LIBRARY_PATH="${JAVA_LIBRARY_PATH}"
else
    LD_LIBRARY_PATH="${JAVA_LIBRARY_PATH}:$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
