#!/bin/bash
# GPU monitoring for waybar (AMD radeontop + sysfs temp)

GPU_DATA=$(timeout 1 radeontop -d - -l 1 2>/dev/null | tail -1)

if [ -z "$GPU_DATA" ]; then
    echo "{\"text\": \"gpu:--\", \"tooltip\": \"Reading...\"}"
    exit 0
fi

GPU=$(echo "$GPU_DATA" | grep -oP 'gpu \K[0-9.]+' | head -1)
GPU_INT=${GPU%.*}

VRAM=$(echo "$GPU_DATA" | grep -oP 'vram [\d.]+% \K[\d.]+' | head -1)
VRAM_INT=${VRAM%.*}

SCLK=$(echo "$GPU_DATA" | grep -oP 'sclk [\d.]+% \K[\d.]+' | head -1)
MCLK=$(echo "$GPU_DATA" | grep -oP 'mclk [\d.]+% \K[\d.]+' | head -1)

TEMP=$(( $(cat /sys/class/hwmon/hwmon2/temp1_input) / 1000 ))
POWER=$(( $(cat /sys/class/hwmon/hwmon2/power1_average) / 1000 ))

echo "{\"text\": \"gpu:${GPU_INT}% ${VRAM_INT}MB ${TEMP}C\", \"tooltip\": \"GPU: ${GPU}%\\nVRAM: ${VRAM}MB\\nTemp: ${TEMP}C\\nPower: ${POWER}W\\nSCLK: ${SCLK}GHz\\nMCLK: ${MCLK}GHz\"}"
