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

 List virtual network interface (NIC) configuration for bhyve VMs.
 Shows driver, parent interface, MAC address, IP address, MTU and
 other NIC parameters per VM or across all VMs.

${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,nic_driver,nic_parent,nic_hwaddr,nic_address,nic_mtu,nic_persistent,nic_ratelimit,nic_flags,nic_vlan_untagged,nic_vlan_tagged.
             When cluster mode is active and shownode=1:
               nodename,jname,nic_driver,nic_parent,nic_hwaddr,nic_address,nic_mtu,nic_persistent,nic_ratelimit,nic_flags,nic_vlan_untagged,nic_vlan_tagged.
 ${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}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-nic-list
 # cbsd bhyve-nic-list jname=myvm
 # cbsd bhyve-nic-list header=0 display=nic_hwaddr,jname

${H3_COLOR}See also${N0_COLOR}:

  cbsd bhyve-nic --help
  cbsd bconfig --help
  cbsd bset --help

"

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

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

[ -n "${display}" ] && odisplay="${display}"
oalljails="${alljails}"
oshownode="${shownode}"
ojname="${jname}"

is_cluster_mode
cluster_mode=$?

if [ ${cluster_mode} -eq 0 ]; then
	if [ "${oshownode}" != "0" ]; then
		alljails=1
		shownode=1
	fi
fi

# restore manual settings
[ -n "${oalljails}" ] && alljails="${oalljails}"
[ -n "${oshownode}" ] && shownode="${oshownode}"

# defaults
[ -z "${display}" -a -z "${odisplay}" ] && display="jname,nic_driver,nic_parent,nic_hwaddr,nic_address,nic_mtu,nic_persistent,nic_ratelimit,nic_flags,nic_vlan_untagged,nic_vlan_tagged"
[ "${shownode}" = "1" -a -z "${odisplay}" ] && display="nodename,${display}"

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}"
}

# -j $jname
# -s alternative SQL file
# -u 1 - always show status as "Unregister"
populate_output_data()
{
	local _i _val
	local unregister="0" _md5_node_name

	while getopts "j:s:u:" opt; do
		case "${opt}" in
			j) jname="${OPTARG}" ;;
			s) sqlfile="${OPTARG}" ;;
			u) unregister="1" ;;
		esac
		shift $(($OPTIND - 1))
	done

	[ -z "${sqlfile}" ] && sqlfile="local"

	_sql="SELECT jname,nic_driver,nic_parent,nic_hwaddr,nic_address,nic_mtu,nic_persistent,nic_ratelimit,nic_flags,nic_vlan_untagged,nic_vlan_tagged FROM bhyvenic"
	cbsdsqlro ${sqlfile_sysdir}/${jname}/local.sqlite ${_sql} | while read jname nic_driver nic_parent nic_hwaddr nic_address nic_mtu nic_persistent nic_ratelimit nic_flags nic_vlan_untagged nic_vlan_tagged; do

		if [ -n "${ojname}" ]; then
			[ "${ojname}" != "${jname}" ] && continue
		fi

		printf "${N0_COLOR}"

		if [ "${sqlfile}" = "local" ]; then
			if [ -e "/dev/vmm/${jname}" ]; then
				printf "${N2_COLOR}"
			else
				printf "${N4_COLOR}"
			fi
		else
			capture _md5_node_name ${miscdir}/cbsd_md5 "${sqlfile}"
			eval _node_is_online=\$node_${_md5_node_name}_online
			if [ "${_node_is_online}" = "1" ]; then
				capture jid cbsdsqlro ${sqlfile} "SELECT jid FROM jails WHERE jname='${jname}'"
				case ${jid} in
					0)
						printf "${N4_COLOR}"
						;;
					*)
						printf "${N2_COLOR}"
						;;
				esac
			else
				printf "${N4_COLOR}"
			fi
		fi

		for _i in ${mydisplay}; do
			_val=
			eval _val=\$$_i
			[ -z "${_val}" ] && _val="\-"
			printf "${_val} "
		done
		printf "${N0_COLOR}\n"
	done
}


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

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

	if [ "${sqlfile}" = "local" ]; then
		sqlfile_sysdir="${jailsysdir}"
	else
		sqlfile_sysdir="${tmpdir}/${1}"
	fi

	cbsdsqlro ${sqlfile} "SELECT jname FROM jails WHERE emulator='bhyve' ORDER BY jname ASC" | while read jname; do
		_status=

		if [ "${sqlfile}" = "local" ]; then
			. ${subrdir}/rcconf.subr
		else
			RHST="${sqlfile}"
			. ${subrdir}/rrcconf.subr
		fi

		if [ ! -r ${sqlfile_sysdir}/${jname}/local.sqlite ]; then
			${ECHO} "${N1_COLOR}${jname}: can't open database file for ${jname}: ${N2_COLOR}${sqlfile_sysdir}/${jname}/local.sqlite${N0_COLOR}"  >> ${my_err_file}
			continue
		fi

		conv_status
		populate_output_data -j ${jname} -s ${sqlfile}
	done
}

show_remote()
{
	show_header

	[ -z "${node}" ] && capture node cbsdsqlro nodes "SELECT nodename FROM nodelist"

	for _n in ${node}; do
		nodename="${_n}"
		node_is_online -n ${nodename} -e 1
		show_jaildata_from_sql ${_n}
	done
}

show_local()
{
	show_header
	show_jaildata_from_sql local
}

show_nic()
{
	if [ -n "${node}" ]; then
		show_remote
		exit 0
	fi

	if [ "${alljails}" = "1" ]; 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
[ -z "${header}" ] && header=1
sqldelimer=" "
my_err_file="${ftmpdir}/bhyve-nic-list.$$"
show_nic | ${COLUMN_CMD} -t

if [ -r ${my_err_file} ]; then
	${ECHO} "${N1_COLOR}Error:${N0_COLOR}" 1>&2
	${CAT_CMD} ${my_err_file} 1>&2
	${RM_CMD} -f ${my_err_file}
fi

exit 0
