#!/bin/sh -p
#
# @(#)src/tools/pfm/java_wrapper, hx131, ax131, 20040424 1.8.2.13
#
# IBM Linux version
#
Version() {						# ibm@42183
    # Return 1 for kernels 2.4.0 thru 2.4.9 on Intel x86
    Arch=`uname -m | sed -e 's/^i./i3/'`		# ibm@31190
    if [ "$Arch" = "i386" ]
    then
    	Pattern=`uname -r`
    	Kernel=`echo $Pattern | sed -e 's/^2\.4\..*/2.4./'`
    	if [ "$Kernel" = "2.4." ]				# ibm@51068
    	then
            Release=`echo $Pattern | sed -e 's/^2\.4\.\([0-9]*\).*/\1/'`
            if [ "$Release" -lt 10 ]			# ibm@51068
            then
            	return 1
	    fi
        fi
    fi
    return 0
}

unset ENV
PRG=`type -path "$0"` >/dev/null 2>&1			#ibm@41418
progname=${0##*/}  
suffix=$(expr "$progname" : '.*\(_.$\)')
JAVAHOME=${PRG%/*}/..

# Resolve symlinks. See 4152645.
while [ -L "$PRG" ]; do
    ls=`/bin/ls -ld "$PRG"`
    link=`/usr/bin/expr "$ls" : '.*-> \(.*\)$'`
    if /usr/bin/expr "$link" : '/' > /dev/null; then
        PRG="$link"
    else
        PRG=${PRG%/*}/$link
    fi
    PRG=`type -path "$PRG"` >/dev/null 2>&1		#ibm@41418
    JAVAHOME=${PRG%/*}/..
    progname=`basename "$PRG"`				#ibm@41418
done

# export JAVAHOME for GetPropertiesMD()
JAVAHOME=`""cd "$JAVAHOME"; pwd`			#ibm@41418
export JAVAHOME

JREHOME=$JAVAHOME/jre

# Where is the JRE in relation to the executable?
unset jre
if [ -f "${JREHOME}/bin/libjava${suffix}.so" ]; then
    jre="${JREHOME}"
fi
if [ -f "${JAVAHOME}/bin/libjava${suffix}.so" ]; then
    jre="${JAVAHOME}"
fi
if [ "x${jre}" = "x" ]; then
    echo "Error: can't find libjava${suffix}.so"
    exit 1
fi

#Start of ibm@60359,ibm@61067

_ibm_j9=0;

# Make sure that the argument is -Xj9

for arg
do
  case "$arg" in
  "-Xj9") _ibm_j9=1;;
  esac
done

if [ $_ibm_j9 -eq 1 ]
then
    export LD_LIBRARY_PATH=$jre/bin:$jre/bin/j9vm:$LD_LIBRARY_PATH
else
    export LD_LIBRARY_PATH=$jre/bin:$jre/bin/classic:$LD_LIBRARY_PATH 
fi


#End of ibm@60359,ibm@61067

prog="$JAVAHOME/bin/exe/${progname}"

# Disable floating stacks if the kernel version < 2.4.10	# ibm@42183
# It is assumed that exporting LD_ASSUME_KERNEL to 2.4.10 is safe both for RHAS 2.1 with floating stacks
# and RHEL 3.0 to disable NPTL.
# Start of ibm@65790

if [ -f /etc/redhat-release ]
then
    Version
    if [ $? -eq 1 ] && [ -z "$IBM_USE_FLOATING_STACKS" ]
    then
       export LD_ASSUME_KERNEL=2.2.5
    else
       export LD_ASSUME_KERNEL=2.4.10 
    fi
fi

# End of ibm@65790

# Start of ibm@72193
# Suse and other distributions seem to use the same technique for disabling NPTL

if [ -z "$LD_ASSUME_KERNEL" ] #If the LD_ASSUME_KERNEL is already not set
then
    getconf GNU_LIBPTHREAD_VERSION 2>&1 | grep NPTL > /dev/null 2>&1
    if [ $? -eq 0 ];then
        export LD_ASSUME_KERNEL=2.4.19
    fi
fi

# End of ibm@72193

# Disable the use of the LDT in the OpenUNIX LKP environment	# ibm@57930
if [ -x /unixware/usr/bin/uname ]
then
    if [ "`/unixware/usr/bin/uname`" = "OpenUNIX" ]
    then
	export IBM_NOLDT=1
    fi
fi

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