#!/usr/local/bin/cbsd
CBSDMODULE="xen"
CIXARG="jname"
CIXPTARG="mode cmd"
MYDESC="Modify parameters for XEN domain (interactive/dialog)"
ADDHELP="
${H3_COLOR}Description${N0_COLOR}:

You can edit available options for XEN VM interactively, through the dialog(1)
interface.  Some parameters can be applied while the VM is running,
but most will require a VM restart. 'cbsd xconfig' without arguments
show a list of XEN to select. When XEN selected or specified as
an argument, 'bconfig' will launch a 'cbsd xsetup-tui' script.

For non-interactive edit, please use 'cbsd xset' instead.

${H3_COLOR}Options${N0_COLOR}:

 ${N2_COLOR}mode=${N0_COLOR}      - set 'list' for list of XENs external command;
 ${N2_COLOR}jname=${N0_COLOR}     - work with <VM>;

${H3_COLOR}Examples${N0_COLOR}:

 # cbsd xconfig

${H3_COLOR}See also${N0_COLOR}:

  cbsd xset --help
  cbsd xen-dsk --help

"

EXTHELP="wf_jconfig"

. ${subrdir}/nc.subr
. ${tools}	# select_jail_by_list

[ -z "${1}" ] && select_jail_by_list -s "List of local VMs" -e xls -a "Off|On"

cixinit

. ${subrdir}/rcconf.subr
[ $? -eq 1 ] && log_err 1 "${N1_COLOR}no such vm: ${N2_COLOR}${jname}${N0_COLOR}"
[ "${emulator}" != "xen" ] && log_err 1 "${N1_COLOR}only for xen type VMs${N0_COLOR}"

CMDDIR="${jailsysdir}/${jname}/bin/"

if [ "${mode}" = "list" ]; then
	${ECHO} "${N1_COLOR}Configure command available: ${N2_COLOR}xconfig jname=${jname} <cmd>:${N0_COLOR}"
	[ -d "${CMDDIR}" ] && ${FIND_CMD} ${CMDDIR} -mindepth 1 -maxdepth 1 -type f -exec ${BASENAME_CMD} {} \;
	exit 0
fi

cfgargs=
for i in ${CIX_OTHER_ARGS}; do
	if [ -z "${cfgargs}" ]; then
		cfgargs="${i}"
	else
		cfgargs="${cfgargs} ${i}"
	fi
	[ -z "${cmd}" ] && cmd="${i}"           # first args
done

if [ -z "${cmd}" ]; then
	myargs="vm_ram vm_cpus astart ip4_addr vm_boot vm_iso_path vm_vnc_port protected hidden \
	cd_vnc_wait xen_vnc_resolution xen_vnc_tcp_bind vnc_password on_poweroff on_reboot on_crash \
	spice_default xen_spice_tcp_bind xen_spice_resolution spice_password soundhw debug_engine \
	xen_hap xen_nestedhvm"

	sorted_myargs=$( for i in ${myargs}; do
		echo "${i}"
	done | ${SORT_CMD} -n )

	xsetup-tui jname=${jname} ${myargs}
elif [ -f "${CMDDIR}${cmd}" ]; then
	. ${CMDDIR}${cmd}
	myconf ${cfgargs}
fi

exit 0
