#!/usr/local/bin/cbsd
#v12.0.4
MYARG=""
MYOPTARG="alljails shownode display node header mode jname"
MYDESC="Show disk images from database"
CBSDMODULE="bhyve"
EXTHELP="wf_bhyve"
ADDHELP="
header=0 don't print header
display= list by comma for column. Default: display=md5,name,path,source,emulator,size,created"

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

. ${cbsdinit}
. ${subrdir}/bhyve.subr
. ${subrdir}/virtual.subr
[ -z "${display}" ] && display="md5,name,path,source,emulator,size"

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

# upper for header
myheader=$( echo ${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"
		[ ! -r ${sqlfile}.sqlite ] && exit 1

	else
		sqlfile="images"
	fi

	_status=
	_sql="SELECT md5,name,path,source,emulator,size FROM images"

	cbsdsqlro ${sqlfile} ${_sql} | while read md5 name path source emulator size; do
		#created=$( echo ${created} | ${TR_CMD} " " "_" )

		if ! is_number ${size}; then
			if conv2human "${size}"; then
				size=${convval}
			fi
		fi

		populate_output_data
		printf "${N2_COLOR}"
		printf "${_status}"
		printf "${N0_COLOR}\n"
	done
}

show_images()
{
	show_header
	show_jaildata_from_sql
}

#### MAIN
[ -z "${header}" ] && header=1
sqldelimer=" "
show_images | ${COLUMN_CMD} -t
exit 0
