#!/usr/bin/env cbsd
CIXARG=""
CIXOPTARG="jname"
MYDESC="CARP VHID passthrough TUI manager"
CBSDMODULE="bhyve"
EXTHELP="wf_bhyve"
ADDHELP="
${H3_COLOR}Description${N0_COLOR}:

 Text user interface for managing CARP VHID passthrough devices.
 Allows selecting VHIDs to attach/detach from a bhyve VM.

${H3_COLOR}Options${N0_COLOR}:

 ${N2_COLOR}jname=${N0_COLOR}     - target virtual machine name.

${H3_COLOR}Examples${N0_COLOR}:

 # cbsd bhyvecfg-passhtu-tui
 # cbsd bhyvecfg-passhtu-tui jname=myvm

${H3_COLOR}See also${N0_COLOR}:

   cbsd bhyve-ppt --help
   cbsd bconfig --help
   cbsd bset --help

"

. ${subrdir}/nc.subr

############################################################ FUNCTIONS

vhid_add()
{
	:
}

vhid_remove()
{
	local _i _vhids=

	for ((_i=1; _i<=128; _i++)); do
		eval mark=\$vhid_selected_${_i}
		[ "${mark}" = "1" ] && _vhids="${_vhids} ${_i}"
	done

	if [ -z "${_vhids}" ]; then
		f_dialog_msgbox "No VHID entries selected for removal" "Info"
		return 0
	fi

	f_dialog_yesno "Remove marked VHIDs:${_vhids}?" "Confirm removal"
	[ $? -ne ${DIALOG_OK} ] && return 0

	for _i in ${_vhids}; do
		eval "vhid_selected_${_i}=\"0\""
	done
}

# Display the VHID selection menu using cbsd_menubox_with_help
dialog_menu_main()
{
	local title=" VHID list "
	local btitle="$DIALOG_BACKTITLE"
	local prompt="${_desc}"
	local sqldelimer=" "
	local defaultitem= hline= mark=

	local menu_list=$( cbsdsqlro local "SELECT id,vhid,advskew,pass,interface,state FROM carp" | while read id vhid advskew pass interface state; do
		eval mark=\$vhid_selected_${id}
		if [ "${mark}" = "1" ]; then
			mark="X"
		else
			mark=" "
		fi
		echo "'${id}'	'[${mark}] vhid=${vhid} advskew=${advskew}'	'iface=${interface};pass=${pass};state=${state}'"
	done ) || err 1 "${N1_COLOR}Error while create vhid map${N0_COLOR}"

	menu_list="${menu_list} '-' '-' ''"
	menu_list="${menu_list} 'ADD' 'Add new VHID' 'Add a new VHID to the passthrough list'"
	menu_list="${menu_list} 'REMOVE' 'Remove marked VHIDs' 'Remove all marked (X) VHIDs from the list'"
	menu_list="${menu_list} 'EXIT' 'Exit' 'Exit VHID manager'"

	f_dialog_default_fetch defaultitem

	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

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

set -e
. ${globalconf}
set +e

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

. ${subrdir}/bsdconfig.subr
. ${subrdir}/settings-tui.subr
. ${dialog}

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

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

	f_dialog_menutag_fetch mtag

	case "$mtag" in
		EXIT)
			break
			;;
		"-")
			continue
			;;
		REMOVE)
			vhid_remove
			continue
			;;
		ADD)
			vhid_add
			continue
			;;
		*)
			vhid=${mtag%% *}
			eval mark=\$vhid_selected_${vhid}
			if [ "${mark}" = "1" ]; then
				eval "vhid_selected_${vhid}=\"0\""
			else
				eval "vhid_selected_${vhid}=\"1\""
			fi
			;;
	esac
done

exit 0

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