#!/usr/local/bin/cbsd
#v10.1.6
MYARG=""
MYOPTARG="module nodesc"
MYDESC="This help"
CBSDMODULE="sys"
ADDHELP="
${H3_COLOR}Description${N0_COLOR}:

 Internal CBSD command list and assistance. By 'cbsd help' you can get a list
 of all CBSD command (include modules). By running 'cbsd help <cmd>' you get a 
 brief description of a specific command.

${H3_COLOR}Options${N0_COLOR}:

 ${N2_COLOR}module=${N0_COLOR} - when set - show command for specific module only,
              valid list of modules: 'sys', 'jail', 'bhyve' and 'xen';
 ${N2_COLOR}nodesc=${N0_COLOR} - when '0' - show command only, without description;

${H3_COLOR}Examples${N0_COLOR}:

 cbsd help | less -r
 cbsd help module=bhyve
 cbsd help jexec

"

if [ "${1}" = "--desc" ]; then
	# skip 'cbsd help module=' filter?
	if [ -n "${CBSDMODULEONLY}" ]; then
		[ "${CBSDMODULEONLY}" != "${CBSDMODULE}" ] && exit 0
	fi
	${ECHO} "[${BOLD}${CBSDMODULE}${N0_COLOR}] ${N2_COLOR}${MYDESC}${N0_COLOR}"
	exit 0
fi
if [ "${1}" = "--help" ]; then
	${ECHO} "[${BOLD}${CBSDMODULE}${N0_COLOR}] ${N2_COLOR}${MYDESC}${N0_COLOR}"
	${ECHO} "${BOLD}require: ${GREENL}${MYARG}${N0_COLOR}"
	${ECHO} "${BOLD}opt: ${N2_COLOR}${MYOPTARG}${N0_COLOR}"
	[ -n "${ADDHELP}" ] && printf "${N2_COLOR}${ADDHELP}${N0_COLOR}"
	printf "${BOLD}External help: ${N2_COLOR}https://github.com/cbsd/cbsd/blob/master/share/docs${N0_COLOR}\n"
	exit 0
fi

descr=1

# exclude jname= and mode= params from args str
for i in $*; do
	prefix7=$( substr --pos=0 --len=7 --str="${i}" )

	case "${prefix7}" in
		nodesc=|module=)
			eval $( echo "${i}" )
			shift
			continue
			;;
	esac
done

if [ "${nodesc}" = "1" ]; then
	descr=0
	NOCOLOR=1
fi

# help for $1 command?
if [ -n "${1}" ]; then
	${1} --help
	exit 0
fi

. ${distdir}/cbsd.conf
. ${distdir}/securecmd

show_help()
{
	local _maxlength=0 _tmplen=0 _sorted_cmd=

	# append for module cmd part
	if [ -f "${etcdir}/modules.conf" ]; then
		for i in $( ${CAT_CMD} ${etcdir}/modules.conf ); do
			[ ! -f "${distdir}/modules/${i}/securecmd" ] && continue
			. ${distdir}/modules/${i}/securecmd
		done
	fi

	_tmplen=$( strlen "${i}" )
	[ ${_tmplen} -gt ${_maxlength} ] && _maxlength=${_tmplen}

	# sort and determine max cmd length
	for i in ${SECCMD}; do
		_tmplen=$( strlen "${i}" )
		[ ${_tmplen} -gt ${_maxlength} ] && _maxlength=${_tmplen}
	done

	_sorted_cmd=$( for i in ${SECCMD}; do
		echo "${i}"
		export _maxlength=${_maxlength}
	done | ${SORT_CMD} -n )

	# CBSDMODULEONLY processed in nc.subr: init() when --desc args used
	[ -n "${module}" ] && export CBSDMODULEONLY="${module}"

	for i in ${_sorted_cmd}; do
		if [ ${descr} -eq 0 ]; then
			echo "${i}"
		else
			_cmddesc=$( ${i} --desc 2>/dev/null )
			[ $? -ne 0 ] && continue
			[ -z "${_cmddesc}" ] && continue
			printf "${BOLD}%-${_maxlength}s ${N1_COLOR} --  ${N2_COLOR}${_cmddesc}${N0_COLOR}" ${i}
			printf "\n"
		fi
	done
}

### MAIN
${ECHO} "${BOLD}CBSD version:                  ${H3_COLOR}${myversion}${N0_COLOR}" 1>&2
${ECHO} "${BOLD}CBSD documentation:            ${H3_COLOR}https://github.com/cbsd/cbsd/blob/master/share/docs${N0_COLOR}" 1>&2
${ECHO} "${BOLD}CBSD Telegram user group chat: ${H3_COLOR}https://t.me/cbsdofficial${N0_COLOR}" 1>&2
echo 1>&2
${ECHO} "${BOLD}${UNDERLINE}Available commands:${N0_COLOR}" 1>&2
[ -z "${module}" ] && ${ECHO} "\n Hint: use 'cbsd help module=<module>' to filter list." 1>&2
echo 1>&2
show_help
