#!/bin/bash

# version 2
# written by fennek182
# for personal use
# waybar module to display gpu usage - for all gpus using mesa (intel)


round() {
    number="$(echo $1 | sed 's/\./,/')"
    printf "%.${2:-0}f" "$number"
}

gpu_usage=$(cat /sys/class/drm/card1/device/gpu_busy_percent)
gpu_temp_core=$(sensors | grep "edge" | awk '{ print $2 }' | cut -c 2-3) #edge temp, there's also junction and mem
gpu_temp_hotspot=$(sensors | grep "junction" | awk '{ print $2 }' | cut -c 2-3) #edge temp, there's also junction and mem
gpu_temp_mem=$(sensors | grep "mem" | awk '{ print $2 }' | cut -c 2-3) #edge temp, there's also junction and mem
gpu_clock=$(cat /sys/class/drm/card1/device/pp_dpm_sclk | grep "*" | awk '{ print $2 }')
gpu_power=$(sensors | grep "PPT" | awk '{ print $2 }')
gpu_power=$(echo "${gpu_power%%.*}")
# gpu_power=$(sensors | grep "PPT" | awk '{ print $2 }' | head -c2)
fan_current_rpm=$(sensors | grep "fan1" | awk '{ print $2 }')
fan_max_rpm=$(sensors | grep "fan1" | awk '{ print $10 }')
fan_duty=$(echo "$(awk "BEGIN { print "$fan_current_rpm/$fan_max_rpm*100" }" | head -c2 )")
# vram_size=$(nvidia-smi -q | grep 'Total' | head -n1 | awk '{print int($3/1000)}')
vram_size=20
vram_used=$(cat /sys/class/drm/card1/device/mem_info_vram_used)
# vram_used=$(echo $vram_used | sed 's/G//')

vram_used_percentage=$(echo "$(awk "BEGIN { print "$vram_used/21474836480*100" }" | cut -c1-4 )")
vram_used_percentage=$(round $vram_used_percentage 0)

# todo: use MB and TB insted of MiB and GiB and hide the G if it's >= 1 GB

# auto format to human readable file size unit
vram_used=$(printf %s\\n $vram_used | numfmt --to=si | sed 's/G*//g')
# vram_used=$(printf %s\\n $vram_used | numfmt --to=iec-i | sed 's/.$//')

# make usage human readable
# vram_used_mib=$(echo "$(awk "BEGIN { print "$vram_used/1024/1024" }" | head -c4)")

# when the usage in MiB is greater than 999MBiB, so over 1G
# if [ ${#vram_used/1024/1024} -gt 1000 ]; then
#     vram_used=$(echo "$(awk "BEGIN { print "$vram_used/1024/1024/1024" }")G")
#     else
#     vram_used=$(echo "$(awk "BEGIN { print "$vram_used/1024/1024" }" | head -c3 )M")
# fi


#echo "GPU   "$gpu_usage"%   $gpu_temp°C   "$gpu_clock"MHz   "$gpu_power"W   FAN   "$fan_duty"%   VRAM   "$vram_used_percentage"%   $vram_used" / "$vram_size"G


# hide core clock and vram usage if gpu usage is below 50%
# [[ $fan_duty == "0" ]] && echo ""$gpu_usage"%  $gpu_temp_core°C  "$gpu_power"W   󰈐 "$fan_duty"%"

# [[ $gpu_usage -lt 51 ]] && echo "$usage%  $power_package"
# [[ $gpu_usage -lt "51" ]] && echo ""$gpu_usage"%  $gpu_temp_core°C  "$gpu_power"W"

# when called from terminal, show the info and exit
# TODO: make this properly check if run from terminal

[[ "$1" == "-a" ]] && echo ""$gpu_usage"%  C: $gpu_temp_core°C  H: $gpu_temp_hotspot°C  M: $gpu_temp_mem°C  "$gpu_clock"  "$gpu_power"W   󰈐 "$fan_duty"%  "$fan_current_rpm"RPM   󰆼  "$vram_used_percentage"%  $vram_used"/"$vram_size"GB && exit

[[ $gpu_temp_core -lt "65" ]] && [[ $gpu_usage -lt "51" ]] && echo ""$gpu_usage"%  "$gpu_power"W"
[[ $gpu_temp_core -gt "66" ]] && echo ""$gpu_usage"%  $gpu_temp_core °C  "$gpu_power"W"

# [[ $gpu_usage -lt "51" ]] && echo ""$gpu_usage"%  $gpu_temp_core°C  "$gpu_power"W"
# [[ $gpu_usage -lt "50" ]] && echo ""$gpu_usage"%  $gpu_temp°C  "$gpu_power"W   󰈐 "$fan_duty"%"
# [[ $gpu_usage -lt "50" ]] && echo ""$gpu_usage"%  $gpu_temp°C  "$gpu_power"W   󰈐 "$fan_duty"%  "$fan_current_rpm"RPM"

# echo "GPU  "$gpu_usage"%  $gpu_temp°C  "$gpu_clock"MHz  "$gpu_power"W  FAN "$fan_duty"%  VRAM "$vram_used_percentage"%  $vram_used"/"$vram_size"G
# echo ""$gpu_usage"%  $gpu_temp°C  "$gpu_clock"MHz  "$gpu_power"W  FAN "$fan_duty"%  VRAM "$vram_used_percentage"%  $vram_used"/"$vram_size"G
# echo ""$gpu_usage"%  $gpu_temp_core°C  "$gpu_clock"  "$gpu_power"W   󰈐 "$fan_duty"%  "$fan_current_rpm"RPM    󰆼  "$vram_used_percentage"%  $vram_used"/"$vram_size"G

# older output with more withspace for the temps - used until 2026-05-04:
# echo ""$gpu_usage"%  C: $gpu_temp_core°C  H: $gpu_temp_hotspot°C  M: $gpu_temp_mem°C  "$gpu_clock"  "$gpu_power"W   󰈐 "$fan_duty"%  "$fan_current_rpm"RPM   󰆼  "$vram_used_percentage"%  $vram_used"/"$vram_size"GB
echo ""$gpu_usage"%  C/H/M: $gpu_temp_core/$gpu_temp_hotspot/$gpu_temp_mem°C  "$gpu_clock"  "$gpu_power"W   󰈐 "$fan_duty"%  "$fan_current_rpm"RPM   󰆼 "$vram_used_percentage"%  $vram_used"GB

#
