#!/usr/bin/env cbsd
# v12.1.10
CBSDMODULE="jail"
MYDESC="Dialog-based text user interface for jail configuration"
CIXARG="jname"
CIXOPTARG="${JARG} outfile"
ADDHELP="

${H3_COLOR}Description${N0_COLOR}:

Text user interface for jail configuration. This script invoked by 'cbsd jconfig'.

${H3_COLOR}Options${N0_COLOR}:

 ${N2_COLOR}jname=${N0_COLOR}     - work with <jail>;

${H3_COLOR}Examples${N0_COLOR}:

 # cbsd jsetup-tui jname=jail1

${H3_COLOR}See also${N0_COLOR}:

  cbsd jconfig --help
  cbsd jget --help
  cbsd jset --help

"


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

[ ! -f "${distsharedir}/jail-arg" ] && err 1 "No such jail-arg skel"
. ${distsharedir}/jail-arg

cixinit

. ${subrdir}/settings-tui.subr
. ${subrdir}/settings-tui-jail.subr
. ${subrdir}/dialog.subr
. ${subrdir}/jsetup-tui.subr

### MAIN
zero_profile="jail-freebsd-default.conf"
#defaults

. ${subrdir}/build.subr
readconf ${zero_profile}

# re-read profile for init global_profile_file variable
get_construct_profile ${default_profile}

[ -z "${profile}" ] && [ -n "${jail_profile}" ] && profile="${jail_profile}"

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

shift #skip for jname

if [ ${jid} -ne 0 ]; then
	# Command for modifying on-the fly here:
	JARG="ip4_addr cpuset astart exec_consolelog mount_src mount_ports mount_kernel allow_mount allow_nullfs allow_fusefs allow_linsysfs allow_linprocfs allow_tmpfs allow_mlock allow_nfsd allow_procfs devfs_ruleset jdomain b_order applytpl protected hidden allow_raw_sockets allow_read_msgbuf allow_vmm sysvsem sysvshm sysvmsg boot_delay jnameserver allow_reserved_ports allow_unprivileged_proc_debug allow_suser allow_extattr allow_adjtime allow_settime allow_chflags sysdir_volume"
else
	JARG="$*"
fi

store_defaults

# fill promt variable used in dialog_menu_main about active jail status
# otherwise - append the argument that we can change also and jname.
if [ ${jid} -ne 0 ]; then
	prompt="Jail is ONLINE. Only on-the-fly options printed."
else
	JARG="jname ${JARG}"
fi

title=" jsetup-tui "
btitle="${title}"

while true; do
	dialog_menu_main || exit

	index=${mtag%% *}
	mychoice=${mtag##* }

	case "${mychoice}" in
		EXIT)
			exit 0
			;;
		"-")
			continue
			;;
		COMMIT)
			commit
			;;
		jrctl)
			jrctl-tui jname="${jname}"
			;;
		b_order|order)
			jorder-tui jname="${jname}"
			capture b_order cbsdsqlro local "SELECT b_order FROM jails WHERE jname='${jname}'"
			;;
		baserw|astart|applytpl|floatresolv|mount_ports|mount_src|vnet|mkhostsfile|persist)
			invert_checkbox ${mychoice}
			continue
			;;
		mount_devfs|allow_mount|allow_devfs|allow_nullfs|allow_fusefs|allow_linprocfs|allow_linsysfs|mount_fdescfs|allow_fdescfs|allow_dying|allow_procfs|protected|hidden|allow_reserved_ports|allow_raw_sockets|allow_unprivileged_proc_debug)
			invert_checkbox ${mychoice}
			continue
			;;
		allow_tmpfs|allow_zfs|allow_kmem|mount_kernel|mount_obj|allow_read_msgbuf|allow_vmm|allow_mlock|allow_nfsd|allow_suser|allow_extattr|allow_adjtime|allow_settime|allow_chflags|sysdir_volume)
			invert_checkbox ${mychoice}
			continue
			;;
		interface)
			get_construct_interface -d 1 -s "tap vboxnet"
			continue
			;;
		jailnic)
			jailnic-tui jname=${jname}
			;;
		*)
			get_construct_${mychoice}
			continue
			;;
	esac
done

exit 0
