
. swset_int.sh

readonly HELP_SWSET="swset.hlp"

TMP_SEL=""
TMP_SRC=""
declare -a ALL_SWSETS


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

	swset_restore

	PS3="[Enter number] "
	while test $done -eq 0; do
		mhdr "SOFTWARE SETS"
		
		select item in "Shell" "Help" "Back" "Save" "List" "Add" \
				"Remove" "Set Source" "Rescan Source"; do
			case "$item" in
			"Shell")         invoke_shell ;;
			"Help")          show_help "$HELP_SWSET" ;;
			"Back")          swset_back; done=1 ;;
			"Save")          swset_save; done=1 ;;
			"List")          swset_list ;;
			"Add")           swset_add ;;
			"Remove")        swset_remove ;;
			"Set Source")    swset_set_src ;;
			"Rescan Source") swset_rescan_src ;;
			esac
			break
		done
	done

	PS3=$old_PS3
}

swset_restore()
{
	eval TMP_SEL=\${${XID_SWSET}_SEL}
	eval TMP_SRC=\${${XID_SWSET}_SRC}
}

swset_back()
{
	eval ${XID_SWSET}_SEL=\"$TMP_SEL\"
	eval ${XID_SWSET}_SRC=\"$TMP_SRC\"
}

swset_save()
{
	: # nothing to do
}

swset_list()
{
	local src=""

	eval src=\${${XID_SWSET}_SRC}

	ihdr "SELECTED SOFTWARE SETS"

	msg "Software Set source: ${src:-<none>}\n"

	list_swsets_sel
}

swset_add()
{
	local old_PS3="$PS3"
	local src=""
	local sel=""
	local item=""
	local swset=""
	local -i done=0

	eval src=\${${XID_SWSET}_SRC}
	eval sel=\${${XID_SWSET}_SEL}

	PS3="[Enter number] "
	while test $done -eq 0; do
		ihdr "ADD SOFTWARE SET"

		msg "Software Set source: ${src:-<none>}\n"

		select item in "All" "Done" ${ALL_SWSETS[*]}; do
			case "$item" in
			"All")  swset="${ALL_SWSETS[*]}"; done=1 ;;
			"Done") done=1 ;;
			*)      swset="$item"; done=1 ;;
			esac
			break
		done
	done

	PS3=$old_PS3

	sel=`sort_list "$sel $swset"`	# add and remove duplicates
	eval ${XID_SWSET}_SEL=\"$sel\"
}

swset_remove()
{
	local old_PS3="$PS3"
	local sel=""
	local item=""
	local swset=""
	local -i done=0

	eval sel=\${${XID_SWSET}_SEL}

	PS3="[Enter number] "
	while test $done -eq 0; do
		ihdr "REMOVE SOFTWARE SET"

		select item in "All" "Done" $sel; do
			case "$item" in
			"All")  clear_swset_sel; done=1 ;;
			"Done") done=1 ;;
			*)      swset="$item"; done=1 ;;
			esac
			break
		done
	done

	PS3=$old_PS3

	sel=`echo $sel | sed -e "s/\<$swset\>//g" 2>/dev/null`
	eval ${XID_SWSET}_SEL=\"$sel\"
}

swset_set_src()
{
	local src=""

	src=`input_field "Enter source" validate_swset_src` || return
	eval ${XID_SWSET}_SRC=\"$src\"

	swset_rescan_src
	if test $? -eq 0; then
		clear_swset_sel
	fi
}

swset_rescan_src()
{
	local src=""
	local lst=""
	local pn=""	# List file pathname
	local -i ret=1

	eval src=\${${XID_SWSET}_SRC}

	lst=`get_swlist_file "$src" "$RUNTIMEDIR"`
	if test $? -eq 0; then
		pn="$RUNTIMEDIR/$lst"
		
		if test -r "$pn"; then	# safety check
			ALL_SWSETS=`get_available_swset_names "$pn"`
			ret=$?
		else
			error "$pn: List file not found"
		fi
	else
		msg "Failed retrieving List file"
	fi

	return $ret
}

clear_swset_sel()
{
	eval unset -v ${XID_SWSET}_SEL
}

list_swsets_sel()
{
	local sel=""
	local s=""

	eval sel=\${${XID_SWSET}_SEL}

	for s in $sel; do
		echo " $s"
	done

	if test -z "$s"; then
		msg "No Software Sets selected at this time."
	fi
}
