#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
LC_ALL=C

SCRIPTS_CHECK_PATHS=("/usr/lib/greenboot/check" "/etc/greenboot/check")
SCRIPTS_GREEN_PATHS=("/usr/lib/greenboot/green.d" "/etc/greenboot/green.d")
SCRIPTS_RED_PATHS=("/usr/lib/greenboot/red.d" "/etc/greenboot/red.d")
declare -A DIS_CHECK_STATUS=()

source_configuration_file() {
    greenboot_configuration_file=/etc/greenboot/greenboot.conf
    if test -f "$greenboot_configuration_file"; then
	# shellcheck source=/etc/greenboot/greenboot.conf
        source $greenboot_configuration_file
    fi
}

source_configuration_file
function init_disabled_map {
    for disabled_healthcheck in "${DISABLED_HEALTHCHECKS[@]}"; do
	DIS_CHECK_STATUS["$disabled_healthcheck"]=1;
    done
}

source_configuration_file
function print_unexecuted_checks {
    for disabled_healthcheck in "${DISABLED_HEALTHCHECKS[@]}"; do
	if [[  "${DIS_CHECK_STATUS[$disabled_healthcheck]}" == 1 ]]; then
    	    echo "WARNING: $disabled_healthcheck was not found and may be misspelled"
	fi
    done
}

source_configuration_file
function is_disabled {
    healthcheck=$1
    for disabled_healthcheck in "${DISABLED_HEALTHCHECKS[@]}"; do
        if [ "${healthcheck}" == "${disabled_healthcheck}" ]; then
	    DIS_CHECK_STATUS["${disabled_healthcheck}"]=0
	    return 0
	fi
    done
    return 1
}

init_disabled_map

script_runner () {
  local scripts_dir=$1; shift
  local mode=$1; shift
  local start_msg=$1; shift
  local required_hc_failed=false
  echo "$start_msg"
  for script in $(find "$scripts_dir" -name '*.sh' | sort); do
    if is_disabled "$(basename "$script")"; then
      echo "'$(basename "$script")' was skipped, as specified in config"
    else
      local rc=0
      systemd-cat -t "$(basename "$script")" bash "$script" || rc=$?
      if [ $rc -ne 0 ]; then
        local failure_msg
        failure_msg="Script '$(basename "$script")' FAILURE (exit code '$rc')"
        case "$mode" in
          "relaxed")
            echo "<2>$failure_msg. Continuing..." >&2
            ;;
          "strict")
            required_hc_failed=true
            echo "<0>$failure_msg. Continuing..." >&2
        esac
      else
        echo "<6>Script '$(basename "$script")' SUCCESS"
      fi
    fi
  done

  [[ $required_hc_failed == false ]]
}

case "$1" in
  "check")
    rc=0
    for health_check_path in "${SCRIPTS_CHECK_PATHS[@]}"; do
      script_runner "$health_check_path/required.d" "strict" "Running Required Health Check Scripts..." || rc=1
      script_runner "$health_check_path/wanted.d" "relaxed" "Running Wanted Health Check Scripts..."
    done
    print_unexecuted_checks
    exit $rc
    ;;
  "green")
    echo "<5>Boot Status is GREEN - Health Check SUCCESS"
    for green_path in "${SCRIPTS_GREEN_PATHS[@]}"; do
        script_runner "$green_path" "relaxed" "Running Green Scripts..."
    done
    ;;
  "red")
    echo "<0>Boot Status is RED - Health Check FAILURE!"
    for red_path in "${SCRIPTS_RED_PATHS[@]}"; do
        script_runner "$red_path" "relaxed" "Running Red Scripts..."
    done
    ;;
  *)
    echo "Unknown argument, exiting." >&2
    exit 1
esac
