#!/bin/sh

java_cmd=/usr/j2se/bin/java
awk_cmd=/usr/bin/awk
sed_cmd=/usr/bin/sed
cut_cmd=/usr/bin/cut
logfile=/tmp/.rsc.log

java_min_version='1.3.0'

#
# Function to run gettext with TEXTDOMAINDIR set.
# TEXTDOMAINDIR is not set in the exported environment so subprocesses 
# do not inadvertently use it.
#

textdomaindir=/opt/rsc/lib/locale
textdomain='SUNW_RSC_rsc'

pgettext () {
        gttext=gettext #Trick to fool shgettext
        TEXTDOMAINDIR=${textdomaindir} ${gttext} ${textdomain} "$1"
}



# Check Java version

check_version() {
    longversion=`${java_cmd} -version 2>&1`
#    echo "longversion=${longversion}"
    version=`echo "${longversion}" | ${awk_cmd} '/java version/ { print $3 }' | ${sed_cmd} -e 's/\"//g' -e 's/[A-Za-z_]*//'`
#    echo "version=${version}"
    major=`echo "${version}" | ${cut_cmd} -d. -f1`
    minor=`echo "${version}" | ${cut_cmd} -d. -f2`
    sub_minor=`echo "${version}" | ${cut_cmd} -d. -f3`
    sub_minor1=`echo "${sub_minor}" | ${cut_cmd} -d_ -f1`
    sub_minor2=`echo "${sub_minor}" | ${cut_cmd} -d_ -f2`
    sub_minor2_num=`echo "${sub_minor2}" | ${sed_cmd} -e 's/[A-Za-z]//'`
    sub_minor2_alpha=`echo "${sub_minor2}" | ${sed_cmd} -e 's/[0-9]*//'`
#    echo "major=${major}, minor=${minor}, sub_minor=${sub_minor}"
#    echo "sub_minor1=${sub_minor1}, sub_minor2=${sub_minor2}"
#    echo "sub_minor2_num=${sub_minor2_num}, sub_minor2_alpha=${sub_minor2_alpha}"

    version_ok=1
    if [ "$version" = "" ]; then
	eval echo "\"`pgettext 'Unable to start Java Runtime Environment.\n\nPlease ensure that the Java Runtime Environment (JRE) or\nJava Software Development Kit (JDK) is installed correctly\nand that all necessary Solaris patches have been installed.\n\nGo to http://www.sun.com/solaris/java to download Java\ncomponents.  Consult the JRE or JDK README file found in\nthe product distribution for more information on how to\ndownload and install the necessary patches for your version\nof the Solaris Operating Environment.'`\""
	exit 1

    elif [ ${major} -eq 1 ]				# check major#
    then
	if [ ${minor} -eq 3 ]				# check minor#
	then
	    if [ ${sub_minor1} -eq 0 ]			# check sub-minor#
	    then
		if [ ${sub_minor2_num} -eq 0 ]		# check sub-minor# after _
		then
#		    if [ ${sub_minor2_alpha} ]		# check for alphabetic portion
#		    then
#			if [ ${sub_minor2_alpha} -ge a ]
#			then
			    version_ok=0
#			    echo "Passed all checks!"
#			fi
#		    fi
		elif [ ${sub_minor2_num} -gt 0 ]
		then
		    version_ok=0
		fi
	    elif [ ${sub_minor1} -gt 0 ]
	    then
		version_ok=0
	    fi
	elif [ ${minor} -gt 3 ]
	then
	    version_ok=0
	fi
    elif [ ${major} -gt 1 ]
    then
	version_ok=0
    fi

    if [ ${version_ok} -ne 0 ]; then
	eval echo "\"`pgettext 'The version of Java installed on this machine is ${version}.\nThis is an unsupported version of Java.\nPlease exit and install version ${java_min_version} or later.\n'`\""

	exit 1
    fi


}


# Make sure user can display to xhost

check_xhost() {

    if [ X"$DISPLAY" = X ]; then
	DISPLAY=":0.0"
	export DISPLAY
    fi

    /usr/openwin/bin/xhost > /dev/null 2>&1

    if [ $? -ne 0 ]; then
	eval echo "\"`pgettext 'You do not have access to the display ${DISPLAY}.\nUse the xhost command to enable access.\n'`\""

        exit 1
    fi


}



if [ ! -f "${java_cmd}" ]
then
    java_cmd=/usr/java/bin/java
    if [ ! -f "${java_cmd}" ]
    then
	eval echo "\"`pgettext 'No Java Runtime Environment is installed on this machine.\nPlease exit and install version ${java_min_version} or later.\n'`\""
	exit 1
    fi
fi

if [ -x ${java_cmd} ]; then
    check_version
    check_xhost
    cd `dirname $0`/../lib

    CLASSPATH=java:java/jmapi-snmp.jar:.
    export CLASSPATH

    unset JAVA_HOME
    unset JDK_HOME

    # Make sure anyone can rewrite logfile
    touch $logfile
    chmod 666 $logfile 2> /dev/null

    # Need to I18N!!!
    pgettext "starting Sun Remote System Control\n"
    exec ${java_cmd} com.sun.rsc.internal.pages.HomePage > $logfile 2>&1
else
    # Need to I18N!!!
    pgettext "No Java Runtime Environment is installed on this machine.\nPlease exit and install version ${java_min_version} or later.\n"
    exit 1
fi
