#!/usr/bin/env bash

set -euo pipefail
# shellcheck source=./framework.sh
source "${BASE_DIR}/framework.sh"

temp_intel() {
    local cores out

    cores="$(echo "${sensors_output}" | grep Core | awk '{printf "%s ", $3} {printf "%s %s %s\n", $6, $9, $12}' | tr -d '+°C,)')"
    out=""

    while IFS= read -r line; do
        IFS=" " read -r current high critical <<< "${line}"

        if [[ "${high}" == "${critical}" ]]; then
            high=$(bc -l <<< "${high} - 20")
        fi

        out+="$(print_color "${current}°C" "${current}" "${high}" "${critical}"), "
    done <<< "${cores}"

    echo "${out::-2}"
}

temp_amd() {
    echo "${sensors_output}" | awk '/Tdie/ {print $2}' | tr -d '+'
}

sensors_output="$(sensors)"

if grep -q 'Tdie' <<< "${sensors_output}"; then
    temps=$(temp_amd)
else
    temps=$(temp_intel)
fi

print_columns "Temperatures" "${temps}"
