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

  Alternative view for [bjq]ls commands

Example:

  cbsd cluster emulator=jail

"

. ${subrdir}/nc.subr
. ${strings}
. ${tools}
emulator=
cwd=
jname=
CLOUD_URL=
CLOUD_KEY=
file=
cixinit

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
	_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' > ${ftmpdir}/servers.json.$$
	else
		echo '{}' > ${ftmpdir}/servers.json.$$
	fi
	${CAT_CMD} > ${ftmpdir}/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} ${ftmpdir}/servers.json.$$ | ${JQ_CMD} '{ servers: . }' > ${ftmpdir}/servers1.json.$$
	${CAT_CMD} ${ftmpdir}/clusters.json.$$ | ${JQ_CMD} '{ clusters: . }' > ${ftmpdir}/clusters1.json.$$

	if [ -n "${file}" ]; then
		${JQ_CMD} -s '.[0] * .[1]' ${ftmpdir}/servers1.json.$$ ${ftmpdir}/clusters1.json.$$ > ${file}.$$
		${MV_CMD} ${file}.$$ ${file}
	else
		${JQ_CMD} -s '.[0] * .[1]' ${ftmpdir}/servers1.json.$$ ${ftmpdir}/clusters1.json.$$
	fi
	${RM_CMD} -f ${ftmpdir}/servers.json.$$ ${ftmpdir}/clusters.json.$$ ${ftmpdir}/servers1.json.$$ ${ftmpdir}/clusters1.json.$$
}

## MAIN
# 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 "${CIX_PWD}/CBSDfile" ]; then
	local_cluster
	exit 0
fi

. ${subrdir}/cbsdfile.subr
. ${subrdir}/time.subr

cluster_status()
{
	local _cid=

	[ -z "${CURL_CMD}" ] && CURL_CMD=$( which curl )
	[ -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}${CIX_APP}: empty CLOUD_URL${N0_COLOR}"
[ -z "${CLOUD_KEY}" ] && err 1 "${N1_COLOR}${CIX_APP}: empty CLOUD_KEY${N0_COLOR}"

cluster_status

exit 0
