#!/bin/ksh -p
#  exp_j2se: j2se functions
#  $Id$

EXP_J2SE_VERSION='$Revision: #11 $'
export EXP_J2SE_VERSION

EXP_VERSION=4.0

if [ -n "${DO_VERSION}" ]
then
    LIB=${0##*/}
    echo "${LIB} version: ${EXP_J2SE_VERSION}"
    exit
fi

#
# validate the entries in the j2se.config file
#
# the variable {i} will be a counter for the ENTRY{i} variable.
# {i} will increment with each directory entry.

# The first step will be to load the file into an array, this
# will make it easier to search for entries.

remove_entries () {

	# directory entry for the j2se
	# installation is invalid, 
	logfile ${EXP_TARGET}/j2se/j2se_config.err

	# comment out the directory entry since 
	# it is invalid.
	if [[ ${i} > 0 ]]
	then
		(( i = i - 1 ))
	else
		(( i = 0 ))
	fi
	j_array[${i}]="# "${j_array[${i}]}
	(( i = i + 1 ))
	j_array[${i}]="# "${j_array[${i}]}
	CONFIG_CHANGED=1
	(( i = i - 1 ))
}

validate_jconfig () {

	# Set the variables that will be used in the script
	typeset -i CONFIG_CHANGED=0
	typeset -i NO_CONFIG=0
	typeset -i NOT_PASSED=0
	typeset -i i=0
	typeset -i j=0

	DIR_NAME=""
	SERVER_TYPE=""
	SaveIFS=${IFS}

	# Check to see of a config file was passed to the routine
	if [[ "${#}" = 0 ]]
	then
		NOT_PASSED=1
	fi

	# Check to see if the passed in config IS a file, and readable
	if [[ -f "${1}" && -r "${1}" ]]
	then
		NO_CONFIG=0
		continue
	else
		NO_CONFIG=1
	fi

	# open the config file and load into the array

	typeset -i cnt=0 	# initialize the counter as an integer and to zero

	exec 3< "$1"		# read the config file into file descriptor 3
	while read -u3 line	# read the file, one line at a time
	do
			j_array[${cnt}]=$line
			(( cnt = cnt + 1 ))
	done

	i=0
	while (( ${i} < ${cnt} ))
	do
		set -A ENTRY ${j_array[${i}]}
		if [[ -d ${j_array[${i}]} ]]
		then
			(( i = i + 1 ))
		else
			${j_array[${i}]} =""
		fi
		CONFIG_CHANGED=1
	done

	# Check if the config was modified, if so we need to write out a new file
	IFS=":"
	if [ ${CONFIG_CHANGED} == 1 ]
	then
		echo "Renaming existing config file to: ${1}.`date -u +\%Y\%m\%d\%H\%M\%S`"
		mv $1 ${1}.`date -u +\%Y\%m\%d\%H\%M\%S`

		echo "Updating config file"
		for elements in ${j_array[*]}
		do
			echo "${elements}" >> $1
		done
	fi		
	IFS=${SaveIFS}
}


yorn () {
    VAR=$"$1"

    while :
    do
        # Display prompt
        echo "$2"
        echo "[y,n] \c"

        # Read response
        read ANSWER

        # Acceptable responses are y|yes|n|no
        case `echo "${ANSWER}" | tr '[A-Z]' '[a-z]'` in
            yes | y)
                ANSWER=yes
                break
                ;;
            no | n)
                ANSWER=no
                break
                ;;
            *)
                echo "Please answer y, yes, n or no"
                continue
                ;;
        esac

    done

    # Set variable
    eval "$1=\"${ANSWER}\""
}

strip_dirname () {
	# This routine will take the pathname of a directory 
	# and remove the "/"'s from the pathname, replacing them
	# with "_"'s and leaving the begining "/" off.

	if [[ -d ${1} ]]
	then
		exit 1
	else
		NEW_SR=`echo $1 |sed 's/\//_/g'|sed 's/_//'`
	fi
}
