#!/usr/bin/env cbsd
#v12.0.4
CIXARG=""
CIXOPTARG="alljails display header jname mode node shownode"
MYDESC="Show bhyve NVMe storage controller"
CBSDMODULE="bhyve"
EXTHELP="wf_bhyve"
ADDHELP="
${H3_COLOR}Description${N0_COLOR}:

 List NVMe storage controller configuration for bhyve VMs.
 Shows device path, RAM, queues, I/O slots, sector size and serial per VM.

${H3_COLOR}Options${N0_COLOR}:

 ${N2_COLOR}alljails=${N0_COLOR}  - set to 1 to include remote nodes in the output. Default: local only.
 ${N2_COLOR}display=${N0_COLOR}   - comma-separated list of columns to show.
             Default: jname,devpath,ram,maxq,qsz,ioslots,sectsz,ser.
 ${N2_COLOR}header=${N0_COLOR}    - set to 0 to suppress the header line. Default: 1.
 ${N2_COLOR}jname=${N0_COLOR}     - target virtual machine name.
 ${N2_COLOR}mode=${N0_COLOR}      - set to rescan to rescan VHD and sync info in database.
 ${N2_COLOR}node=${N0_COLOR}      - query specific node(s) only.
 ${N2_COLOR}shownode=${N0_COLOR}  - set to 1 to display node name(s) for listed VMs.

${H3_COLOR}Examples${N0_COLOR}:

 # cbsd bhyve-nvme-list
 # cbsd bhyve-nvme-list jname=myvm
 # cbsd bhyve-nvme-list header=0 display=devpath,ram,ser

${H3_COLOR}See also${N0_COLOR}:

   cbsd bhyve-nvme --help
   cbsd bconfig --help
   cbsd bset --help

"

. ${subrdir}/nc.subr

. ${system}
cixinit
. ${strings}
. ${subrdir}/bhyve.subr
. ${subrdir}/virtual.subr

[ -z "${display}" ] && display="jname,devpath,ram,maxq,qsz,ioslots,sectsz,ser"

#remove commas for loop action on header
mydisplay=${display//,/ }

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

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

# if $1 = "Unregister" then overwrite status to "Unregister"
populate_output_data()
{
	local _i _val dsk_size

	_status=

	#populate values for in output string
	for _i in ${mydisplay}; do
		_val=""
		eval _val=\$$_i
		[ -z "${_val}" ] && _val="-"

		if [ -z "${_status}" ]; then
			_status="${N0_COLOR}${_val}"
		else
			_status="${_status} ${_val}"
		fi
	done
}


# $1 - which file from. Eg: local
show_jaildata_from_sql()
{
	local _i

	#   set sqlfile for ". rcconf" including
	if [ -n "${1}" ]; then
		sqlfile="$1"
	else
		sqlfile="local"
	fi

	[ -n "${2}" ] && jname="${2}"

	_status=
	_sql="SELECT devpath,ram,maxq,qsz,ioslots,sectsz,ser FROM bhyve_nvme"
	cbsdsqlro ${sqlfile} "${_sql}" | while read devpath ram maxq qsz ioslots sectsz ser; do
		populate_output_data
		printf "${N2_COLOR}"
		printf "${_status}"
		printf "${N0_COLOR}\n"
	done

}

show_remote()
{
	show_header

	[ -z "${node}" ] && node=$( cbsd node mode=list header=0 allinfo=0 )

	for _n in $node; do
		nodename="${_n}"
		show_jaildata_from_sql "inv.${_n}"
	done
}

show_local()
{
	local _errcode _status _vm _lvm

	show_header

	for _vm in ${vms}; do
		_lvm="${jailsysdir}/${_vm}/local.sqlite"
		[ -r "${_lvm}" ] && show_jaildata_from_sql ${_lvm} ${_vm}
	done

}

show_dsk()
{

	if [ -n "${node}" ]; then
		show_remote
		exit
	fi

	if [ -n "${alljails}" ]; then
		show_local
		header=0
		show_remote
	else
		show_local
	fi

}

vm_list()
{
	if [ -n "${jname}" ]; then
		vms="${jname}"
	else
		capture vms cbsdsqlro local "SELECT jname FROM jails WHERE emulator = 'bhyve'"
	fi
}


vms=
vm_list

#### MAIN
if [ "${mode}" = "rescan" ]; then
	rescan_dsk
	exit 0
fi

[ -z "${header}" ] && header=1
sqldelimer=" "
show_dsk | ${COLUMN_CMD} -t

exit 0
