#!/usr/local/bin/cbsd
. ${subrdir}/nc.subr
. ${tools}
. ${dialog}
. ${strings}
. ${subrdir}/settings-tui.subr		# invert_checkbox

# dialog_menu_main
#
# Display the dialog(1)-based application main menu.
#
dialog_menu_main()
{
	local _input _retval _pref
	local _checkbox="nic_persistent" i _mytest

	local btitle="$DIALOG_BACKTITLE"
	local title=" ${jname} ${nic}: New NIC "
	hline=
	local defaultitem=

	local prompt="${_desc}"

	f_dialog_default_fetch defaultitem

	[ -z "${nic_driver}" ] && nic_driver="vtnet"
	[ -z "${nic_type}" ] && nic_type="0"
	[ -z "${nic_parent}" ] && nic_parent="auto"
	[ -z "${nic_hwaddr}" ] && nic_hwaddr="0"
	[ -z "${nic_address}" ] && nic_address="0"
	[ -z "${nic_persistent}" ] && nic_persistent="0"
	[ -z "${nic_flags}" ] && nic_flags="0"

	# checkbox mark
	for i in ${_checkbox}; do
		#eval _new_mytest=\$new_$i
		#[ -n "${_new_mytest}" ] && eval "${i}=${_new_mytest}"
		eval _mytest=\$$i
		if [ "${_mytest}" = "1" -o "${_mytest}" = "true" ]; then
			export ${i}_mark="X"
		else
			export ${i}_mark=" "
		fi
	done

	local menu_list="
		'nic_driver'	'${nic_driver}'		'NIC driver'
		'nic_parent'	'${nic_parent}'		'NIC parent interface. 0 - auto'
		'nic_type'	'${nic_type}'		'Select NIC type'
		'nic_hwadd'	'${nic_hwaddr}'		'Mac address. 0 - auto'
		'nic_address'	'${nic_address}'	'Assign IP address to nic (hoster side).  0 - no assign'
		'nic_flags'	'${nic_flags}'		'Additional NIC flags'
	" # END-QUOTE

	if [ "${bhyve_have_net_ratelimit}" = "1" ]; then
		menu_list="${menu_list}	'nic_ratelimit'	'${nic_ratelimit}'	'NIC bandwith ratelimit'"
	fi

	# persistent checkbox for tap interface only
	_pref3=$( substr --pos=0 --len=3 --str=${nic_parent} )
	if [ "${_pref3}" = "tap" ]; then
		#checkbox area
		for i in ${_checkbox}; do
			eval _mytest=\$${i}_mark
			[ -z "${_mytest}" ] && _mytest=" "
			#inc_menu_index item_let
			menu_list="${menu_list}	'${i}'	'[${_mytest}]'	'${_mydesc}'"
		done
	else
		nic_persistent=0
	fi

	menu_list="${menu_list}
		'-'		'-'			''
		'COMMIT'	'COMMIT'		'Create and attach new NIC'
	" # END-QUOTE

	cbsd_menubox_with_help
	retval=$?

	f_dialog_data_sanitize menu_choice
	f_dialog_menutag_store "$menu_choice"
	f_dialog_default_store "$menu_choice"

	return ${retval}
}


############################################################ MAIN
export NOCOLOR=1

MYARG="jname nic"
MYOPTARG=""
MYDESC="Add and attach new vitual NIC to VM"
CBSDMODULE="bhyve"

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

set -e
. ${globalconf}
set +e

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

. ${subrdir}/bsdconfig.subr
#f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages-bhyvenic.subr
. $BSDCFG_LIBE/$APP_DIR/include/messages-bhyvenic.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}"

. $BSDCFG_LIBE/$APP_DIR/include/bhyvenic.subr

. ${subrdir}/bhyve.subr
. ${subrdir}/vnet.subr

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

readconf bhyve-default-default.conf		# bhyve_have_net_ratelimit?
[ -z "${nic_ratelimit}" ] && nic_ratelimit="0"

#
# 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})
			case "$mtag" in
				?" $msg_exit")
					break
					;;
				"-"|slot|nic_type)
					continue
					;;
				"nic_driver")
					get_nic_driver
					;;
				"nic_hwaddr")
					get_construct_nic_hwaddr
					;;
				"nic_address")
					get_construct_ip4_addr && nic_address="${ip4_addr}"
					;;
				"nic_parent")
					interface="${nic_parent}"
					get_construct_interface -s "vboxnet lo xnb" -b 0 -d 1 -m 1 -v 1 -c ${nic_parent} -n 1 && nic_parent="${interface}" -g 1
					;;
				"nic_persistent")
					invert_checkbox ${mtag}
					#new_bootable="${bootable}"
					continue
					;;
				"nic_ratelimit")
					get_construct_nic_ratelimit
					;;
				"nic_flags")
					get_construct_nic_flags
					;;
				"COMMIT")
					if [ -z "${nic_type}" -o -z "${nic_driver}" -o -z "${nic_hwaddr}" ]; then
						f_dialog_msgbox "nic_type,nic_driver and nic_hwaddr field is mandatory"
						continue
					fi
					_res=$( add_nic -d "${nic_driver}" -r "${nic_ratelimit}" -t "${nic_type}" -h "${nic_hwaddr}" -a "${nic_address}" -p "${nic_parent}" -z "${nic_persistent}" )
					if [ $? -ne 0 ]; then
						f_dialog_msgbox "Error: ${_res}"
					else
						f_die
					fi
					;;
				*)
					index=${mtag%% *}
					nic=${mtag##* }
					command="bhyvenic-cfgnic jname=${jname} nic=${nic}"
					;;
			esac
			;;
		${DIALOG_HELP})
			get_help
			continue
			;;
		*)
			f_die
			;;
	esac
done

return $SUCCESS

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