#!/usr/bin/env cbsd
#v15.0.0
. ${subrdir}/nc.subr
. ${strings}
. ${tools}
CBSDMODULE="bhyve"
MYDESC="bhyve UEFI boot order list"
CIXARG="jname"
CIXOPTARG="display header human quiet"
ADDHELP="
${H3_COLOR}Description${N0_COLOR}:

 Text user interface for managing bhyve UEFI boot order configuration.

${H3_COLOR}Options${N0_COLOR}:

 ${N2_COLOR}display=${N0_COLOR}   - comma-separated list of columns to display.
             Default: boot,name,item.
 ${N2_COLOR}header=${N0_COLOR}    - set to 0 to suppress the header line. Default: 1.
 ${N2_COLOR}human=${N0_COLOR}     - set to 1 to format output for human readability. Default: 1.
 ${N2_COLOR}jname=${N0_COLOR}     - target virtual machine name.
 ${N2_COLOR}quiet=${N0_COLOR}     - set to 1 to suppress error messages. Default: 0.

${H3_COLOR}Examples${N0_COLOR}:

 # cbsd bhyve-efivar jname=vm1

${H3_COLOR}See also${N0_COLOR}:

  cbsd bhyve-efivar-tui --help
  cbsd bconfig --help
  cbsd bget --help
  cbsd bset --help

"

human=1
quiet=0
cixinit
. ${strings}

[ -z "${display}" -a -z "${odisplay}" ] && display="boot,name,item"

mydisplay=$( printf '%s' "${display}" | ${TR_CMD} ',' '|' )
myheader=$( printf '%s' "${mydisplay}" | ${TR_CMD} '[:lower:]' '[:upper:]' )

show_header()
{
	local _header="${H1_COLOR}${BOLD}${myheader}${N0_COLOR}"
	[ ${header} -ne 0 ] && ${ECHO} "${_header}"
}

show_jaildata_from_sql()
{
	local i=
	local val=

	for i in ${name_list}; do
		eval val=\$${i}

		[ -z "${val}" ] && val="unknown"

		printf "${N0_COLOR}"

		if [ "${i}" = "Boot${curboot}" ]; then
			${ECHO} "${N2_COLOR}*|${i}|${val}${N0_COLOR}"
		else
			${ECHO} "${N4_COLOR}-|${i}|${val}${N0_COLOR}"
		fi
	done
}

show_local()
{
	show_header
	show_jaildata_from_sql local
}


### MAIN
. ${subrdir}/rcconf.subr
[ ${?} -eq 1 ] && err 1 "${N1_COLOR}no such jail: ${N2_COLOR}${jname}${N0_COLOR}"
[ "${emulator}" != "bhyve" ] && err 1 "${N1_COLOR}Only for bhyve type VMs${N0_COLOR}"
shift #skip for jname

VM_VARS="${jailsysdir}/${jname}/BHYVE_UEFI_VARS.fd"

[ ! -r "${VM_VARS}" ] && err 1 "${N1_COLOR}${CIX_APP}: no such UEFI VARS file: ${N2_COLOR}${VM_VARS}${N0_COLOR}"
capture temp ${MKTEMP_CMD}
trap "${RM_CMD} -f ${temp} ${temp}.efi" HUP INT ABRT BUS TERM EXIT
${miscdir}/efivar ${VM_VARS} > ${temp}

capture _curboot_raw ${AWK_CMD} '/BootOrder:/{print $2}' ${temp}
capture curboot strstrip --str="${_curboot_raw}" --chars=","

${GREP_CMD} -E -v '(^BootCurrent:)|(^BootOrder:)|(Unreferenced)' ${temp} | ${TR_CMD} '+*' ' ' | ${GREP_CMD} Boot0 | ${SED_CMD} 's://::g' > ${temp}.efi

name_list=$( ${AWK_CMD} '/Boot[0-9]+/{print $1}' ${temp}.efi | ${SORT_CMD} -u | ${XARGS_CMD} )

while read _name _other; do
	eval "${_name}=\"${_other}\""
done < ${temp}.efi

${RM_CMD} -f ${temp}.efi

if [ -z "${name_list}" ]; then
	if [ "${quiet}" = "1" ]; then
		exit 1
	else
		err 1 "${N1_COLOR}${CIX_APP}: VARS not found: ${N2_COLOR}${VM_VARS}${N0_COLOR}"
	fi
fi
[ -z "${header}" ] && header=1
sqldelimer=" "
if [ ${human} -eq 1 ]; then
	show_local | ${COLUMN_CMD} -t -s'|'
else
	show_local
fi

exit 0
