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

[ -z "${CIX_BIN}" ] && CIX_BIN="/usr/local/bin/cbsd"

# 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 _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

CIXARG="node"
CIXOPTARG=""
MYDESC="Add node by ssh"
CBSDMODULE="bhyve"

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

set -e
. ${globalconf}
set +e

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

. ${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"

#
# 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
						${CIX_BIN} 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
################################################################################
