#!/bin/sh
#
# Copyright(c) 1999 Sun Microsystems, Inc. All rights reserved
#
#ident  "@(#)esm_setup.sh 1.15     99/10/08 SMI"
#
#
# 	This script is intended to be sources as part of other scripts
#	startup.  It will provide variables necessary to start stations
#       and user interfaces.
#
#	Usage
#		in sh/ksh
#			. prog_name
#		in csh
#			source prog_name
#
# 	Environment Variable Overrides are:
#		ESM_HOME
#			Location of root of tree (e.g /opt/SUNWesm)
#		ESM_RT_HOME
#			Location of root of config tree (e.g /)
#		ESM_JAVA_HOME
#			Location of Java Run Time 
#		ESM_CLASSPATH_OVERRIDE
#			Override discovered classpath with this
#		ESM_CLASSPATH_PRE
#			Prepend discovered classpath with this 
#                       if ESM_CLASSPATH_OVERRIDE is not specified
#		ESM_CLASSPATH_POST
#			Append discovered classpath with this
#                       if ESM_CLASSPATH_OVERRIDE is not specified
#		ESM_JAVA_ARG
#			Optional arguments to add the the Java execution
#		ESM_FOREGROUND
#			Change the model of execution for those tasks
#                       which are set to execute in the background to 
#                       execute in the foreground.  This is provided 
#                       mainly for use within test engines.
#		ESM_THREAD_MODEL
#			Set the thread model as desired
#
# 	Environment Variable Modified are:
#	        LD_LIBRARY_PATH
#                       The LD_LIBRARY_PATH is prepended with the 
#                       path to our libraries
#
#	Environment variables that are exported
#		_ESM_HOME
#			Location of the root of the tree
#		_ESM_RT_HOME
#			Location of the root of the config tree
#		_ESM_JAVA_HOME
#			Location to find the java executable
#		_ESM_JAVA_CLASSPATH
#			The classpath for execution
#		_ESM_JAVA_START
#			The command line to call the java executable with
#			the classpath set.
#
#       Return:
#		0 on success
#		1 or greater on failure
#

progname=esm_setup

PATH=/usr/bin:/usr/sbin:/sbin:/usr/ucb/bin
PKGINFO=pkginfo
RTPACKAGE=SUNWesmrt
PACKAGE=SUNWesm
cwd=`pwd`
ERROR_CONDITION=0

# Allow exiting of the script
script_exit ()
{
    ERROR_CONDITION=$1

    # Unset common variables
    unset TEXTDOMAIN
    unset TEXTDOMAINDIR

    exit $ERROR_CONDITION
}

# Set the location of the run-time relocation root of the tree
set_esmrt_home ()
{
    # _ESM_RT_HOME attempts to pick up the information from the package
    # database
    _ESM_RT_HOME=`${PKGINFO} -r $RTPACKAGE 2> /dev/null`

    if [ "$_ESM_RT_HOME" = "" ]
    then
	_ESM_RT_HOME=/
    fi

    _ESM_RT_HOME=${ESM_RT_HOME:-$_ESM_RT_HOME}
    export _ESM_RT_HOME
}

# Set the location of the root of the tree 
set_esm_home ()
{
    # ESM_HOME is picked up from the environment.  
    # _ESM_HOME is set from the command line.

    # If we find _ESM_HOME do not bother to set it
    if [ "$_ESM_HOME" = "" ]
    then
        # _RELOCATE_DIR attempts to pick up the information from the package
        # database
        _RELOCATE_DIR=`${PKGINFO} -r $PACKAGE 2> /dev/null`
        if [ "$_RELOCATE_DIR" != "" ]
        then
	    _RELOCATE_DIR=${_RELOCATE_DIR}/SUNWesm
        fi

        # _RELOCATE_DEF is a last attempt default
        _RELOCATE_DEF=/opt/SUNWesm
    
        # Set the Location of the root of the tree 
        _ESM_HOME=${ESM_HOME:-${_RELOCATE_DIR:-$_RELOCATE_DEF}}
        export _ESM_HOME

        # Unset the variables we are no longer using
        unset _RELOCATE_DIR
        unset _RELOCATE_DEF

        if [ ! -d $_ESM_HOME ]
        then
	    echo "$progname: \c" >&2
            gettext $TEXTDOMAIN "Unable to locate root directory" >&2
	    echo " $_ESM_HOME" >&2
	    script_exit 1
        fi

    fi # [ "$_ESM_HOME" != "" ]
}

# Set the LD_LIBRARY_PATH
set_library_path ()
{
    # Set LD_LIBRARY_PATH to include our default location
    if  [ "$LD_LIBRARY_PATH" = "" ]
    then
	LD_LIBRARY_POST=
    else
	LD_LIBRARY_POST=:${LD_LIBRARY_PATH}
    fi
    # Check to see if we are already on the path.  If so do nothing.
    echo $LD_LIBRARY_PATH | grep ${_ESM_HOME} > /dev/null 2>&1
    if [ $? -ne 0 ]
    then
        LD_LIBRARY_PATH=${_ESM_HOME}/lib${LD_LIBRARY_POST}
        export LD_LIBRARY_PATH
    fi

    # Unset local variables not longer needed
    unset LD_LIBRARY_POST
}

# Check java version and execution status
check_java_status()
{

    if [ ! -x "$1" ]
    then
	return 1
    fi

    _VERSION=`$1 -version 2>&1 | tail -1 | sed s/,// | awk '{print $4}'`

    
    case x$_VERSION in
     xSolaris_JDK_1.2*) return 0 ;;
     *) return 1 ;;
    esac

}

