#!/usr/bin/env cbsd
#v12.1.10
CBSDMODULE="jail"
CIXARG="jname"
CIXOPTARG="mode cmd"
MYDESC="Modify parameters for jail (interactive/dialog)"
ADDHELP="
${H3_COLOR}Description${N0_COLOR}:

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

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

${H3_COLOR}Options${N0_COLOR}:

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

${H3_COLOR}Examples${N0_COLOR}:

 # cbsd jconfig

${H3_COLOR}See also${N0_COLOR}:

  cbsd jset --help

"

EXTHELP="wf_jconfig"

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

[ -z "${1}" ] && select_jail_by_list -s "List of local jails:" -r 1 -o 0

cixinit

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

[ "${emulator}" != "jail" ] && err 1 "${N1_COLOR}${CIX_APP}: for jail only (${emulator})${N0_COLOR}"

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

if [ "${mode}" = "list" ]; then
	echo
	${ECHO} "${N1_COLOR}Additional command available: ${N2_COLOR}jconfig jname=${jname} <cmd>:${N0_COLOR}"
	if [ -d "${CMDDIR}" ]; then
		capture _cmdfiles ${FIND_CMD} ${CMDDIR} -mindepth 1 -maxdepth 1 -type f
		for _f in ${_cmdfiles}; do
			echo "${_f##*/}"
		done
	fi
	exit 0
fi

vimage=""
[ "${vnet}" ] && vimage="vnet"

cfgargs=
for i in ${@}; do
	capture prefix6 substr --pos=0 --len=6 --str="${i}"
	[ "${prefix6}" = "jname=" ] && continue
	if [ -z "${cfgargs}" ]; then
		cfgargs="${i}"
	else
		cfgargs="${cfgargs} ${i}"
	fi
	[ -z "${cmd}" ] && cmd="${i}"		# first args
done

if [ -z "${cmd}" ]; then
	myargs="allow_devfs allow_dying allow_fusefs allow_kmem allow_linprocfs allow_linsysfs allow_mlock allow_mount allow_nfsd allow_nullfs allow_procfs \
	allow_raw_sockets allow_read_msgbuf allow_reserved_ports allow_tmpfs allow_unprivileged_proc_debug allow_vmm allow_zfs applytpl arch astart basename baserw boot_delay \
	childrenmax ci_gw4 cpuset devfs_ruleset enforce_statfs exec_consolelog exec_fib exec_start exec_stop exec_timeout floatresolv hidden host_hostname interface ip4_addr \
	jdomain jnameserver mkhostsfile mnt_start mnt_stop mount_devfs mount_fdescfs mount_kernel mount_linprocfs mount_linsysfs mount_ports mount_procfs mount_src persist \
	protected stop_timeout sysdir_volume sysvmsg sysvsem sysvshm ver vnet"

	# FreeBSD 14.2+
	[ ${freebsdhostversion} -ge 1402000 ] && myargs="${myargs} allow_adjtime allow_chflags allow_extattr allow_settime allow_suser"

	jsetup-tui jname=${jname} ${myargs}

elif [ -f "${CMDDIR}${cmd}" ]; then
	. ${CMDDIR}${cmd}
	myconf ${cfgargs}
fi

exit 0
