#!/usr/local/bin/cbsd
#v11.2.0
CBSDMODULE="bhyve"
MYARG=""
MYOPTARG="alljails shownode display node header order"
MYDESC="List jail and status"
ADDHELP="alljails=1 - (0 or 1): force to display foreign/remote resources\n\
  when sqlreplica=1 and node available, alljails sets to 1 automatically\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.\n\
    Default: if RCTL enabled: jname,jid,vm_ram,vm_curmem,vm_cpus,pcpu,vm_os_type,ip4_addr,status,vnc_port\n\
    If RCTL unavailable: jname,jid,vm_ram,vm_cpus,vm_os_type,ip4_addr,status,vnc_port\n\
    If sqlrepica and node available: nodename,jname,jid,vm_ram,vm_cpus,vm_os_type,ip4_addr,status,vnc_port\n\
     additional field: path\n\
order= asc (default) or desc\n"
EXTHELP="wf_bls.html"

. ${subr}
. ${strings}
. ${nodes}

readconf bls.conf
init $*

oalljails="${alljails}"		# store original settings, they have more weight vs auto
oshownode="${shownode}"		# store original settings, they have more weight vs auto

# autosettings for alljails and shownode
is_cluster_mode
cluster_mode=$?		# cluster_mode=0 when we have any node

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

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

if [ -z "${display}" ]; then
	case "${racct}" in
		1)
			display="jname,jid,vm_ram,vm_curmem,vm_cpus,pcpu,vm_os_type,ip4_addr,status,vnc_port"
			;;
		*)
			display="jname,jid,vm_ram,vm_cpus,vm_os_type,ip4_addr,status,vnc_port"
			;;
	esac
fi

[ "${shownode}" = "1" ] && display="nodename,${display}"
[ -z "${order}" ] && order="asc"

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

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

JLS=""

conv_status()
{
	local _maintain_reason=

	case "${status}" in
		0)
			status="Off"
			;;
		1)
			status="On"
			;;
		2)
			status="Slave"
			;;
		3)
			_maintain_reason=$( cbsdsql local SELECT maintenance FROM jails WHERE jname=\"${jname}\" 2>/dev/null )
			if [ -n "${_maintain_reason}" -a "${_maintain_reason}" != "0" ]; then
				status="Maintenance:${_maintain_reason}"
			else
				status="Maintenance"
			fi
			;;
		*)
			status="Unknown"
			;;
	esac
}


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

