#!/bin/ksh -p
#  exp_sunone: sunone functions
#  $Id$

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

EXP_VERSION=4.0

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

#
# validate the entries in the sunone file
# server root entries will be first, then the instances
# the only required argument will be the sunone_config file
#
# the variable {i} will be a counter for the ENTRY{i} variable.
# {i} will increment with each server_root entry.
# the variable {i} will be a counter for the INST{j} variable.
# {j} will increment with each instance entry. The {j} variable
# will get reset upon a new server root 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 server_root is
	# invalid, the instances for this
	# server_root will also be invalid!
	logfile ${EXP_TARGET}/sunone/sunone_config.err

	# comment out the server type and server_root entry since 
	# they are invalid.
	if [[ ${i} > 0 ]]
	then
		(( i = i - 1 ))
	else
		(( i = 0 ))
	fi
	i_array[${i}]="# "${i_array[${i}]}
	(( i = i + 1 ))
	i_array[${i}]="# "${i_array[${i}]}
	CONFIG_CHANGED=1

	# since the server root is invalid, the instance
	# for that server root will also be invalid
	# we need to continue moving through the array
	# and comment out the instances for this server root
	while (( ${i} < ${cnt} ))
	do
		if [[ ${i_arry[${i}]} != "" ]]
		then
			i_array[${i}]="# "${i_array[${i}]}
			CONFIG_CHANGED=1
			(( i = i + 1 ))
			continue
		else
			break;
		fi
	done
	(( i = i - 1 ))
}

gather_instances () {

	# check to make sure there is an instance
	# for this server_root
	typeset -i j=0
	SaveIFS=${IFS}
	IFS=":"
	SR_ENTRY=${i_array[${i}]}
	while (( ${#i_array[${#}]} != 0 ))
	do
		if [[ ${SR_ENTRY[1]} == "/" ]]
		then
			(( j = j + 1 ))
			# validate this instance
			set -A INST_NAME ${i_array[${j}]}
			if [[ ! -d ${i_array[${j}]}"/"${INST_NAME[1]} ]]
			then
				# instance is not valid!
				# let the user know..
	
				i_array[${j}]="# "${i_array[${j}]}
				CONFIG_CHANGED=1
			fi
		fi
	(( j = j + 1 ))
	if [[ ${j} == ${cnt} ]]
	then
		break
	fi
	done
	IFS=${SaveIFS}
}

validate_iconfig () {

	# 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
		echo "No config file passed!"
		exit 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
		echo "Config file is not readable"
		exit 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
			i_array[${cnt}]=$line
			(( cnt = cnt + 1 ))
	done

	i=0
	while (( ${i} < ${cnt} ))
	do
		set -A ENTRY ${i_array[${i}]}
		case ${ENTRY} in
		Application)
			SERVER_TYPE=App
			(( i = i + 1 ))
			if [[ ! -d ${i_array[${i}]} ]]
			then
				remove_entries
			else
				gather_instances
			fi
			break
			;;
		Portal)
			SERVER_TYPE=portal
			(( i = i + 1 ))
			if [[ ! -d ${i_array[${i}]} ]]
			then
				remove_entries
			else
				gather_instances
			fi
			break
			;;
		Messaging)
			SERVER_TYPE=mail
			(( i = i + 1 ))
			if [[ ! -d ${i_array[${i}]} ]]
			then
				remove_entries
			else
				gather_instances
			fi
			break
			;;
		Web)
			SERVER_TYPE=web
			(( i = i + 1 ))
			if [[ ! -d ${i_array[${i}]} ]]
			then
				remove_entries
			else
				gather_instances
			fi
			break
			;;
		Directory)
			SERVER_TYPE=ldap
			(( i = i + 1 ))
			if [[ ! -d ${i_array[${i}]} ]]
			then
				remove_entries
			else
				gather_instances
			fi
			break
			;;
		*)
			(( i = i + 1 ))
			continue
			;;
		esac

	(( i = i + 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 ${i_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}\""
}

handle_serverroot () {
	# This routine will take the pathname of the server root
	# 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
}
