
. sysdef_int.sh

readonly HELP_SYS="sysdef.hlp"
readonly HELP_SYS_MODEL="sysdef_model.hlp"
readonly HELP_SYS_CPU="sysdef_cpu.hlp"

TMP_MODEL_ID=""
TMP_CPU_ID=""
TMP_HOSTNAME=""
TMP_PASSWD=""


sysdef_setup()
{
	local old_PS3="$PS3"
	local item=""
	local -i done=0

	sysdef_restore
	MDB_init_database

	PS3="[Enter number] "
	while test $done -eq 0; do
		mhdr "SYSTEM DEFINITION SETUP"
		
		select item in "Shell" "Help" "Back" "Save" "List" \
				"System Model" "System CPU" "Hostname" \
				"Root Password"; do
			case "$item" in
			"Shell")         invoke_shell ;;
			"Help")          show_help "$HELP_SYS" ;;
			"Back")          sysdef_back; done=1 ;;
			"Save")          sysdef_save; done=1 ;;
			"List")          sysdef_list ;;
			"System Model")  sysdef_model ;;
			"System CPU")    sysdef_cpu ;;
			"Hostname")      sysdef_hostname ;;
			"Root Password") sysdef_password ;;
			esac
			break
		done
	done

	PS3=$old_PS3
}

sysdef_restore()
{
	eval TMP_MODEL_ID=\${${XID_SYSTEM}_MODEL_ID}
	eval TMP_CPU_ID=\${${XID_SYSTEM}_CPU_ID}
	eval TMP_HOSTNAME=\${${XID_SYSTEM}_HOSTNAME}
	eval TMP_PASSWD=\${${XID_SYSTEM}_PASSWD}
}

sysdef_back()
{
	eval ${XID_SYSTEM}_MODEL_ID=\"$TMP_MODEL_ID\"
	eval ${XID_SYSTEM}_CPU_ID=\"$TMP_CPU_ID\"
	eval ${XID_SYSTEM}_HOSTNAME=\"$TMP_HOSTNAME\"
	eval ${XID_SYSTEM}_PASSWD=\"$TMP_PASSWD\"
}

sysdef_save()
{
	: # nothing to do
}

sysdef_list()
{
	local model=""
	local model_id=""
	local cpu=""
	local cpu_id=""
	local host=""
	local pass=""
	local tmp=""

	eval model_id=\${${XID_SYSTEM}_MODEL_ID}
	eval cpu_id=\${${XID_SYSTEM}_CPU_ID}
	eval host=\${${XID_SYSTEM}_HOSTNAME}
	eval tmp=\${${XID_SYSTEM}_PASSWD}

	model=`MDB_get_system_name_by_id "$model_id"`
	cpu=`MDB_get_cpu_by_id "$model_id" "$cpu_id"`
	pass=${tmp:+<set>}

	ihdr "SYSTEM DEFINITIONS"
	msg  "System Model   : ${model:-<unset>}"
	msg  "System CPU     : ${cpu:-<unset>}"
	msg  "Hostname       : ${host:-<unset>}"
	msg  "Root Password  : ${pass:-<unset>}"
}

sysdef_model()
{
	local old_IFS="$IFS"
	local old_PS3="$PS3"
	local item=""
	local model=""
	local -i idx=0
	local -i done=0
	local -a models

	#
	#
	#
	IFS=$'\n'
	models=(`MDB_list_system_names`)
	IFS=$old_IFS

	PS3="[Enter number] "
	while test $done -eq 0; do
		ihdr "SELECT SYSTEM MODEL"

		select item in "Shell" "Help" "Back" "<ACTUAL>" "${models[@]}"
		do
			case "$item" in
			"Shell")    invoke_shell ;;
			"Help")     show_help "$HELP_SYS_MODEL" ;;
			"Back")     done=1 ;;
			"<ACTUAL>") model="__ACTUAL__"; done=1 ;;
			*)          model="$item"; done=1 ;;
			esac
			break
		done
		#
		#
	done
	PS3=$old_PS3

	#
	#
	if test $REPLY -eq 4; then
		eval ${XID_SYSTEM}_MODEL_ID=\"$model\"
		eval ${XID_SYSTEM}_CPU_ID=\"__ACTUAL__\"	# CPU ID follows
	elif test $REPLY -ge 5; then
		idx=$(($REPLY - 5))
		eval ${XID_SYSTEM}_MODEL_ID=\`MDB_get_system_id_by_index '$idx'\`
		eval ${XID_SYSTEM}_CPU_ID=\"\"	# becomes invalid w/ new model
	fi
}

sysdef_cpu()
{
	local old_IFS="$IFS"
	local old_PS3="$PS3"
	local item=""
	local cpu=""
	local cpus=""
	local model=""
	local model_id=""
	local tmp=""
	local -i idx=0
	local -i done=0
	local -a cpus

	eval model_id=\${${XID_SYSTEM}_MODEL_ID}
	model=`MDB_get_system_name_by_id $model_id`

	#
	#
	tmp=`MDB_list_cpus $model_id`	# unmodified IFS!
	IFS=$'\n'
	cpus=(`echo "$tmp"`)
	IFS=$old_IFS

	PS3="[Enter number] "
	while test $done -eq 0; do
		ihdr "SELECT SYSTEM CPU"
		msg "Selected system model: ${model:-<none>}\n"

		select item in "Shell" "Help" "Back" "<ACTUAL>" "${cpus[@]}"; do
			case "$item" in
			"Shell")    invoke_shell ;;
			"Help")     show_help "$HELP_SYS_CPU" ;;
			"Back")     done=1 ;;
			"<ACTUAL>") cpu="__ACTUAL__"; done=1 ;;
			*)          cpu="$item";  done=1 ;;
			esac
			break
		done
		#
		#
	done
	PS3=$old_PS3

	#
	#
	if test $REPLY -eq 4; then
		eval ${XID_SYSTEM}_CPU_ID=\"$cpu\"
	elif test $REPLY -ge 5; then
		idx=$(($REPLY - 5))
		eval ${XID_SYSTEM}_CPU_ID=\`MDB_get_cpu_id_by_index '$model_id' '$idx'\`
	fi
}

sysdef_hostname()
{
	local host=""
	
	host=`input_field "Enter hostname" validate_hostname` || return

	eval ${XID_SYSTEM}_HOSTNAME=\"$host\"
}

sysdef_password()
{
	local pass=""

	pass=`input_field "Enter password" "" 1` || return
	
	eval ${XID_SYSTEM}_PASSWD=\"$pass\"
}
