#!/usr/bin/env cbsd
#v11.0.16
CBSDMODULE="jail"
CIXARG=""
CIXOPTARG="jname mode editor"
MYDESC="Show or modify jail description"
ADDHELP="
${H3_COLOR}Description${N0_COLOR}:

Show or modify jail description.

${H3_COLOR}Options${N0_COLOR}:

 ${N2_COLOR}jname=${N0_COLOR}   - specify jail name;
 ${N2_COLOR}mode=${N0_COLOR}    - set 'update' for update information;
 ${N2_COLOR}editor=${N0_COLOR}  - alternative editor (default: nvi);

${H3_COLOR}Examples${N0_COLOR}:

 # cbsd jdescr
 # cbsd jdescr jname=myjail
 # cbsd jdescr jname=myjail mode=update

${H3_COLOR}See also${N0_COLOR}:

  cbsd jset --help

"
EXTHELP="wf_jdescr"

unset jname

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

cixinit

showdescr()
{
	SYSDIR="${jailsysdir}/${jname}"
	PDESCR="${SYSDIR}/descr"

	if [ "${mode}" = "update" ]; then
		[ ! -d "${SYSDIR}" ] && /bin/mkdir -m 0775 -p ${SYSDIR}
		# ensure we have rw for group ( front/UI )
		/usr/bin/touch ${PDESCR} && /usr/sbin/chown ${cbsduser}:${cbsduser} ${PDESCR}
		/bin/chmod 0660 ${PDESCR}
		${editor} "${PDESCR}"
	else
		[ -r "${PDESCR}" ] && ${CAT_CMD} ${PDESCR}
	fi
}

showall()
{
	local jname _rows

	capture _rows cbsdsqlro local "SELECT jname FROM jails WHERE emulator='jail' ORDER BY jname ASC"
	while read jname; do
		[ -z "${jname}" ] && continue
		echo "${BOLD}${jname} {${N0_COLOR}"
		showdescr
		echo "${BOLD}}${N0_COLOR}"
	done <<< "${_rows}"
}

[ -z "${editor}" ] && editor="nvi"

if [ -z "${jname}" ]; then
	showall
else
	. ${subrdir}/rcconf.subr
	[ $? -eq 1 ] && err 1 "${N1_COLOR}No such jail: ${N2_COLOR}${jname}${N0_COLOR}"
	showdescr
fi
