#!/usr/bin/env cbsd
#v12.0.6
CBSDMODULE="bhyve"
CIXARG="jname mode"
CIXOPTARG="display header select device_name"
MYDESC="Operate with bhyve PCI bus map"
ADDHELP="
${H3_COLOR}Description${N0_COLOR}:

 Manage bhyve PCI bus device map: list, get or reset PCI slot assignments.

${H3_COLOR}Options${N0_COLOR}:

 ${N2_COLOR}jname=${N0_COLOR}       - target bhyve domain;
 ${N2_COLOR}mode=${N0_COLOR}        - operation mode:
           * list  - show current PCI bus map (default);
           * get   - get PCI bus for specified device (device_name= required);
           * reset - truncate and re-order PCI bus table;
 ${N2_COLOR}device_name=${N0_COLOR} - pcislot_name of device (mandatory for mode=get);
 ${N2_COLOR}select=${N0_COLOR}      - SQL column for mode=get (default: pcislot_bus);
 ${N2_COLOR}display=${N0_COLOR}     - columns to display (default: pcislot_name,pcislot_bus,pcislot_pcislot,pcislot_function,pcislot_desc);
 ${N2_COLOR}header=${N0_COLOR}      - show header (1 by default);

${H3_COLOR}Examples${N0_COLOR}:

 # cbsd bpcibus jname=vm1
 # cbsd bpcibus jname=vm1 mode=get device_name=virtio-net
 # cbsd bpcibus jname=vm1 mode=reset

${H3_COLOR}See also${N0_COLOR}:

  cbsd bhyve-ppt --help
  cbsd bconfig --help

"

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

select="pcislot_bus"
device_name=

cixinit

. ${subrdir}/rcconf.subr
case "${emulator}" in
	bhyve|xen|qemu)
		true
		;;
	*)
		log_err 1 "${N1_COLOR}Not in bhyve emulator: ${N2_COLOR}${jname}${N0_COLOR}"
		;;
esac

[ -z "${display}" ] && display="pcislot_name,pcislot_bus,pcislot_pcislot,pcislot_function,pcislot_desc"

capture mydisplay ${TR_CMD} ',' '  ' <<< "${display}"
capture myheader ${TR_CMD} '[:lower:]' '[:upper:]' <<< "${mydisplay}"

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

populate_output_data()
{
	local _i= _val=

	_status=
	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
}

show_basesdata_from_sql()
{
	local _sqlfile=

	if [ -n "${1}" ]; then
		_sqlfile="$1"
	else
		_sqlfile="local"
	fi

	cbsdsqlquery ${_sqlfile} "SELECT pcislot_name,pcislot_bus,pcislot_pcislot,pcislot_function,pcislot_desc FROM pcibus" | while read _q; do
		pcislot_name=${_q@.pcislot_name}
		pcislot_bus=${_q@.pcislot_bus}
		pcislot_pcislot=${_q@.pcislot_pcislot}
		pcislot_function=${_q@.pcislot_function}
		pcislot_desc=${_q@.pcislot_desc}
		populate_output_data
		printf "${N2_COLOR}${_status}${N0_COLOR}\n"
	done
}

show_remote()
{
	show_header

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

	for _n in ${node}; do
		show_basesdata_from_sql "inv.${_n}"
	done
}

show_local()
{
	show_header
	show_basesdata_from_sql ${jailsysdir}/${jname}/local.sqlite
}

show_pcibus_map()
{
	if [ -n "${node}" ]; then
		show_remote
	else
		show_local
	fi
}

#### MAIN
[ -z "${header}" ] && header=1

case "${mode}" in
	get)
		[ -z "${device_name}" ] && err 1 "${N1_COLOR}mandatory parameter: ${N2_COLOR}device_name=${N0_COLOR}"
		sqllistdelimer=" "
		cbsdsqlro ${jailsysdir}/${jname}/local.sqlite "SELECT ${select} FROM pcibus WHERE pcislot_name='${device_name}' OR pcislot_name='virtio-net-pci'"
		;;
	list)
		sqllistdelimer=" "
		show_pcibus_map | ${COLUMN_CMD} -t
		;;
	reset)
		cbsdsqlrw ${jailsysdir}/${jname}/local.sqlite "DELETE FROM pcibus"
		${ECHO} "${N1_COLOR}Purge pcibus table for: ${N2_COLOR}${jname}${N0_COLOR}"
		;;
	*)
		err 1 "${N1_COLOR}Unknown mode ${mode}, valid: ${N2_COLOR}list,reset${N0_COLOR}"
esac

exit 0
