#!/usr/local/bin/cbsd
. ${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} ${dsk}: New Image "
	hline=

	local prompt="${_desc}"

	local menu_list="
		'size'		'${new_dsk_size}'	'Current disk image size. Resize currently is not supported'
		'controller'	'${new_dsk_controller}'	'Select controller type'
		'slot'		'0'			'Bus slot. 0 - auto'
		'dsk_type'	'vhd'			'Disk type ( vhd, vmdk, vdi, qcow )'
		'-'		'-'			''
		'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

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

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

set -e
. ${globalconf}
set +e

. ${subr}
. ${strings}
. ${tools}
init $*

. ${dialog}
. ${workdir}/bsdconfig.subr
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages-bhyvedsk.subr

. ${jrcconf}
[ $? -eq 1 ] && err 1 "${MAGENTA}No such VM: ${GREEN}${jname}${NORMAL}"
[ "${emulator}" != "bhyve" ] && err 1 "${MAGENTA}Not in bhyve mode${NORMAL}"

. $BSDCFG_LIBE/$APP_DIR/include/bhyvedsk.subr

. ${workdir}/bhyve.subr

#load_dsk_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_bhyvedsk "
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
	ret=$?

	command=
	case $ret in
		${DIALOG_OK})
			f_dialog_menutag_fetch mtag
			case "$mtag" in
				?" $msg_exit")
					break
					;;
				"-"|slot|dsk_type)
					continue
					;;
				"controller")
					get_dsk_controller
					;;
				"size")
					get_dsk_size
					;;
				"COMMIT")
					if [ -z "${new_dsk_size}" -o -z "${new_dsk_controller}" ]; then
						f_dialog_msgbox "disk size and disk controller field is mandatory"
						continue
					fi
					_res=$( add_dsk -c "${new_dsk_controller}" -d "${dsk}" -s "${new_dsk_size}" )
					if [ $? -ne 0 ]; then
						f_dialog_msgbox "Error: ${_res}"
					else
						f_die
					fi
					;;
				*)
					index=${mtag%% *}
					dsk=${mtag##* }
					command="bhyvedsk-cfgdsk jname=${jname} dsk=${dsk}"
					;;
			esac
			;;
		${DIALOG_HELP})
			get_help
			continue
			;;
		*)
			f_die
			;;
	esac
done

return $SUCCESS

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