#!/usr/bin/env bash

# degrees in Celsius which we regard as too high, defaults to config
alertLevel="$1"
hostname=$(hostname)

# load in defaults from config
scriptRoot="$(dirname "$0")/.."
configFile="$scriptRoot/config.sh"
if [[ -f "${configFile}" ]]; then
    source "${configFile}"

    if [[ -z "${alertLevel}" ]]; then
        alertLevel="${THERMAL_ALERT}"
    fi
fi

# get current status
thermal=$("$scriptRoot/thermal.sh" "${alertLevel}")
alert="$?"

if [[ '0' -ne "${alert}" ]]; then
    # if currently in an alert status
    alertLevelF=$(echo "scale=1 ; ${alertLevel} * 9 / 5 + 32" | bc -l)
    echo "${thermal}" $'\n' "above threshold ${alertLevel} C (${alertLevelF} F)" | ntfy pub -T red_square -t "Thermal alert on $hostname" thermal
    touch "${HOME}/.thermal.alert"
elif [[ -f "${HOME}/.thermal.alert" ]]; then
    # if previously in an alert status
    alertLevelF=$(echo "scale=1 ; ${alertLevel} * 9 / 5 + 32" | bc -l)
    echo "${thermal}" $'\n' "previously above threshold ${alertLevel} C (${alertLevelF} F)" | ntfy pub -T orange_square -t "Thermal alert on $hostname" thermal
    rm "${HOME}/.thermal.alert"
fi

