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

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

	local _btitle="$DIALOG_BACKTITLE"
	local _title=" ${jname} ${ctr}: New Controller "
	local _hline=

	local _prompt="${_desc}"

	local _menu_list="
		'type'		'${new_ctr_type}'	'Select controller type'
		'name'		'${new_ctr_name}'	'Name of controller'
		'count'		'${new_ctr_count}'	'Max Port Count'
		'cache'		'${new_ctr_cache}'	'Use Host I/O Cache'
		'-'		'-'			''
		'COMMIT'	'COMMIT'		'Create and attach new disk'
	" # END-QUOTE

	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="jname ctr"
CIXOPTARG=""
MYDESC="Add and attach new vitual image to VM"
CBSDMODULE="bhyve"

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

set -e
. ${globalconf}
set +e

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

. ${dialog}
. ${subrdir}/bsdconfig.subr
. ${BSDCFG_LIBE}/${APP_DIR}/include/messages-bhyve-dskcontroller.subr

. ${subrdir}/rcconf.subr
[ $? -eq 1 ] && err 1 "${N1_COLOR}No such VM: ${N2_COLOR}${jname}${N0_COLOR}"
[ "${emulator}" != "bhyve" ] && err 1 "${N1_COLOR}Not in bhyve mode${N0_COLOR}"

. ${subrdir}/bhyve.subr
. ${CIX_DISTDIR}/share/bhyve/bhyve-controller.subr

#load_ctr_info

# 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_add_bhyvectr "
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"

#
# Loop over the main menu until we've accomplished what we came here to do
#
while :; do

	if [ -z "${new_ctr_name}" -a -n "${new_ctr_type}" ]; then
		_tmp_new_ctr_type=$( echo ${new_ctr_type} | ${TR_CMD} '-' '_' )
		_tmp_new_ctr_type="${_tmp_new_ctr_type}_ctr"
		_freename=$( freectr jname=${jname} default_ctrname=${_tmp_new_ctr_type} )
		new_ctr_name="${_freename}"
	fi
	[ -z "${new_ctr_count}" ] && new_ctr_count="8"
	[ -z "${new_ctr_cache}" ] && new_ctr_cache="nocache"

	dialog_menu_main
	_ret=$?

	_command=
	case $_ret in
		${DIALOG_OK})
			f_dialog_menutag_fetch _mtag
			case "$_mtag" in
				?" $msg_exit")
					break
					;;
				"-"|slot|ctr_type)
					continue
					;;
				"name")
					get_ctr_name
					;;
				"type")
					get_ctr_type
					;;
				"count")
					get_ctr_count
					;;
				"cache")
					get_ctr_cache
					;;
				"COMMIT")
					if [ -z "${new_ctr_name}" -o -z "${new_ctr_type}" -o -z "${new_ctr_count}" -o -z "${new_ctr_cache}" ]; then
						f_dialog_msgbox "all field is mandatory"
						continue
					fi
					capture _res add_ctr -c ${new_ctr_count} -n "${new_ctr_name}" -t ${new_ctr_type} -z ${new_ctr_cache}
					if [ $? -ne 0 ]; then
						f_dialog_msgbox "Error: ${_res}"
					else
						f_die
					fi
					;;
				*)
					_ctr=${_mtag##* }
					_command="bhyvectr-cfgctr jname=${jname} ctr=${_ctr}"
					;;
			esac
			;;
		${DIALOG_HELP})
			get_help
			continue
			;;
		*)
			f_die
			;;
	esac
done

return $SUCCESS

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