#!/usr/local/bin/cbsd
#v12.1.11
MYARG=""
MYOPTARG="emulator"
MYDESC="Alternative view for [bjq]ls commands"
CBSDMODULE="sys"
ADDHELP="
  Alternative view for [bjq]ls commands

Example:

  cbsd cluster emulator=jail

"

. ${subrdir}/nc.subr
. ${strings}
emulator=
cwd=
jname=
CLOUD_URL=
CLOUD_KEY=
. ${cbsdinit}

[ -z "${JQ_CMD}" ] && JQ_CMD=$( which jq )
[ -z "${JQ_CMD}" ] && err 1 "${N1_COLOR}${CBSD_APP} error: jq requred${N0_COLOR}"

local_cluster()
{
	local _env_list=
	if [ -n "${emulator}" ]; then
		case "${emulator}" in
			jail)
				_env_list=$( jorder )
				;;
			bhyve)
				_env_list=$( border )
				;;
			qemu)
				_env_list=$( qorder )
				;;
			xen)
				_env_list=$( xorder )
				;;
			virtualbox)
				_env_list=$( vorder )
				;;
		esac
	else
		local _jails=$( jorder )
		local _bhyve=$( border )
		local _qemu=$( qorder )
		local _xen=$( xorder )
		_env_list="${_jails} ${_bhyve} ${_qemu} ${_xen}"
	fi

	local _total_env=0
	local _json_files=


	for i in ${_env_list}; do
		_total_env=$(( _total_env + 1 ))
	done
#${CAT_CMD} <<EOF
#{
#  "servers":
#EOF
	_json_files=

	if [ ${_total_env} -ne 0 ]; then

		for i in ${_env_list}; do
			[ ! -r "${jailsysdir}/${i}/stats.json" ] && save-jail-info jname=${i}
			if [ -r "${jailsysdir}/${i}/stats.json" ]; then
				_json_files="${_json_files} ${jailsysdir}/${i}/stats.json"
			fi
		done

		${CAT_CMD} ${_json_files} | ${JQ_CMD} -s 'flatten' > /tmp/servers.json.$$
	else
		echo '{}' > /tmp/servers.json.$$
	fi

${CAT_CMD} > /tmp/clusters.json.$$ <<EOF
  {
    "total_environment": ${_total_env},
    "total_cpus": 6,
    "total_ram_bytes": 3221225472,
    "total_ram": "3 Gb",
    "total_imgsize_bytes": 32212254720,
    "total_imgsize": "30 Gb"
  }
EOF

	${CAT_CMD} /tmp/servers.json.$$ | ${JQ_CMD} '{ servers: . }' > /tmp/servers1.json.$$
	${CAT_CMD} /tmp/clusters.json.$$ | ${JQ_CMD} '{ clusters: . }' > /tmp/clusters1.json.$$
	${JQ_CMD} -s '.[0] * .[1]' /tmp/servers1.json.$$ /tmp/clusters1.json.$$

	${RM_CMD} -f /tmp/servers.json.$$ /tmp/clusters.json.$$ /tmp/servers1.json.$$ /tmp/clusters1.json.$$
}

# init CBSDfile data
if [ -n "${cwd}" ]; then
	if [ ! -r ${cwd}/CBSDfile ]; then
		local_cluster
		exit 0
	fi
	cbsdfile="${cwd}/CBSDfile"
	cd ${cwd}
fi

if [ -z "${cbsdfile}" -a ! -r "${CBSD_PWD}/CBSDfile" ]; then
	local_cluster
	exit 0
fi
. ${subrdir}/cbsdfile.subr

. ${subrdir}/time.subr

cluster_status()
{
	local CURL_CMD=$( which curl )
	local _cid

	[ -z "${CURL_CMD}" ] && err 1 "${N1_COLOR}cloud up requires curl, please install: ${N2_COLOR}pkg install -y curl${N0_COLOR}"
	[ -z "${CBSDFILE_RECURSIVE}" ] && ${ECHO} "${N1_COLOR}main cloud api: ${N2_COLOR}${CLOUD_URL}${N0_COLOR}" 1>&2
	_cid=$( ${miscdir}/cbsd_md5 "${CLOUD_KEY}" )
	# drop privileges to nobody
	${SUDO_CMD} -u nobody ${CURL_CMD} --no-progress-meter -H "cid:${_cid}" ${CLOUD_URL}/api/v1/cluster
}

[ -z "${CLOUD_URL}" ] && err 1 "${N1_COLOR}${CBSD_APP}: empty CLOUD_URL${N0_COLOR}"
[ -z "${CLOUD_KEY}" ] && err 1 "${N1_COLOR}${CBSD_APP}: empty CLOUD_KEY${N0_COLOR}"

cluster_status

exit 0
