#!/bin/sh

#
# $Id: j2sdk-config_linux.sh,v 1.2 2000/08/29 22:20:02 kreilede Exp $
#
# Copyright 1994-2000 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

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 >&2 "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

# Interpret THREADS_FLAG environment variable, if set.
DEFAULT_THREADS_FLAG=native
if [ "${THREADS_FLAG}" = green -o "${THREADS_FLAG}" = native ]; then
    THREADS_TYPE="${THREADS_FLAG}_threads"
elif [ "x${THREADS_FLAG}" = x ]; then
    THREADS_TYPE="${DEFAULT_THREADS_FLAG}_threads"
else
    echo >&2 "Error: \
              value \`${THREADS_FLAG}' of variable THREADS_FLAG is illegal."
    exit 1
fi

# Select HPI, and take note of VM type.  Note that the assignment below does
# not mean that the assigned values are the defaults for `ttype' and `vmtype'.
ttype=native_threads
vmtype=classic
needshift=yes
if [ "x$1" = "x-client" ]; then
    # Backdward compatibility: The betas used -hotspot
    # User wants HotSpot Client.  Replace -hotspot with -client.
    shift 1
    set -- -client "$@"
fi
if [ "x$1" = "x-classic" ]; then
    # User wants classic
    ttype="${THREADS_TYPE}"
elif [ "x$1" = "x-green" ]; then
    # User wants classic/green.  Replace -green with -classic.
    ttype=green_threads
    shift 1
    set -- -classic "$@"
elif [ "x$1" = "x-native" ]; then
    # User wants classic/native.  Replace -native with -classic.
    ttype=native_threads
    shift 1
    set -- -classic "$@"
elif grep "^$1\$" ${jre}/lib/jvm.cfg > /dev/null; then
    # User specified some VM type known to jvm.cfg.
    vmtype=`echo $1 | cut -c 2-`
else
    # User didn't specify a VM type, see if default is classic.
    default=`grep -v '^#' "${jre}/lib/jvm.cfg" | head -1`
    if [ "x$default" = "x-classic" ]; then
	ttype="${THREADS_TYPE}"
    fi
    vmtype=`echo ${default} | cut -c 2-`
    needshift=no
fi

if [ "${vmtype}" = "classic" ]; then
   if [ "${ttype}" = "green_threads" ]; then
      LD_BIND_NOW=yes
      export LD_BIND_NOW
   fi
    _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}/bin/realpath ${jre}/../lib/${proc}`"
fi
# Set LD_LIBRARY_PATH for libjvm.so (necessitated by ld.so bugid 4176579)
JAVA_LIBRARY_PATH="${jre}/lib/${proc}/${vmtype}${JAVA_LIBRARY_PATH}"

# Set LD_LIBRARY_PATH to find libawt.so etc.
JAVA_LIBRARY_PATH="${jre}/lib/${proc}:${jre}/lib/${proc}/${ttype}/:${JAVA_LIBRARY_PATH}"

LD_LIBRARY_PATH="${JAVA_LIBRARY_PATH}${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
export LD_LIBRARY_PATH

if [ "${needshift}" = yes ]
then
    shift 1
fi

usage ()
{
    echo >&2 "\
Usage: j2sdk-config [VMTYPE] [OPTIONS]
VM Type:
        [-hotspot]
        [-server]
        [-classic]
        [-native]
        [-green]
Options:
        [--javahome]
        [--version]
        [--ext-dir]
        [--cflags]
        [--libs]
        [--ld-library-path]"
    exit 1
}

testgreen ()
{
    if [ "${ttype}" = "green_threads" ]
    then
        echo >&2 "JNI with the classic green threads VM is not supported."
        exit 1
    fi
}

javahome=
version=
extdir=
cflags=
libs=
ldlibpath=

for i
do
    case $i in
        --javahome)
            javahome=yes
            ;;
        --version)
            version=yes
            ;;
        --ext-dir)
            extdir=yes
            ;;
        --cflags)
            cflags=yes
            ;;
        --libs)
            libs=yes
            ;;
        --ld-library-path)
            ldlibpath=yes
            ;;
        *)
            usage
            ;;
    esac
done
if [ -z "${javahome}${version}${extdir}${cflags}${libs}${ldlibpath}" ]
then 
    usage
fi

# Top level j2sdk directory
if [ "${javahome}" = yes ]
then
    echo ${APPHOME}
fi

# J2SE version
if [ "${version}" = yes ]
then
    exec "${APPHOME}/bin/${proc}/${ttype}/java" -${vmtype} -version
fi

# J2SE extension directory
if [ "${extdir}" = yes ]
then
    echo "${JREHOME}/lib/ext"
fi

# C flags
if [ "${cflags}" = yes ]
then
    testgreen
    echo "-D_REENTRANT -D_GNU_SOURCE -I${APPHOME}/include -I${APPHOME}/include/linux"
fi

# LD flags
if [ "${libs}" = yes ]
then
    testgreen
    if [ "${vmtype}" = classic ]
    then
        echo "-L${jre}/lib/${proc}/${vmtype} -L${jre}/lib/${proc}/${ttype} -ljvm -lhpi -lpthread"
    else
        echo "-L${jre}/lib/${proc}/${vmtype} -ljvm -lpthread"
    fi
fi

# Library path for the Invocation API
if [ "${ldlibpath}" = yes ]
then
    testgreen
    echo ${JAVA_LIBRARY_PATH}'${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}'
fi

exit 0
