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

# dialog_menu_main
#
# Display the dialog(1)-based application main menu.
#
dialog_menu_main()
{
	local _title="$DIALOG_TITLE"
	local _btitle="$DIALOG_BACKTITLE"
	local _prompt=""
	local _menu_list="
		'A $msg_show_remote_images'	''	'cbsd repo action=list sources=img'
		'B $msg_show_remote_vm'		''	'cbsd repo action=list sources=vm'
		'C $msg_show_remote_bases'	''	'cbsd repo action=list sources=base'
		'D $msg_show_remote_kernel'	''	'cbsd repo action=list sources=kernel'
	" # END-QUOTE
	local _defaultitem=
	local _hline="$hline_arrows_tab_enter"

	local _f_dialog_title=" CBSD Repository "

	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 $DIALOG_OK
}



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

. ${subrdir}/bsdconfig.subr
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages-repo.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_repo "
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 || break
	f_dialog_menutag_fetch _mtag

	[ -z "${_mtag}" ] && exit $SUCCESS

	_command=

	case "${_mtag}" in
		?" $msg_exit") break ;;
		?" $msg_show_remote_images")
			_command="repo-images"
			;;
		?" $msg_show_remote_vm")
			f_dialog_msgbox "Not Available Yet" && continue
			;;
		?" $msg_show_remote_bases")
			_command="repo-bases"
			;;
		?" $msg_show_remote_kernel")
			_command="repo-kernels"
			;;
		*)
			continue
			;;
	esac

	if [ "${_command}" ]; then
		$BSDCFG_LIBE/$APP_DIR/${_command} ${USE_XDIALOG:+-X}
	fi

done

return $SUCCESS

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