#!/bin/ksh -p
# $Id$
#

# Explorer Version:
EXP_VERSION=4.0

# Get and set the EXP_HOME and EXP_LIB variables
#
DIR=${0%/*}
if [ "${DIR}" = "/usr/sbin" ]
then
    EXP_HOME="/"
    EXP_LIB="/usr/lib/explorer"
    EXP_DEFAULTS="/etc/default/explorer"
else
    # Make DIR absolute path
    if [ `echo ${DIR} | cut -c1` != "/" ]
    then
        DIR=`pwd`
    fi
    EXP_HOME=${DIR%/*}
    EXP_LIB="${EXP_HOME}/lib"
#    EXP_DEFAULTS="${EXP_HOME}/etc/default/explorer"
    EXP_DEFAULTS="${EXP_ETC}/default/explorer"
fi

EXP_LIB=${EXP_HOME}/lib

# Source tools functions
. ${EXP_LIB}/exp_tools

# Source j2se functions
. ${EXP_LIB}/exp_j2se

# Function definitions first.

# initialize the variables that are used throughout the script
typeset -i i=0
typeset -i CONFIG_CHANGED=0

ANSWER=""
DIR_ENTRY=""
EDIT_CONFIG=""
EDITOR=""
ENTRY=""
INST=""
INSTALLED=""
RESPONSE=""
RESPONSE2=""
DIR=""
DIR_GOOD=""
TEXT=""
VAR=""

aelvx () {

    while :
    do
	screen_footer

        # Read response
        read RESPONSE

	# convert to lowercase
	typeset -l ${ANSWER}

        # Acceptable responses are a|add|e|edit|l|list|v|validate|x|exit
        case "${RESPONSE}" in
            add | a)
                RESPONSE=add
                break
                ;;
            edit | e)
                RESPONSE=edit
                break
                ;;
            list | l)
                RESPONSE=list
                break
                ;;
            validate | v)
		RESPONSE=val
		break
		;;	
	    exit | x)
		RESPONSE=exit
		break
		;;
            *)
                print "Please answer a, add, e, edit, l, list, v, validate, or x, or exit"
                continue
                ;;
        esac
    done

    # Set variable
    eval $1=${RESPONSE}
}

validate_dir () {

	NOT_FOUND=0
	(( v_pos = 11 ))
	(( h_pos = 0 ))
	position_cursor
	clear_below
	# Validate the directory entry.
	if [[ -z ${DIR} ]]
	then
		TEXT="You did not enter anything, do you want to exit?"
		line_length=`print ${#TEXT}`
#		vert_center
		horiz_center
		(( h_pos = h_pos - 4 ))
		position_cursor
		yorn RESPONSE "${TEXT} \c"
		if [[ "${RESPONSE}" == yes ]]
		then
			exit 1
		else
			(( h_pos = 0 ))
			position_cursor
			clear_line
			continue
		fi
	else
		if [[ ! -d ${DIR} ]]
		then
			TEXT="The server directory you entered is invalid!"
			line_length=`print ${#TEXT}`
#			vert_center
			horiz_center
			position_cursor
			print "${TEXT}"
			TEXT="Do you wish to try again?"
			line_length=`print ${#TEXT}`
#			vert_center
			horiz_center
			(( h_pos = h_pos - 4 ))
			line_increment 1
			position_cursor
			yorn RESPONSE "${TEXT} \c"
			if [[ "${RESPONSE}" == yes ]]
			then
				TEXT="Enter the j2se root directory that you would like"
				line_length=`print ${#TEXT}`
				horiz_center
				line_decrement 2
				position_cursor
				clear_below
				line_increment 2
				position_cursor
				print "${TEXT}"
				TEXT="to add to the configuration file:"
				line_length=`print ${#TEXT}`
				horiz_center
				line_increment 1
				position_cursor
				print "${TEXT}"
				line_increment 1
				(( h_pos = 0 ))
				position_cursor
				read DIR
				validate_dir ${DIR}
			else
				NOT_FOUND=1
			fi
		else
			# Check to see if the directory entered already 
			# exists in the config file
			IFS="="
			i=0
			while (( ${i} < ${#j_array[*]} ))
			do
				if [[ ${j_array[${i}]} == ${DIR} ]]
				then
					TEXT="That j2se directory root already exists in the config file!"
					line_length=`print ${#TEXT}`
					vert_center
					horiz_center
					position_cursor
					print "${TEXT}"
					line_increment 1
					position_cursor
					TEXT="Press \"Return\" to continue: "
					print "${TEXT}\c"
					read JUNK
					return 1
				fi
			(( i = i + 1 ))
			done
		fi
	fi

	if [[ ${NOT_FOUND} == 0 ]]
	then
		j_array[${i}]=${DIR}
		TEXT="The j2se directory root entered appears to be valid!"
		line_length=`print ${#TEXT}`
		vert_center
		horiz_center
		position_cursor
		print "${TEXT}"
		CONFIG_CHANGED=1
		save_config
	fi
}


copy_jconfig () {

	(( v_pos = 1 ))
	(( h_pos = 0 ))
	position_cursor
	clear_below
	if [[ -f ${EXP_JCONFIG} && -r ${EXP_JCONFIG} ]]
	then
		cp ${EXP_JCONFIG} ${EXP_ETC}/j2se.config > /dev/null 2>&1
		if (( $? == 0 ))
		then
			TEXT="Config file copied successfully!"
			line_length=`print ${#TEXT}`
			vert_center
			horiz_center
			position_cursor
			print "${TEXT}"
		else
			TEXT1="I'm sorry, there was a problem copying the config file"
			TEXT2="to the ${EXP_ETC} directory."
			TEXT3="Check permissions, or try and determine why the copy"
			TEXT4="did not work."
			TEXT5="Exiting due to a fatal error."
			line_length=`print ${#TEXT1}`
			vert_center
			horiz_center
			line_decrement 2
			position_cursor
			print "${TEXT1}"
			line_length=`print ${#TEXT2}`
			vert_center
			horiz_center
			line_decrement 1
			position_cursor
			print "${TEXT2}"
			line_length=`print ${#TEXT3}`
			vert_center
			horiz_center
			position_cursor
			print "${TEXT3}"
			line_length=`print ${#TEXT4}`
			vert_center
			horiz_center
			line_increment 1
			position_cursor
			print "${TEXT4}"
			line_length=`print ${#TEXT5}`
			vert_center
			horiz_center
			line_increment 2
			position_cursor
			print "${TEXT5}"
			(( v_pos = $term_lines ))
			(( h_pos = 0 ))
			position_cursor
			exit 1
		fi
		validate_jconfig ${EXP_JCONFIG}
		(( v_pos = 0 ))
		(( h_pos = 0 ))
		position_cursor
		clear_below
		screen_header
		screen_footer
	else
		TEXT="The config file entered is either NOT a file or is NOT readable."
		line_length=`print ${#TEXT}`
		vert_center
		horiz_center
		line_increment 1
		position_cursor
		print "${TEXT}"
		TEXT="Do you want to try again?"
		line_length=`print ${#TEXT}`
		vert_center
		horiz_center
		(( h_pos = h_pos - 4 ))
		position_cursor
		yorn RESPONSE "${TEXT} \c"
		if [[ ${RESPONSE} == yes ]]
		then
			read EXP_JCONFIG
			copy_jconfig ${EXP_JCONFIG}
		else
			TEXT="Do you want to exit?"
			line_length=`print ${#TEXT}`
			vert_center
			horiz_center
			(( h_pos = h_pos - 4 ))
			postion_cursor
			yorn RESPONSE "${TEXT} \c"
			if [[ ${RESPONSE} == yes ]]
			then
				exit 1
			fi
		fi
	fi
}

add_edit_list_validate_exit () {

	SaveIFS=${IFS}
	aelvx RESPONSE
	case ${RESPONSE} in
	edit)
		# Edit the config file
		# re-load the array after the config file has been edited.
		# Write out the config file
		if [[ ${CONFIG_CHANGED} == 1 ]]
		then
			save_config
		fi

		# Clear the array!
		unset j_array
		if [[ -n ${EDITOR} ]]
		then
			${EDITOR} ${EXP_JCONFIG}
		else
			vi ${EXP_JCONFIG}
		fi
		validate_jconfig ${EXP_JCONFIG}
		CONFIG_CHANGED=1
		(( v_pos = 0 ))
		(( h_pos = 0 ))
		position_cursor
		clear_below
		screen_header
		screen_footer
		;;
	add)
		(( v_pos = 0 ))
		(( h_pos = 0 ))
		position_cursor
		clear_below
		screen_header
		# Add entries to the config file, 
		# verifying those entries as we go along.
		(( v_pos = 0 ))
		(( h_pos = 0 ))
		line_increment 1
		position_cursor
		clear_below
		line_increment 2
		position_cursor
		TEXT="Please enter the directory for the"
		line_length=`print ${#TEXT}`
		vert_center
		horiz_center
		position_cursor
		print "${TEXT}"
		TEXT="j2se installation that you would like to add:"
		line_length=`print ${#TEXT}`
		line_increment 1
		horiz_center
		position_cursor
		print "${TEXT}"
		line_increment 1
		(( h_pos = 0 ))
		position_cursor
		read DIR
		validate_dir ${DIR}
		;;
	list)
		validate_jconfig ${EXP_JCONFIG}
		i=0
		j=0
		screen_header
		(( v_pos = 1 ))
		(( h_pos = 0 ))
		position_cursor
		clear_below
		while (( ${i} < ${#j_array[*]} ))
		do
			(( v_pos = 2 ))
			while (( $v_pos < (( $term_lines - 4 )) ))
			do
				if [[ ${i} = ${#j_array[*]} ]]
				then
					break
				fi
				(( h_pos = 0 ))
				position_cursor
				print "${j}) ${j_array[${i}]}" # print the mumber and the array element
				(( i = i + 1 )) # increment i
				(( j = j + 1 )) # increment j
				line_increment 1
			done
			(( v_pos = 3 ))
			(( h_pos = 40 ))
			while (( $v_pos < (( $term_lines - 4 )) ))
			do
				if [[ ${i} = ${#j_array[*]} ]]
				then
					break
				fi
				position_cursor
				print "${j}) ${j_array[${i}]}" # print the mumber and the array element
				(( i = i + 1 )) # increment i
				(( j = j + 1 )) # increment j
				line_increment 1
			done
			if (( ${i} < ${#j_array[*]} ))
			then
				if [[ ${i} = ${#j_array[*]} ]]
				then
					break
				fi
				# Too many lines for this screen
				(( h_pos = 0 ))
				line_increment 1
				position_cursor
				clear_below
				TEXT="Press enter for next page..."
				line_length=`print ${#TEXT}`
				horiz_center
				print "${TEXT}"
				read ENTER
				(( v_pos = 0 ))
				(( h_pos = 0 ))
				position_cursor
				clear_below
				screen_header
			fi
		done
		screen_footer
		;;
	val)
		validate_jconfig ${EXP_JCONFIG}
		if [[ $? == 0 ]]
		then
			screen_header
			screen_footer
			clear_below
			TEXT="j2se Config file verified!"
			line_length=`print ${#TEXT}`
			vert_center
			horiz_center
			position_cursor
			print "${TEXT}"
		else
			screen_header
			screen_footer
			clear_below
			TEXT="ERROR, j2se Config file error!"
			line_length=`print ${#TEXT}`
			vert_center
			horiz_center
			position_cursor
			print "${TEXT}"
		fi
		;;
	*)
		# Write out the config file
		if [[ ${CONFIG_CHANGED} == 1 ]]
		then
			save_config
		fi
		exit
		;;
	esac
	add_edit_list_validate_exit
}

save_config () {

	# Write out the config file
	if [[ -f ${EXP_JCONFIG} ]]
	then
		# Write out the changes if any
		mv $EXP_JCONFIG ${EXP_JCONFIG}.`date -u +\%Y\%m\%d\%H\%M\%S`
	fi
		
	i=0
	IFS=${SaveIFS}
	EXP_JCONFIG="${EXP_ETC}/j2se.config"
	while (( ${i} < (( ${#j_array[*]} )) ))
	do	
		echo "${j_array[${i}]}" >> ${EXP_JCONFIG}
		(( i = i + 1 ))
	done
	IFS=${SaveIFS}
}

# Screen Tools
clear_screen () {

	# Clear the dynamic area of the screen, leaving the top line and bottom 3 lines...
	(( v_pos = 0 ))
	(( h_pos = 0 ))
	position_cursor
	term_counter=1
	(( bottom = (( $term_cols - 4 )) ))
	while (( $term_counter < $bottom ))
	do
		tput cup $term_counter $h_pos
		clear_line
		(( term_counter = term_counter + 1 ))
	done

}

screen_header () {

	# Clear the screen, start fresh!
	clear
	TEXT="Explorer/j2se Configuration Editor"
	line_length=`print ${#TEXT}`
	horiz_center
	(( v_pos = 0 ))
	position_cursor
	clear_below
	print ${TEXT}

}

screen_footer () {

	TEXT=`gettext "Do you want to (a)dd entries to the file via command line,\n(e)dit the file in an editor, (l)list the config file,\n(v)alidate the file, or e(x)it? [a,e,l,v,x] "`
        # Display prompt
	(( v_pos = (( $term_lines - 3 )) ))
	(( h_pos = 0 ))
	position_cursor
        print "${TEXT} \c"

}

vert_center () {

	(( v_pos = (( $term_lines / 2 )) ))

}

horiz_center () {

	(( h_pos = (( $term_cols - $line_length )) / 2 ))

}

line_increment () {

	(( v_pos = (( v_pos + $1 )) ))

}

line_decrement () {

	(( v_pos = (( v_pos - $1 )) ))
	
}

clear_below () {
	
	tput ed

}

clear_line () {
	
	tput el

}

position_cursor () {
	
	tput cup $v_pos $h_pos

}

bold () {
	
	tput bold

}

init_scr () {
	
	tput sgr0

}

# Main routine. Checks for config file, then allows for add, edit, list, or exit of the program.

term_lines=`tput lines`
term_cols=`tput cols`

# Clear the screen, start fresh!
clear
screen_header

# Offer to display the README file!
TEXT="Do you wish to view the README file?"
TEXT2="This file will explain what the j2se config file is,"
TEXT3="what it is used for, and how this program works."
line_length=`print ${#TEXT}`
vert_center
horiz_center
line_decrement 2
position_cursor
clear_line
print ${TEXT}
line_length=`print ${#TEXT2}`
horiz_center
line_increment 1
position_cursor
clear_line
print ${TEXT2}
line_length=`print ${#TEXT3}`
horiz_center
(( h_pos = h_pos - 4 ))
line_increment 1
position_cursor
clear_line
yorn README "${TEXT3} \c"
(( h_pos = 0 ))
position_cursor
clear_line
line_decrement 1
(( h_pos = 0 ))
position_cursor
clear_line
line_decrement 1
(( h_pos = 0 ))
position_cursor
clear_line
if [[ "${README}" == yes ]]
then
	/usr/bin/more ${EXP_HOME}/doc/README.j2se_config
	(( v_pos = 0 ))
	(( h_pos = 0 ))
	position_cursor
	clear_below
	screen_header
fi

EXP_JCONFIG=${EXP_ETC}/j2se.config
# Check for existing j2se Config File
if [ -f "${EXP_JCONFIG}" ]
then
	EXP_JCONFIG=${EXP_ETC}/j2se.config

	# Validate the current entries in the config file
	(( v_pos = 0 ))
	(( h_pos = 0 ))
	position_cursor
	clear_below
	screen_header
	screen_footer

	validate_jconfig ${EXP_JCONFIG}
	if [[ $? = 0 ]]
	then
		TEXT=`gettext "The j2se config file was found and appears to to valid!"`
		line_length=`print ${#TEXT}`
		vert_center
		horiz_center
		position_cursor
		bold
		print ${TEXT}
		init_scr
	fi

	while :
	do
		add_edit_list_validate_exit
	done


else
	# Confirm that j2se is installed.
	TEXT="j2se config file could not be found!"
	TEXT2="Are you sure you have j2se software installed?"
	line_length=`print ${#TEXT}`
	vert_center
	horiz_center
	position_cursor
	bold
	print "${TEXT}"
	line_length=`print ${#TEXT2}`
	horiz_center
	(( h_pos = h_pos - 4 ))
	line_increment 1
	position_cursor
	yorn INSTALLED "${TEXT2} \c"
	init_scr
	(( h_pos = 0 ))
	position_cursor
	clear_line
	line_decrement 1
	(( h_pos = 0 ))
	position_cursor
	clear_line
	if [[ "${INSTALLED}" == yes ]]
	then
		TEXT="I cannot find the config file in the appropriate place."
		line_length=`print ${#TEXT}`
		horiz_center
		position_cursor
		clear_line
		bold
		print "${TEXT}"
		init_scr
		TEXT="Do you want to create a new one?"
		line_length=`print ${#TEXT}`
		horiz_center
		(( h_pos = h_pos - 4 ))
		line_increment 1
		position_cursor
		clear_line
		position_cursor
		bold
		yorn RESPONSE "${TEXT} \c"
		init_scr
		if [[ ${RESPONSE} == no ]]
		then
			TEXT="Do you want to enter a different location for the config file? \c"
			line_length=`print ${#TEXT}`
			horiz_center
			(( h_pos = h_pos - 4 ))
			line_increment 1
			position_cursor
			clear_line
			position_cursor
			yorn RESPONSE "${TEXT} \c"
			init_scr
			if [[ ${RESPONSE} == yes ]]
			then
				(( v_pos = 1 ))
				(( h_pos = 0 ))
				position_cursor
				clear_below
				TEXT="After you enter the config filename, It will be verified, then copied"
				line_length=`print ${#TEXT}`
				vert_center
				horiz_center
				position_cursor
				print "${TEXT}"
				TEXT="to the correct location. Enter the full pathname of the config file: "
				line_length=`print ${#TEXT}`
				horiz_center
				line_increment 1
				position_cursor
				print "${TEXT}"
				line_increment 1
				(( h_pos = 0 ))
				position_cursor
				read EXP_JCONFIG
				copy_jconfig ${EXP_JCONFIG}
				add_edit_list_validate_exit
			else
				(( v_pos = 1 ))
				(( h_pos = 0 ))
				position_cursor
				clear_below
				TEXT="Exiting since there is not an appropriate config file."
				line_length=`print ${#TEXT}`
				vert_center
				horiz_center
				position_cursor
				tput blink
				print "${TEXT}"
				init_scr
				(( v_pos = $term_lines ))
				(( h_pos = 0 ))
				position_cursor
				exit 1
			fi
		else
			add_edit_list_validate_exit
		fi
	else
		(( v_pos = 1 ))
		(( h_pos = 0 ))
		position_cursor
		clear_below
		TEXT="Exiting since j2se is not installed."
		line_length=`print ${#TEXT}`
		vert_center
		horiz_center
		position_cursor
		tput smul
		bold
		print "${TEXT}"
		tput rmul
		init_scr
		(( v_pos = $term_lines ))
		(( h_pos = 0 ))
		position_cursor
		exit 1
	fi
fi
