#!/usr/local/bin/cbsd
#v10.1.6
MYARG=""
MYOPTARG="alljails shownode display node header mode jname"
MYDESC="Operate with bhyve disk images and databse"
CBSDMODULE="bhyve"
EXTHELP="wf_bhyve.html"
ADDHELP="alljails=1 - get jaillist from remote node\n\
shownode=1 - show nodename for jails\n\
node= only for current node\n\
header=0 don't print header\n\
display= list by comma for column. Default: id,vhid,advskew,pass,interface,state\n\
mode=rescan - rescan vhd and sync info in sql base\n"

. ${subr}
. ${system}
. ${strings}
. ${tools}

init $*

. ${workdir}/carp.subr

[ -z "${display}" ] && display="id,vhid,advskew,pass,interface,state"

#remove commas for loop action on header
mydisplay=$(echo ${display} |/usr/bin/tr ',' '  ')

# upper for header
myheader=$(echo ${mydisplay} |/usr/bin/tr '[:lower:]' '[:upper:]')

show_header()
{
	local _header="${BOLD}${myheader}${NORMAL}"
	[ ${header} -eq 1 ] && $ECHO ${_header}
}

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

	_status=

	printf "${NORMAL}" # for column sort

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

		printf "${_val} "
	done

	printf "\n"
}


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

	_sql="SELECT id,vhid,advskew,pass,interface,state FROM carp"
	cbsdsql local ${_sql}| while read id vhid advskew pass interface state; do
#		populate_output_data
#		${ECHO} ${_status}
		IFS=" "
		populate_output_data
		IFS="|"
	done

	IFS=" "
}


show_local()
{
	local _errcode _status

	show_header
	show_jaildata_from_sql local
}

show_vhid()
{
	show_local
}

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

show_local | /usr/bin/column -t
