#!/bin/bash

# This script checks ALL services on the server and returns a status for each.

# Return format is simple ENV with the following format:
# SERVICE_NAME_HEALTH=healthy|unhealthy|unknown
# SERVICE_NAME_PORTS=port1,port2,port3

# Get all services on the server
SCRIPT_DIR="$(dirname "$0")"

# // DROPSHELL_DIR
# //   |-- backups
# //   |-- services
# //       |-- service name
# //           |-- config  <-- this is passed as argument to all scripts
# //               |-- service.env
# //           |-- template
# //               |-- (script files)
# //               |-- example
# //                   |-- service.env
# //                   |-- (other config files for specific server&service)

CURRENT_OUTPUT=""
CURRENT_EXIT_CODE=0

load_dotenv(){
    local file_path=$1
    if [ -f "${file_path}" ]; then
        source "${file_path}"
    fi
}

function run_command() {
    local service_path=$1
    local command=$2
    local capture_output=${3:-false}  # default to false if not specified

    # check if the command is a file
    if [ ! -f "${service_path}/template/${command}.sh" ]; then
        return;
    fi

    # run the command in a subshell to prevent environment changes
    CURRENT_OUTPUT=$(
        set -a
        load_dotenv "${service_path}/template/_default.env"
        load_dotenv "${service_path}/config/service.env"
        set +a

        # update the main variables.
        export CONFIG_PATH="${service_path}/config"
        # SERVER is correct
        export SERVICE="${SERVICE_NAME}"
        
        if [ "$capture_output" = "true" ]; then
            # Capture and return output
            bash "${service_path}/template/${command}.sh" 2>&1
        else
            # Run silently and return exit code
            bash "${service_path}/template/${command}.sh" > /dev/null 2>&1
        fi
    )
    CURRENT_EXIT_CODE=$?
}

function command_exists() {
    local service_path=$1
    local command=$2
    if [ ! -f "${service_path}/template/${command}.sh" ]; then
        return 1
    fi
    return 0
}

# Get all services on the server
SERVICES_PATH=$(realpath "${SCRIPT_DIR}/../../")

# Get all service names
SERVICE_NAMES=$(ls "${SERVICES_PATH}")

# Iterate over all service names
for SERVICE_NAME in ${SERVICE_NAMES}; do

    SERVICE_PATH=$(realpath "${SERVICES_PATH}/${SERVICE_NAME}")

    #--------------------------------
    # Get the service health
    if ! command_exists "${SERVICE_PATH}" "status"; then
        SERVICE_HEALTH="unknown"
    else
        run_command "${SERVICE_PATH}" "status" "false"
        if [ "${CURRENT_EXIT_CODE}" -eq 0 ]; then
            SERVICE_HEALTH="healthy"
        else
            SERVICE_HEALTH="unhealthy"
        fi
    fi

    #--------------------------------
    # Get the service ports
    if ! command_exists "${SERVICE_PATH}" "ports"; then
        SERVICE_PORTS=""
    else
        run_command "${SERVICE_PATH}" "ports" "true"
        SERVICE_PORTS="${CURRENT_OUTPUT}"
    fi

    #--------------------------------
    # return the health and ports
    echo "${SERVICE_NAME}_HEALTH=${SERVICE_HEALTH}"
    echo "${SERVICE_NAME}_PORTS=${SERVICE_PORTS}"
done
