#!/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 _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 [ "${xen_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

	local _height _width _rows
	eval f_dialog_menu_with_help_size _height _width _rows \
		\"\$_title\"  \
		\"\$_btitle\" \
		\"\$_prompt\" \
		\"\$_hline\"  \
		$_menu_list

	_height=$(( _height + 1 ))

	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"
	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="xen"

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-xennic.subr
. $BSDCFG_LIBE/$APP_DIR/include/messages-xennic.subr

. ${subrdir}/rcconf.subr
[ $? -eq 1 ] && err 1 "${N1_COLOR}No such VM: ${N2_COLOR}${jname}${N0_COLOR}"
[ "${emulator}" != "xen" ] && err 1 "${N1_COLOR}Not in xen mode${N0_COLOR}"

. $BSDCFG_LIBE/$APP_DIR/include/xennic.subr

. ${subrdir}/xen.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_xennic "
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"

readconf xen-default-default.conf		# xen_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})
			f_dialog_menutag_fetch _mtag
			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="xennic-cfgnic jname=${jname} nic=${nic}"
					;;
			esac
			;;
		${DIALOG_HELP})
			get_help
			continue
			;;
		*)
			f_die
			;;
	esac
done

return $SUCCESS

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