#!/bin/bash

round() {
    printf "%.${2:-0}f" "$1"
}

temp_cpu=$(echo "$(sensors | grep Tctl | awk '{print $2}' | cut -c 2-3)")
# temp_liquid=$(echo "$(liquidctl status | grep 'Liquid' | awk '{print $4}')°C")
# rpm_fan=$(echo "$(liquidctl status | grep 'Fan' | awk '{print $4}')RPM")
# rpm_pump=$(echo "$(liquidctl status | grep 'Pump' | awk '{print $4}')RPM")
power_core=$(echo "$(sensors | grep SVI2_P_Core | awk '{print $2}')")
power_soc=$(echo "$(sensors | grep SVI2_P_SoC | awk '{print $2}')")
power_package=$(echo "$power_core + $power_soc" | bc)
power_package="$(round $power_package 0)W"
#usage=$(echo "$(grep 'cpu ' /proc/stat | awk '{cpu_usage=($2+$4)*100/($2+$4+$5)} END {printf "%0.2f%", cpu_usage}')")

temp_liquid=$(echo "$(sensors | grep 'Coolant' | awk '{print $2}' | cut -c 2-)")
temp_liquid=$(round $temp_liquid 0)

rpm_fan=$(echo "$(sensors | grep 'Fan' | awk '{print $2}')RPM")
# rpm_fan=$(echo "$(liquidctl status | grep 'Fan' | awk '{print $4}')RPM")
rpm_pump=$(echo "$(sensors | grep 'Pump' | awk '{print $2}')RPM")

#deprecated:
#clock=$(lscpu | grep 'CPU MHz' | awk '{ print $3 }' | head -c4) 

# clock=$(echo "$(cat /proc/cpuinfo | grep MHz | sort -r | head -n1 | awk '{ print $4 }' | cut -c-4)"Mhz)
clock=$(cat /proc/cpuinfo | grep MHz | sort -r | head -n1 | awk '{ print $4 }' | cut -c-4)

#divide clock in Mhz by 1000 for Ghz
# clock_ghz=$( echo "scale=1; $clock/1000" | bc -l )
clock_ghz="$( echo "scale=1; $clock/1000" | bc -l | sed 's/\./,/g' )GHz"

# Get number of cores
CORECOUNT=$(grep -c ^processor /proc/cpuinfo)
# Use top, skip the first 7 rows, count the sum of the values
#   in column 9 - the CPU column, do some simple rounding at the end
usage=$(top -bn 1 | awk -v n=$CORECOUNT 'NR > 7 { s += $9 } END { print int(s / n + .5); }')


# echo "CPU  $usage%  CORE $temp_cpu  LIQUID  $temp_liquid  FAN $rpm_fan  PUMP $rpm_pump  $clock Mhz  $power_package"
# echo "CPU  $usage%  $temp_cpu  $clock  $power_package"
# [[ "$temp_cpu" -gt 50 || %usage -gt 33 ]] && echo "$usage%  $power_package" && exit
# [[ "$temp_cpu" -gt 50 || %usage -gt 33 ]] && echo "$usage%  $power_package" || && exit

# [[ "$temp_liquid" -gt 35 ]] && echo "$usage%  $temp_cpu°C   $temp_liquid°C  $power_package" && exit || echo "$usage%  $temp_cpu°C  $power_package"

[[ "$temp_liquid" -gt 38 ]] && echo "$usage%  $temp_cpu°C   $temp_liquid°C  $power_package" && exit || echo "$usage%  $power_package"
echo "$usage%  $temp_cpu°C   $temp_liquid°C  󰈐 $rpm_fan  $clock_ghz  $power_package"

#
