#!/usr/local/bin/cbsd
. ${subrdir}/nc.subr

############################################################ FUNCTIONS

# dialog_menu_main
#
# Display the dialog(1)-based application main menu.
#
dialog_menu_main()
{
	local _input _retval

	local btitle="$DIALOG_BACKTITLE"

	local title=" VHID list "
	hline=

	local prompt="${_desc}"
	local sqldelimer=" "

	local mark

	local menu_list=$( cbsdsqlro local "SELECT id,vhid,advskew,pass,interface,state FROM carp" | while read id vhid advskew pass interface state; do
		eval mark=\$vhid_selected_${id}
		if [ "${mark}" = "1" ]; then
			mark="X"
		else
			mark=" "
		fi
		echo "'${id}'	'[${mark}] vhid=${vhid} advskew=${advskew}'	'iface=${interface};pass=${pass};state=${pass}'"
	done ) || err 1 "${N1_COLOR}Error while create vhid map${N0_COLOR}"

	menu_list="${menu_list} '-'	'-'	''"

	local height width rows
	eval f_dialog_menu_with_help_size height width rows \
		\"\$title\"  \
		\"\$btitle\" \
		\"\$prompt\" \
		\"\$hline\"  \
		$menu_list

	height=$(( height + 1 ))

	# Obtain default-item from previously stored selection
	f_dialog_default_fetch defaultitem

	local menu_choice
	menu_choice=$( eval $DIALOG \
		--clear                                 \
		--title \"\$title\"                     \
		--backtitle \"\$btitle\"                \
		--hline \"\$hline\"                     \
		--item-help                             \
		--ok-label \"\$msg_ok\"                 \
		--cancel-label \"Remove\"               \
		--extra-button                          \
		--extra-label \"Add\"                   \
		--help-button				\
		--help-label \"Exit\"			\
		${USE_XDIALOG:+--help \"\"}             \
		--default-item \"\$defaultitem\"        \
		--menu \" \$prompt \"                   \
		$height $width $rows                    \
		$menu_list                              \
		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
	)

	local retval=$?
	f_dialog_data_sanitize menu_choice
	f_dialog_menutag_store "$menu_choice"

	# Only update default-item on success
	[ $retval -eq $DIALOG_OK ] && f_dialog_default_store "$menu_choice"
	return $retval
}

vhid_add()
{





}


vhid_remove()
{
	local vhids=

	for i in $( /usr/bin/seq 1 128 ); do
		eval mark=\$vhid_selected_${i}
		[ "${mark}" = "1" ] && vhids="${vhids} ${i}"
	done

	echo "REMOE VHIDS: ${vhids}"
	read p
}


############################################################ MAIN
export NOCOLOR=1

MYARG=""
MYOPTARG=""
MYDESC="Edit properties for vitual image of VM"
CBSDMODULE="bhyve"

globalconf="${distdir}/cbsd.conf";

set -e
. ${globalconf}
set +e

. ${subrdir}/nc.subr
. ${strings}
. ${tools}
. ${cbsdinit}

. ${subrdir}/bsdconfig.subr

# Incorporate rc-file if it exists
[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"

#
# Process command-line arguments
#
while getopts h$GETOPTS_STDARGS flag; do
	case "$flag" in
	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;;
	esac
done
shift $(( $OPTIND - 1 ))

#
# Initialize
#
f_dialog_title " VHIDs "
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init

#
# Loop over the main menu until we've accomplished what we came here to do
#
while :; do
	dialog_menu_main
	ret=$?

	case $ret in
		${DIALOG_OK})
			f_dialog_menutag_fetch mtag
			case "$mtag" in
				?" $msg_exit")
					break
					;;
				"-")
					continue
					;;
				*)
					vhid=${mtag%% *}
					# val=${mtag##* }
					eval mark=\$vhid_selected_${vhid}
					if [ "${mark}" = "1" ];then
						eval "vhid_selected_${vhid}=\"0\""
					else
						eval "vhid_selected_${vhid}=\"1\""
					fi
					;;
			esac
			;;
		${DIALOG_CANCEL})
			vhid_remove
			continue
			;;
		${DIALOG_EXTRA})
			vhid_add
			continue
			;;
		*)
			f_die
			;;
	esac
done

return $SUCCESS

################################################################################
# END
################################################################################