# $1 - pid
get_rctl()
{
	local _val

	get_rctl_values -m process -p ${1} -j ${jname}
}

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

	printf "${NORMAL}" # for column sort

	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"

	_pid="0"

	if [ "${sqlfile}" = "local" ]; then
		if [ -e "/dev/vmm/${jname}" ]; then
			# bhyve is active
			printf "${GREEN}"

			if [ "${racct}" = "1" ]; then
				memoryuse=0
				pcpu=0
				_pid=$( cbsdsql ${sqlfile} SELECT jid FROM jails WHERE jname=\"${jname}\" )
				if [ "${_pid}" = "0" -o "${_pid}" = "1" ]; then
					_val="0"
				else
					get_rctl ${_pid}
				fi
			fi
		else
			printf "${CYAN}"
		fi
	else
		# pop status variable from node_is_online()
		_md5_node_name=$( /sbin/md5 -q -s ${sqlfile} )
		eval _node_is_online=\$node_${_md5_node_name}_online
		if [ "${_node_is_online}" = "1" ]; then
			# retr from sql
			case ${jid} in
				0)
					printf "${CYAN}"
					;;
				*)
					printf "${GREEN}"
					;;
			esac
		else
			printf "${DGRAY}"
		fi
	fi

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

		# todo: remote sql fetch via retrinv
		#if [ "${sqlfile}" != "local" ]; then
		#	[ "${_i}" != "jname" ] && _val="x"
		#	printf "${_val} "
		#	continue
		#fi

		case "${_i}" in
			vm_ram)
				# convert to MB
				_val=$(( _val / 1024 / 1024 ))
				;;
			vm_curmem)
				# convert to MB
				if [ "${_pid}" != "0" ]; then
					_val=$(( memoryuse / 1024 / 1024 ))
					[ -z "${_val}" ] && _val=0
				else
					_val="0"
				fi
				;;
			pcpu)
				if [ "${_pid}" != "0" ]; then
					_val="${pcpu}"
					[ -z "${_val}" ] && _val=0
				else
					_val="0"
				fi
				;;
			vnc_port)
				if [ -r "${sqlfile_sysdir}/${jname}/vnc_port" ]; then
					_tmpport=$( /bin/cat ${sqlfile_sysdir}/${jname}/vnc_port )
					if [ -r ${sqlfile_sysdir}/${jname}/local.sqlite ]; then
						_tmpbind=$( cbsdsql ${sqlfile_sysdir}/${jname}/local.sqlite "SELECT bhyve_vnc_tcp_bind FROM settings ORDER BY (created) DESC LIMIT 1" 2>/dev/null )
						# we need for special route of tcpbind presentation for foreign bhyve. replace 0.0.0.0 by nodeip
						if [ "${sqlfile}" != "local" ]; then
							_tmpbind=$( cbsdsql ${sqlfile} "SELECT nodeip FROM local LIMIT 1" 2>/dev/null )
						fi
						_val="${_tmpbind}:${_tmpport}"
					else
						_val="Unk"
					fi
				else
					_val="0"
				fi
				;;
			ip4_addr)
				if [ -r ${sqlfile_sysdir}/${jname}/local.sqlite ]; then
					_val=$( cbsdsql ${sqlfile_sysdir}/${jname}/local.sqlite "SELECT ip4_addr FROM settings ORDER BY (created) DESC LIMIT 1" 2>/dev/null )
				else
					_val="Unk"
				fi
				[ "${_val}" = "(null)" ] && _val="DHCP"
				;;
			status)
				[ "${unregister}" = "1" ] && _val="Unregister"
				;;
		esac

		[ -z "${_val}" ] && _val="\-"

		printf "${_val} "
	done

	printf "${NORMAL}\n"
}


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

	#   set sqlfile for ". rcconf" including
	if [ -n "${1}" ]; then
		sqlfile="$1"
	else
		sqlfile="local"
	fi

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

	cbsdsql ${sqlfile} SELECT jname FROM jails WHERE emulator=\"bhyve\" ORDER BY jname ASC | while read jname; do
		_status=""
		. ${jrcconf}
		conv_status
		populate_output_data -j ${jname} -s ${sqlfile}
	done
}


show_local()
{
	local _errcode _status

	show_header

	show_jaildata_from_sql local

	# Unregister area
	[ ! -d "${jailrcconfdir}" ] && return 0
	ip4_addr="-"
	host_hostname="-"
	path="-"
	jid="0"
	vm_ram="-"
	vm_cpus="-"
	vm_os_type="-"
	path="-"
	status="-"
	vnc_port="-"

	for J in $( /bin/ls ${jailrcconfdir} ); do
		[ "${J}" = "dir.id" ] && continue
		jname=""
		[ ! -r ${jailrcconfdir}/${J} ] && continue
		. ${jailrcconfdir}/${J}
		# for bhyve vm only
		[ "${emulator}" != "bhyve" ] && continue
		[ -z "${jname}" ] && continue
		populate_output_data -u 1 -j ${jname}
		${ECHO} ${_status}
	done
}


show_remote()
{
	show_header

	[ -z "${node}" ] && node=$( cbsdsql nodes SELECT nodename FROM nodelist 2>/dev/null | /usr/bin/xargs )

	for _n in ${node}; do
		nodename="${_n}"
		# init and export into $node_${md5_node_name}_online node status
		node_is_online -n ${nodename} -e 1
		show_jaildata_from_sql ${_n}
	done
}

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

	if [ "${alljails}" = "1" ]; then
		show_local
		header=0
		show_remote
	else
		show_local
	fi
}

. ${workdir}/jrctl.subr

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

show_jails | /usr/bin/column -t
