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

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

	local title="$DIALOG_TITLE"
	local btitle="$DIALOG_BACKTITLE"

	local f_dialog_title=" Properties for ${node} "
	hline=

	local menu_list="
		'summary'	'summary'	'Show node details'
		'-'		'-'		''
		'remove'	'remove'	'Remove node key and cache data'
	" # END-QUOTE

	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 \"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
}


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

MYARG="node"
MYOPTARG=""
MYDESC="Add node by ssh"
CBSDMODULE="bhyve"

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

set -e
. ${globalconf}
set +e

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

. ${subrdir}/bsdconfig.subr
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages-nodecfg.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 " $msg_configure_node "
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=$?

	command=
	case $ret in
		${DIALOG_OK})
			f_dialog_menutag_fetch mtag
			case "$mtag" in
				?" $msg_exit")
					break
					;;
				remove)
					if getyesno "Detach and remove ssh key and cache data for ${mtag} node?"; then
						/usr/local/bin/cbsd node mode=remove node="${node}"
						f_die
					fi
					;;
				summary)
					stats=$( ${MKTEMP_CMD} )
					trap "rm -f ${stats}" HUP INT ABRT BUS TERM EXIT
					cbsd node mode=show node=${node} > ${stats}
					f_show_help ${stats}
					;;
				*)
					continue
					;;
			esac
			;;
		${DIALOG_HELP})
			get_help
			continue
			;;
		*)
			f_die
			;;
	esac
done

return $SUCCESS

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