#!/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=" Nic properties ${jname}:${nic_path} "
	hline=

	local prompt="${_desc}"
	local sqldelimer=" "

	eval $( cbsdsqlro ${jailsysdir}/${jname}/local.sqlite SELECT id,nic_driver,nic_slot,nic_type,nic_parent,nic_hwaddr,nic_address,nic_mtu,nic_persistent,nic_ratelimit,nic_flags FROM qemunic WHERE id=\"${nic}\" AND jname=\"${jname}\" | while read nic_id nic_driver nic_slot nic_type nic_parent nic_hwaddr nic_address nic_mtu nic_persistent nic_ratelimit nic_flags; do
		echo "export nic_id=\"${nic_id}\""
		echo "export nic_driver=\"${nic_driver}\""
		echo "export nic_slot=\"${nic_slot}\""
		echo "export nic_type=\"${nic_type}\""
		echo "export nic_parent=\"${nic_parent}\""
		echo "export nic_hwaddr=\"${nic_hwaddr}\""
		echo "export nic_address=\"${nic_address}\""
		echo "export nic_mtu=\"${nic_mtu}\""
		echo "export nic_persistent=\"${nic_persistent}\""
		echo "export nic_ratelimit=\"${nic_ratelimit}\""
		echo "export nic_flags=\"${nic_flags}\""
	done ) || err 1 "${N1_COLOR}Error while create NIC map${N0_COLOR}"

	# 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_hwaddr'	'${nic_hwaddr}'		'NIC Mac address. 0 - auto'
		'nic_type'	'${nic_type}'		'Select NIC type'
		'nic_address'	'${nic_address}'	'Assign IPs on this nic (hoster side)'
		'nic_mtu'	'${nic_mtu}'		'NIC MTU. 0 - auto'
		'nic_flags'	'${nic_flags}'		'NIC flags'
	" # END-QUOTE

	if [ "${qemu_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}
		'-'		'-'			''
		'remove'	'remove'		'Detach and remove 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 ))

	# 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 nic"
MYOPTARG=""
MYDESC="Edit properties for vitual image of VM"
CBSDMODULE="qemu"

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

set -e
. ${globalconf}
set +e

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

. ${subrdir}/bsdconfig.subr
. $BSDCFG_LIBE/$APP_DIR/include/messages-qemunic.subr

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

. $BSDCFG_LIBE/$APP_DIR/include/qemunic.subr
. ${subrdir}/qemu.subr

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

. ${subrdir}/qemu.subr
. ${subrdir}/settings-tui.subr

readconf qemu-default-default.conf		# qemu_have_net_ratelimit?

#
# 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|size|nic_type)
					continue
					;;
				"nic_driver")
					get_nic_driver && update_nic_driver ${nic_id}
					;;
				"nic_address")
					ip4_addr="${nic_address}"
					get_construct_ip4_addr && nic_address="${ip4_addr}" && update_nic_address ${nic_id}
					;;
				"nic_hwaddr")
					get_construct_nic_hwaddr && update_nic_hwaddr ${nic_id}
					;;
				"nic_mtu")
					get_construct_nic_mtu && update_nic_mtu ${nic_id}
					;;
				"nic_parent")
					case "${nic_driver}" in
						vtnet)
							if get_construct_interface -s "vboxnet lo xnb" -b 0 -d 1 -m 1 -v 1 -c ${nic_parent} -n 1; then
								nic_parent="${interface}"
								update_nic_parent ${nic_id}
							fi
							;;
						vale)
							get_vale_switch && update_nic_parent ${nic_id}
							;;
					esac
					;;
				"remove")
					if getyesno "Detach and remove NIC from ${jname} VM?"; then
						qemu_remove_nic "${nic_id}"
						f_die
					fi
					;;
				"nic_persistent")
					invert_checkbox ${mtag} && update_nic_persistent ${nic_id}
					;;
				"nic_ratelimit")
					get_construct_nic_ratelimit && update_nic_ratelimit ${nic_id}
					;;
				"nic_flags")
					get_construct_nic_flags && update_nic_flags ${nic_id}
					;;
				*)
					;;
			esac
			;;
		${DIALOG_HELP})
			get_help
			continue
			;;
		*)
			f_die
			;;
	esac
done

return $SUCCESS

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