#!/usr/bin/env 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 "
	local hline=

	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"
	[ -z "${nic_vlan_untagged}" ] && nic_vlan_untagged="0"
	[ -z "${nic_vlan_tagged}" ] && nic_vlan_tagged="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'
		'nic_vlan_untagged'	'${nic_vlan_untagged}'	'Untagged VLAN ID. 0 - disabled'
		'nic_vlan_tagged'	'${nic_vlan_tagged}'	'Tagged VLAN set (comma-sep). 0 - disabled'
	" # 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
	capture _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

CIXARG="jname nic"
CIXOPTARG=""
MYDESC="Add and attach new vitual NIC to VM"
CBSDMODULE="bhyve"

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

set -e
. ${globalconf}
set +e

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

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

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=$?
	f_dialog_menutag_fetch _mtag

	_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
					;;
				"nic_vlan_untagged")
					get_construct_nic_vlan_untagged
					;;
				"nic_vlan_tagged")
					get_construct_nic_vlan_tagged
					;;
				"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
					capture _res add_nic -d "${nic_driver}" -r "${nic_ratelimit}" -t "${nic_type}" -h "${nic_hwaddr}" -a "${nic_address}" -p "${nic_parent}" -z "${nic_persistent}" -u "${nic_vlan_untagged}" -T "${nic_vlan_tagged}"
					if [ $? -ne 0 ]; then
						f_dialog_msgbox "Error: ${_res}"
					else
						f_die
					fi
					;;
				*)
					_nic=${_mtag##* }
					_command="bhyvenic-cfgnic jname=${jname} nic=${_nic}"
					;;
			esac
			;;
		${DIALOG_HELP})
			get_help
			continue
			;;
		*)
			f_die
			;;
	esac
done

return $SUCCESS

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