#!/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} ${nvme}: New Controller "
	local _hline=

	local _prompt="${_desc}"

	local _menu_list="
		'devpath'	'${new_nvme_devpath}'	'/dev/blockdev or /path/to/image - path to device'
		'ram'		'${new_nvme_ram}'	'ram=size_in_MiB'
		'maxq'		'${new_nvme_maxq}'	'Max number of queues'
		'qsz'		'${new_nvme_qsz}'	'Max elements in each queue'
		'ioslots'	'${new_nvme_ioslots}'	'Max number of concurrent I/O equests'
		'sectsz'	'${new_nvme_sectsz}'	'Sector size (defaults to blockif sector size)'
		'ser'		'${new_nvme_ser}'	'Serial number with maximum 20 characters'
		'-'		'-'			''
		'COMMIT'	'COMMIT'	'Create and attach new NVMe'
	" # 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 nvme"
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-nvme.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-nvme.subr

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


capture _cur_nvme_count cbsdsqlro ${jailsysdir}/${jname}/local.sqlite 'SELECT COUNT(id) FROM bhyve_nvme'

if [ "${_cur_nvme_count}" != "0" ]; then
	f_dialog_msgbox "Sorry, this version support only one NVMe controller per guest"
	exit 0
fi

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

	if [ -z "${new_nvme_name}" -a -n "${new_nvme_type}" ]; then
		new_nvme_name="${nvme}_${new_nvme_type}"
	fi
	[ -z "${new_nvme_count}" ] && new_nvme_count="8"
	[ -z "${new_nvme_cache}" ] && new_nvme_cache="nocache"

	dialog_menu_main
	_ret=$?

	_command=
	case $_ret in
		${DIALOG_OK})
			f_dialog_menutag_fetch _mtag
			case "$_mtag" in
				?" $msg_exit")
					break
					;;
				"-"|slot|nvme_type)
					continue
					;;
				"devpath")
					get_nvme_devpath
					;;
				"ram")
					get_nvme_ram
					;;
				"maxq")
					get_nvme_maxq
					;;
				"qsz")
					get_nvme_qsz
					;;
				"ioslots")
					get_nvme_ioslots
					;;
				"sectsz")
					get_nvme_sectsz
					;;
				"ser")
					get_nvme_ser
					;;
				"COMMIT")
					if [ -z "${new_nvme_devpath}" ]; then
						f_dialog_msgbox "devpath field is mandatory"
						continue
					fi
					capture _res bhyve-nvme mode=attach jname=${jname} devpath="${new_nvme_devpath}" ioslots="${new_nvme_ioslots}" maxq="${new_nvme_maxq}" qsz="${new_nvme_qsz}" ram="${new_nvme_ram}" sectsz="${new_nvme_sectsz}" ser="${new_nvme_ser}"
					if [ $? -ne 0 ]; then
						f_dialog_msgbox "Error: ${_res}"
					else
						f_die
					fi
					;;
				*)
					_nvme=${_mtag##* }
					_command="bhyvenvme-cfgnvme jname=${jname} nvme=${_nvme}"
					;;
			esac
			;;
		${DIALOG_HELP})
			get_help
			continue
			;;
		*)
			f_die
			;;
	esac
done

return $SUCCESS

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