#!/usr/local/bin/cbsd
# shellcheck shell=sh
# shellcheck disable=SC2154,SC2086,SC1091
# v12.1.10

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

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

# shellcheck disable=SC2034
{
	CBSDMODULE="jail"
	MYDESC="Dialog-based text user interface for jail configuration"
	MYARG="jname"
	MYOPTARG="${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}/cbsdinit.subr
. ${subrdir}/settings-tui.subr
. ${subrdir}/settings-tui-jail.subr
. ${subrdir}/dialog.subr
. ${subrdir}/jsetup-tui.subr

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

# shellcheck disable=SC2034
title=" config for ${jname} "
f_dialog_info "scan and build menu entry."

. ${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"
else
	JARG="$*"
fi

f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init

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
	# shellcheck disable=SC2034
	# prompt is used by funtcion dialog_menu_main
	prompt="Jail is ONLINE. Only on-the-fly options printed."
else
	JARG="jname ${JARG}"
fi

while true; do
	f_dialog_info "scan and build menu entry.."

	dialog_menu_main || f_die

	# shellcheck disable=SC2034
	index=${mtag%% *}
	mychoice=${mtag##* }

	case "${mychoice}" in
		"EXIT")
			exit 0
			;;
		"-")
			continue
			;;
		"COMMIT")
			commit
			;;
		"jrctl")
			/usr/local/bin/cbsd jrctl-tui jname="${jname}"
			;;
		b_order|order)
			jorder-tui jname="${jname}"
			# shellcheck disable=SC2034
			b_order=$( cbsdsqlro local "SELECT b_order FROM jails WHERE jname=\"${jname}\"" 2>/dev/null )
			;;
		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)
			invert_checkbox ${mychoice}
			continue
			;;
		interface)
			get_construct_interface -d 1 -s "tap bridge vboxnet"
			continue
			;;
		"jailnic")
			jailnic-tui jname=${jname}
			;;
		*)
			get_construct_${mychoice}
			continue
			;;
	esac
done

exit 0
