#!/usr/local/bin/cbsd
#v12.1.11
MYARG=""
MYOPTARG="cbsdfile cur_env cwd jname"
MYDESC="State of the cluster CBSDfile is managing, via CBSD API"
CBSDMODULE="sys"
ADDHELP="\
 (experimental)\n\
 cbsdfile - alternative path to CBSDfile, could be relative to the\n\
   working directory like: tmp/CBSDfile\n\
"
EXTHELP="wf_cbsdfile"

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

[ -z "${cur_env}" ] && cur_env=1

# init CBSDfile data
if [ -n "${cwd}" ]; then
	[ ! -r ${cwd}/CBSDfile ] && err 1 "${N1_COLOR}${CBSD_APP}: no such CBSDfile: ${N2_COLOR}${cwd}/CBSDfile${N0_COLOR}"
	cbsdfile="${cwd}/CBSDfile"
	cd ${cwd}
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=$( ${MD5_CMD} -qs "${CLOUD_KEY}" )
	# drop privileges to nobody
	${SU_CMD} -m nobody -c "${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
