#!/bin/bash

# STATUS SCRIPT
# The status script is OPTIONAL.
# It is used to return the status of the service (0 is healthy, 1 is unhealthy).
# It is called with the path to the server specific env file as an argument.


# This is an example of a status script that checks if the service is running.
source "$(dirname "$0")/_common.sh"
check_required_env_vars "CONTAINER_NAME"

# check if the service is running
_is_container_running $CONTAINER_NAME || die "Service is not running - did not find container $CONTAINER_NAME."

# determine port.
command -v jq &> /dev/null || die "jq could not be found, please install it"
[ -f "${CONFIG_PATH}/sos_config.json" ] || die "sos_config.json does not exist"
PORT=$(jq -r '.port' "${CONFIG_PATH}/sos_config.json")

# check if the service is healthy
curl -s -X GET http://localhost:${PORT}/status | jq -e '.result == "success"' \
     || die "Service is not healthy - did not get OK response from /status endpoint."

echo "Service is healthy"
exit 0