#! /bin/ksh

#
# A simple script to check if java is the right verion
# use echo to return the javapath
#

JRE1_4_0="1.4.0"
JRE1_4_1="1.4.1"
JRE1_4_2="1.4.2"

# 
# check if SUNWj3rt is download by checking SUNWj3rt dir
# if yes
#    set javapath = /usr/j2se
# if not
#    check if SUNWj3rt has been installed
#      if yes,
#         check version and;
#         set javapath = basedir
#      if not, 
#         check if /usr/java is the right version (for those using
#         self extrating version)
#         if is the right version
#             set javapath=/usr/java
#         if not; set javapath=""
#
 
ARG1=$1
ARG2=$2

if [ "X$ARG2" != "X" ];then
	ALTROOT="-R $ARG2"
else
	ALTROOT=""
fi 

#echo "ALTROOT=$ALTROOT"

if [ -d $ARG1/SUNWj3rt ]; then
    JREPATH=/usr/j2se
else
    /bin/pkginfo $ALTROOT -q SUNWj3rt
    FOUNDPKG=$?  
    if [ $FOUNDPKG -eq 1 ]; then 
	# check version path under /usr/java
	$ARG2/usr/java/bin/java -version 2> /tmp/javaversion.tmp.$$
#echo "VERSION1="
#cat /tmp/javaversion.tmp.$$
	CURR_JREVERSION=`cat /tmp/javaversion.tmp.$$ | grep "java version" | awk '{print $3}' | sed 's/-.*$/"/' | sed 's/_.*$/"/'`
	CURR_JREVERSION=${CURR_JREVERSION%\"}
	CURR_JREVERSION=${CURR_JREVERSION#\"}
	/bin/rm /tmp/javaversion.tmp.$$
	if [ "$CURR_JREVERSION" == "$JRE1_4_0" ] || [ "$CURR_JREVERSION" == "$JRE1_4_1" ] || [ "$CURR_JREVERSION" == "$JRE1_4_2" ]; then
	    JREPATH=/usr/java
	else
	    JREPATH=""
	fi;
    else
	JREPATH=`pkginfo $ALTROOT -l SUNWj3rt | grep BASEDIR | awk '{print $2}'`
	$ARG2$JREPATH/j2se/bin/java -version 2> /tmp/javaversion.tmp.$$
#echo "VERSION2="
#cat /tmp/javaversion.tmp.$$
	        CURR_JREVERSION=`cat /tmp/javaversion.tmp.$$ | grep "java version" | awk '{ print $3}' | sed 's/-.*$/"/' | sed 's/_.*$/"/'`
        CURR_JREVERSION=${CURR_JREVERSION%\"}
        CURR_JREVERSION=${CURR_JREVERSION#\"}
        /bin/rm /tmp/javaversion.tmp.$$
        if [ "$CURR_JREVERSION" == "$JRE1_4_0" ] || [ "$CURR_JREVERSION" == "$JRE1_4_1" ] || [ "$CURR_JREVERSION" == "$JRE1_4_2" ]; then
            JREPATH=/usr/j2se
        else    
            JREPATH=""
        fi;
    fi;
fi;


#echo $JREPATH > /tmp/jrepath.tmp
echo $JREPATH
