#!/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 btitle="$DIALOG_BACKTITLE"
	local title=" ${jname} ${ctr}: New Controller "
	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

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

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

set -e
. ${globalconf}
set +e

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

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

#
# 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
					_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
					;;
				*)
					index=${mtag%% *}
					ctr=${mtag##* }
					command="bhyvectr-cfgctr jname=${jname} ctr=${ctr}"
					;;
			esac
			;;
		${DIALOG_HELP})
			get_help
			continue
			;;
		*)
			f_die
			;;
	esac
done

return $SUCCESS

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