# find the proper java bin
find_java_bin ()
{

    # 
    # Check the JRE package
    #
    tmp=`${PKGINFO} -r SUNWj2rt 2> /dev/null `
    if [ "$tmp" != "" ]
    then
	if [ $tmp = / ]
	then
	    _TMP_JAVA_HOME=${tmp}java1.2/jre/bin/java
	else
	    _TMP_JAVA_HOME=${tmp}/java1.2/jre/bin/java
	fi
    fi
    
    if check_java_status $_TMP_JAVA_HOME
    then
	_ESM_JAVA_HOME=$_TMP_JAVA_HOME
	return
    fi
    
    
    #
    # Finally check some default locations
    #
    _TMP_JAVA_HOME=/usr/java1.2/jre/bin/java
    
    if check_java_status $_TMP_JAVA_HOME
    then
    	_ESM_JAVA_HOME=$_TMP_JAVA_HOME
	return
    fi
    

    #
    # If you get this far, things have failed
    #
    
    unset _ESM_JAVA_HOME
    echo "$progname: \c" >&2
    gettext $TEXTDOMAIN "Unable to locate executable Java Runtime" >&2
    echo " $_JAVA_RT_BIN" >&2
    script_exit 3

}
	
# Set up environment to use proper java bin
set_java_home()
{    
    
    _ESM_JAVA_HOME=""

    # If esm java home is set in environment, use that and do no checks
    if [ -s "$ESM_JAVA_HOME" ]
    then
	_ESM_JAVA_HOME=$ESM_JAVA_HOME
	return
    fi

    find_java_bin

    export _ESM_JAVA_HOME
    # Guarantee that java home is not being taken from the external environment
    JAVA_HOME="${_ESM_JAVA_HOME}/../.."
    export JAVA_HOME
    # Unset the JAVA_COMPILER variable
    set JAVA_COMPILER=jit
    export JAVA_COMPILER

    # Unset unused variables
    unset ESM_JAVA_HOME
    unset JAVA_RT_DIR

}

# set the thread model
set_thread_model ()
{
    _ESM_THREAD_MODEL=${ESM_THREAD_MODEL:-native}
    export _ESM_THREAD_MODEL
}

# Start the station
set_command_line ()
{
    # Location to look for java jar files
    _ESM_CLASS_LOCATION=$_ESM_HOME/lib/classes
    # Initialize classpath to null
    _TREE_CLASSPATH=$_ESM_CLASS_LOCATION
    # Memory configuration
    _ESM_JVM_MEMORY=-Xss524288

    # Allow the user to complete override the _ESM_JAVA_CLASSPATH Found
    if [ x$ESM_CLASSPATH_OVERRIDE != x ]
    then
	_ESM_JAVA_CLASSPATH=$ESM_CLASSPATH_OVERRIDE
	echo "$progname: \c" >&2
	gettext $TEXTDOMAIN "Warning: Overriding CLASSPATH with" >&2
	echo " $ESM_CLASSPATH_OVERRIDE" >&2
    else
	FILES=`/bin/ls ${_ESM_CLASS_LOCATION}/*.jar`
	_TREE_CLASSPATH=$_TREE_CLASSPATH:`echo $FILES | sed -e 's/ /:/g'`
    
        # Add local classpath requirements here
        # Pick up ESM_CLASSPATH_PRE from the environment
        # Pick up ESM_CLASSPATH_POST from the environment
        _ESM_JAVA_CLASSPATH=
        if [ x$ESM_CLASSPATH_PRE != x ]
        then
            _ESM_JAVA_CLASSPATH=$ESM_CLASSPATH_PRE:
        fi
        _ESM_JAVA_CLASSPATH=$_ESM_JAVA_CLASSPATH$_TREE_CLASSPATH
        if [ x$ESM_CLASSPATH_POST != x ]
        then
            _ESM_JAVA_CLASSPATH=$_ESM_JAVA_CLASSPATH:$ESM_CLASSPATH_POST
        fi

    fi # [ x$ESM_CLASSPATH_OVERRIDE != x ]

    export _ESM_JAVA_CLASSPATH


    # if _ESM_THEAD_MODEL exists prepend it with a "-"
    if [ "$_ESM_THREAD_MODEL" != "" ]
    then
	_ESM_THREAD_MODEL="-$_ESM_THREAD_MODEL"
    fi

    _ESM_JAVA_START="$_ESM_JAVA_HOME $_ESM_THREAD_MODEL -classpath $_ESM_JAVA_CLASSPATH "
    export _ESM_JAVA_START
    
    unset _ESM_CLASS_LOCATION
    unset _TREE_CLASSPATH
}

## main()

# Set the name of the message object file to be used for i18n
TEXTDOMAIN=$progname

# Set the location for the internationalization libraries
_TEXTDOMAINDIR=`pkgparam $PACKAGE BASEDIR 2> /dev/null ` 
if [ "$_TEXTDOMAINDIR" != "" ] 
then
    _TEXTDOMAINDIR=${_TEXTDOMAINDIR}/SUNWesm
fi
TEXTDOMAINDIR=${_TEXTDOMAINDIR:-${ESM_TEXTDOMAINDIR:-/usr/opt/SUNWesm}}/lib/locale
export TEXTDOMAINDIR

# Set the the variables that point to the root of the tree
set_esm_home 

# Set the the variables that point to the root of the relocatable tree
set_esmrt_home 

# Set LD_LIBRARY_PATH, if needed
set_library_path

# Set the thread model
set_thread_model

# Set the path to find the java executable
set_java_home

# Set up the java command line
set_command_line

unset TEXTDOMAIN
unset TEXTDOMAINDIR
