#!/usr/local/bin/cbsd
#v12.1.14
MYARG="search_profile"
MYOPTARG="display header active uniq show_bhyve show_xen show_jail show_cloud"
MYDESC="Operate with bhyve disk images and database"
CBSDMODULE="sys"
ADDHELP="search_profile= - prefix for filename, e.g: vm-${vm_os_type}, ${emulator}-freebsd- \n\
header=0 don't print header\n\
display= list by comma for column: path,name,active,contrib. Default: path,name,active\n\
show_bhyve=0,1,2 show only active=0 (disabled), 1-(enabled) and 2 - (any). default is: 2\n\
show_xen=0,1,2 show only active=0 (disabled), 1-(enabled) and 2 - (any). default is: 2\n\
show_jail=0,1,2 show only active=0 (disabled), 1-(enabled) and 2 - (any). default is: 2\n\
show_cloud=0,1,2 show only cloud=0 (disabled), 1-(enabled) and 2 - (any). default is: 0\n\
uniq=0,1 - 0 (default) - show all profiles, 1 - sort uniq by name with ~cbsd/etc/ win over ~cbsd/etc/defaults\n"

. ${subrdir}/nc.subr

. ${cbsdinit}

. ${system}

[ -z "${show_bhyve}" ] && show_bhyve=0
[ -z "${show_jail}" ] && show_jail=0
[ -z "${show_xen}" ] && show_xen=0
[ -z "${show_cloud}" ] && show_cloud=0
[ -z "${uniq}" ] && uniq=0
[ -z "${display}" ] && display="path,name,active"
[ -z "${only_bhyve_active}" ] && only_bhyve_active=2
[ -z "${only_xen_active}" ] && only_xen_active=2

#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="${BOLD}${myheader}${N0_COLOR}"
	[ ${header} -eq 1 ] && ${ECHO} ${_header}
}

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

	_status=

	printf "${N0_COLOR}" # 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"
}

# return 1 if $1 exist in global $vm_name_list variable
is_uniq()
{
	for i in ${vm_name_list}; do
		[ "${i}" = "${1}" ] && return 1
	done

	return 0
}

# $1 - which file from. Eg: local
show_jaildata_from_sql()
{
	local _i
	local _tmp_profile_list
	local _profile_list
	local _etcdefdir_len=$( strlen ${etcdir}/default )
	local _etcdef_part=$( substr --pos=0 --len=${_etcdefdir_len} --str=${etcdir}/default )

	# sort - /etc must win before etc/default
	_tmp_profile_list=$( ${FIND_CMD} ${etcdir}/defaults ${etcdir} -type f -depth 1 -maxdepth 1 -name ${search_profile}\* -exec ${REALPATH_CMD} {} \; | ${SORT_CMD} -r )

	vm_name_list=

	local name_col=1

	# determine column number for name field
	# it will come in handy for sorting
	for _i in ${mydisplay}; do
		[ "${_i}" = "name" ] && break
		name_col=$(( name_col + 1 ))
	done

	for _i in ${_tmp_profile_list}; do
		vm_profile=
		bhyve_active=0
		xen_active=0
		jail_active=0
		is_cloud=0
		eval $( ${GREP_CMD} -E "(^jail_profile=)|(^vm_profile=)|(^bhyve_active=)|(^xen_active=)|(^jail_active)|(^is_cloud)" ${_i} )
		if [ ${show_bhyve} -eq 1 -o ${show_xen} -eq 1 ]; then
			[ -z "${vm_profile}" ] && continue
		fi
		path="${_i}"
		[ -z "${is_cloud}" ] && is_cloud=0
		[ -z "${xen_active}" ] && xen_active=0
		[ -z "${jail_active}" ] && jail_active=0
		[ -z "${bhyve_active}" ] && bhyve_active="99"
		[ -n "${jail_profile}" ] && name="${jail_profile}"
		[ -n "${vm_profile}" ] && name="${vm_profile}"

		case "${show_cloud}" in
			0)
				[ "${is_cloud}" = "1" ] && continue
				;;
			1)
				[ "${is_cloud}" = "0" ] && continue
		esac

		if [ ${show_bhyve} -eq 1 ]; then
			case "${show_bhyve}" in
				0|1)
					[ "${bhyve_active}" != "${show_bhyve}" ] && continue
					;;
			esac
		fi

		if [ ${show_xen} -eq 1 ]; then
			case "${show_xen}" in
				0|1)
					[ "${xen_active}" != "${show_xen}" ] && continue
					;;
			esac
		fi

		if [ ${show_jail} -eq 1 ]; then
			case "${show_jail}" in
				0|1)
					[ "${jail_active}" != "${show_jail}" ] && continue
					;;
			esac
		fi

		if [ ${uniq} -eq 1 ]; then
			if ! is_uniq ${name}; then
				continue
			fi
			vm_name_list="${vm_name_list} ${name}"
		fi

		contrib=0
		_cur_part=$( substr --pos=0 --len=${_etcdefdir_len} --str=${_i} )
		if [ "${_cur_part}" = "${_etcdef_part}" ]; then
			contrib=1
		fi

		populate_output_data
	done | ${SORT_CMD} -k${name_col} -n

	unset vm_name_list
}


show_local()
{
	local _errcode _status

	show_header
	show_jaildata_from_sql local
}

show_vhid()
{
	show_local
}

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