#!/usr/local/bin/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"
f_mustberoot_init

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