#!/usr/bin/env cbsd
#v10.1.0
CBSDMODULE="bhyve"
MYDESC="Ncurses based control for bhyve"
CIXARG="jname"
CIXOPTARG=""
ADDHELP="
${H3_COLOR}Description${N0_COLOR}:

 Ncurses (dialog) based TUI for managing bhyve virtual machines.
 Provides an interactive menu to start, stop, attach to console,
 edit configuration and destroy a bhyve domain.

 When launched without jname= argument, displays a selection list
 of all available bhyve VMs.

${H3_COLOR}Options${N0_COLOR}:

 ${N2_COLOR}jname=${N0_COLOR} - target bhyve domain name. If omitted, a VM
              selection dialog is shown.

${H3_COLOR}Examples${N0_COLOR}:

 # cbsd bcontrol-tui jname=myvm
 # cbsd bcontrol-tui

${H3_COLOR}See also${N0_COLOR}:

  cbsd bstart --help
  cbsd bstop --help
  cbsd bconfig --help
  cbsd blogin --help

"

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

[ -z "${1}" ] && select_jail_by_list -s "List of local VMs:" -r 0 -e bls
cixinit

. ${dialog}

dialog_menu_main()
{
	repo=0 # flags for execution jsetup-tui from repo get image

	title=" Control for ${jname} "
	hline=

	local menu_list="
		'EXIT'	'EXIT'	'Exit'
	" # END-QUOTE

	if [ ${jid} -ne 0 ]; then
			menu_list="${menu_list} 'bstop'		'stop vm'		'cbsd bstop jname=${jname}'"
			menu_list="${menu_list} 'blogin'	'attach to console'	'cbsd blogin jname=${jname}. Use Ctrl+b,d to detach (tmux)'"
		else
			menu_list="${menu_list} 'bstart'	'start vm'	'cbsd bstart jname=${jname}'"
	fi

	menu_list="${menu_list}	'bconfig'	'config'	'cbsd bconfig jname=${jname}'"
	menu_list="${menu_list}	'-'	'-'	''"
	menu_list="${menu_list}	'bremove'	'destroy vm'	'cbsd bremove jname=${jname}'"

	cbsd_menubox
	retval=$?

	# Only update default-item on success
	[ $retval -eq $DIALOG_OK ] && f_dialog_default_store "$menu_choice"

	return $retval
}

### MAIN
. ${subrdir}/rcconf.subr
[ $? -eq 1 ] && err 1 "${N1_COLOR}No such jail: ${N2_COLOR}${jname}${N0_COLOR}"

f_dialog_title "$msg_system_console_configuration"
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"

while [ 1 ]; do
	. ${subrdir}/rcconf.subr
	dialog_menu_main || f_die
	case "${mtag}" in
		"EXIT")
			exit 0
			;;
		bstart|bstop)
			/usr/local/bin/cbsd ${mtag} jname=${jname}
			${ECHO} " *** Complete. Press any key"
			read p
			;;
		blogin)
			capture vm_efi cbsdsqlro local "SELECT vm_efi FROM bhyve WHERE jname='${jname}'"

			if [ "${vm_efi}" != "none" ]; then
				if [ -f "${jailsysdir}/${jname}/vnc_port" ]; then
					read vm_port < "${jailsysdir}/${jname}/vnc_port"
					echo " *** VM booted in VNC mode. ***"
					${ECHO} "${N1_COLOR}For attach VM console, use: ${N2_COLOR}vncviewer ${bhyve_vnc_tcp_ipconnect}:${vm_port}${N0_COLOR}"
					read p
				else
					echo " *** VM booted in VNC mode. ***"
					echo "But no ${jailsysdir}/${jname}/vnc_port file"
					read p
				fi
			else
				/usr/local/bin/cbsd ${mtag} jname=${jname}
			fi
			;;
		bremove)
			if getyesno "Remove vm from filesystem. Are you sure ?"; then
				/usr/local/bin/cbsd ${mtag} jname=${jname}
				${ECHO} " *** Complete. Press any key"
				read p
				f_die
			fi
			;;
		"bconfig")
			/usr/local/bin/cbsd bconfig jname=${jname}
			;;
	esac
done

exit 0
