#!/bin/ksh -x
#
# Explorer Version:
EXP_VERSION=4.0
#
# Source tools functions
. ${EXP_LIB}/exp_tools

# Source local functions
. ${EXP_LIB}/exp_j2se

script=${0##*/}
which_gate_exit $script default all

#  Collect j2se data.

# check for, and load the j2se.config file if it exists.
load_config ()
{
	# Check for ${EXP_ETC}/j2se.config file
	if [ ! -f ${EXP_ETC}/j2se.config ]
	then
	   	TEXT=`gettext "${EXP_ETC}/j2se.config file not found!"`
#  		logfile WARNING ${TEXT}
   		echo_file j2se/j2se.config "${EXP_ETC}/j2se.config file not found!"
	fi

	# validate the config file.
	# This routine validates the config file and
	# loads the items into the j_array
	if [ -f ${EXP_ETC}/j2se.config ]
	then
		validate_jconfig ${EXP_ETC}/j2se.config
	fi
}

# The j_array will either contain a listing of directories
# where java (j2se) is installed, or be empty. (no j2se.config file)

# Check for installed j2se packages
# compare the directory with the listings in the j_array,
# only add new entries, not existing ones.

load_config
j=0
i=0
J2_HOME[${j}]=`pkginfo -r SUNWj2rt 2> /dev/null`
if [ $? = 0 ]
then
	J2_HOME[${j}]="${J2_HOME[${j}]}/java"
	(( j = j + 1 ))
fi

J2_HOME[${j}]=`pkginfo -r SUNWj2dev 2> /dev/null`
if [ $? = 0 ]
then
	(( i = j - 1 ))
	if [ ${J2_HOME[${i}]} != "${J2_HOME[${j}]}/java" ]
	then
		J2_HOME[${j}]="${J2_HOME[${j}]}/java"
		(( j = j + 1 ))
	fi
fi

J2_HOME[${j}]=`pkginfo -r SUNWj2se 2> /dev/null`
if [ $? = 0 ]
then
	J2_HOME[${j}]="${J2_HOME[${j}]}/j2se"
	(( j = j + 1 ))
fi

J2_HOME[${j}]=`pkginfo -r SUNWj3rt 2> /dev/null`
if [ $? = 0 ]
then
	J2_HOME[${j}]="${J2_HOME[${j}]}/j2se"
	(( j = j + 1 ))
fi

# Check the newly found java installations with what is in the
# config file. If they match, only use one.
j=0
while (( ${j} < ${#J2_HOME[*]} ))
do
	i=0
	FOUND=0
	while (( ${i} < ${#j_array[${i}]} ))
	do
		if [[ ${J2_HOME[${j}]} = ${j_array[${i}]} ]]
		then
			FOUND=1
			break;
		else
			FOUND=0
			(( i = i + 1 ))
		fi
	done
	if [[ ${FOUND} = 0 ]]
	then
		if [[ ${#J2_HOME[${j}]} > 0 ]]
		then
			j_array[${i}]="${J2_HOME[${j}]}"
		fi
	fi
	(( j = j + 1 ))
done

# Check for default locations of j2se in the filesystem
k=0
if [[ -d /usr/java ]]
then
	DEF_LOC[${k}]="/usr/java"
	(( k = k + 1 ))
fi
if [[ -d /usr/j2se ]]
then
	DEF_LOC[${k}]="/usr/j2se"
	(( k = k + 1 ))
fi
if [[ -d /usr/jre ]]
then
	DEF_LOC[${k}]="/usr/jre"
	(( k = k + 1 ))
fi

# if we have default locations of j2se installed, ie. the directories exist.
# then compare those against what we currently have in the config file, or
# have found via pkginfo, and only add those that do not already exist.
i=0
j=0
k=0
FOUND=0
if [[ ${#j_array[*]} < 1 ]]
then
	while (( ${k} < ${#DEF_LOC[*]} ))
	do
		j_array[${k}]=${DEF_LOC[${k}]}
		(( k = k + 1 ))
	done
else
	while (( ${i} < ${#j_array[*]} ))
	do
		while (( ${k} < ${#DEF_LOC[*]} ))
		do
			if [ ${DEF_LOC[${k}]} = ${j_array[${i}]} ]
			then
				# do not add, break out of loop, we already found it.
				FOUND=1
				(( i = i + 1 ))
				break
			else
				(( k = k + 1 ))
			fi
			(( i = i + 1 ))
			if [[ ${i} > ${#j_arrray[*]} ]]
			then
				break
			fi
		done
		if [[ ${FOUND} = 0 ]]
		then
			(( i = ${#j_array[*]} ))
			j_array[${i}] = ${DEF_LOC1}
		fi
	done
fi
# Gather the j2se data

j=0
while (( ${j} < ${#j_array[*]} ))
do
	J2_HOME2[${j}]=`echo ${j_array[${j}]} | sed 's/\//_/g'|sed 's/_//'`
	if [ -d ${j_array[${j}]}/jre ]
	then
		get_cmd "${j_array[${j}]}/bin/java -version"		j2se/${J2_HOME2[${j}]}/j2se_version
		get_cmd "${j_array[${j}]}/bin/java -fullversion"		j2se/${J2_HOME2[${j}]}/j2se_fullversion

		get_file "${j_array[${j}]}/lib/security/java.policy"	j2se/${J2_HOME2[${j}]}
		get_file "${j_array[${j}]}/lib/security/java.security"	j2se/${J2_HOME2[${j}]}

		get_cmd "/usr/bin/sum ${j_array[${j}]}/lib/*"		j2se/${J2_HOME2[${j}]}/j2se_checksum
		(( j = j + 1 ))
	else
		get_cmd "${j_array[${j}]}/jre/bin/java -version"		j2se/${J2_HOME2[${j}]}/j2se_version
		get_cmd "${j_array[${j}]}/jre/bin/java -fullversion"		j2se/${J2_HOME2[${j}]}/j2se_fullversion

		get_file "${j_array[${j}]}/jre/lib/security/java.policy"	j2se/${J2_HOME2[${j}]}
		get_file "${j_array[${j}]}/jre/lib/security/java.security"	j2se/${J2_HOME2[${j}]}

		get_cmd "/usr/bin/sum ${j_array[${j}]}/jre/lib/*"		j2se/${J2_HOME2[${j}]}/j2se_checksum
		(( j = j + 1 ))
	fi
done
