#!/usr/bin/env bash

# Get system statistics
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print 100 - $8}' | cut -d. -f1)
MEMORY_USAGE=$(free -m | awk 'NR==2{printf "%.0f", $3*100/$2 }')

CPU_TEMP=""
# Get temp info (CPU temp)
if [ -r /sys/class/thermal/thermal_zone0/temp ]; then
    CPU_TEMP_RAW=$(cat /sys/class/thermal/thermal_zone0/temp)
    CPU_TEMP_C=$((CPU_TEMP_RAW / 1000))
    CPU_TEMP="        ${CPU_TEMP_C}°C"
fi


echo "  ${CPU_USAGE}%${CPU_TEMP}         ${MEMORY_USAGE}%"

# GPU info
GPU_USAGE=$(cat /sys/class/drm/card1/device/gpu_busy_percent)
GPU_VRAM_USED=$(cat /sys/class/drm/card1/device/mem_info_vram_used)
GPU_VRAM_TOTAL=$(cat /sys/class/drm/card1/device/mem_info_vram_total)
GPU_VRAM_USAGE=$((GPU_VRAM_USED * 100 / GPU_VRAM_TOTAL))


echo "  ${GPU_USAGE}%         ${GPU_VRAM_USAGE}%"

# Battery time: find battery device via upower and parse "time to full" / "time to empty"
# fallback to sysfs if upower not available
BAT_PATH=""
if command -v upower >/dev/null 2>&1; then
    BAT_PATH=$(upower -e | grep -E "battery|BAT" | head -n1)
fi

if [ -z "$BAT_PATH" ]; then
    # try linux sysfs battery status file
    if [ -r /sys/class/power_supply/BAT0/status ]; then
        BAT_STATUS_FILE="/sys/class/power_supply/BAT0/status"
    else
        BAT_STATUS_FILE=""
    fi
    BATTERY_STATUS=""
    if [ -n "$BAT_STATUS_FILE" ]; then
        BATTERY_STATUS=$(cat "$BAT_STATUS_FILE")
    fi
else
    # use upower to read battery properties
    BATTERY_STATUS=$(upower -i "$BAT_PATH" | awk -F: '/state:/ {gsub(/^[ \t]+|[ \t]+$/,"",$2); print $2; exit}')
fi

if [ "$BATTERY_STATUS" = "charging" ] || [ "$BATTERY_STATUS" = "Charging" ]; then
    if [ -n "$BAT_PATH" ]; then
        # get everything after the colon and trim
        TIME_TO_FULL=$(upower -i "$BAT_PATH" | sed -n 's/^[ \t]*time to full:[ \t]*//Ip')
    else
        TIME_TO_FULL=""
    fi
    if [ -n "$TIME_TO_FULL" ]; then
        echo "  ${TIME_TO_FULL} to full"
    else
        echo "  Charging"
    fi
elif [ "$BATTERY_STATUS" = "discharging" ] || [ "$BATTERY_STATUS" = "Discharging" ]; then
    if [ -n "$BAT_PATH" ]; then
        TIME_TO_EMPTY=$(upower -i "$BAT_PATH" | sed -n 's/^[ \t]*time to empty:[ \t]*//Ip')
    else
        TIME_TO_EMPTY=""
    fi
    if [ -n "$TIME_TO_EMPTY" ]; then
        echo "  ${TIME_TO_EMPTY} to empty"
    else
        echo "  Discharging"
    fi
fi

# Check if internet is up by pinging a reliable host
if ping -c 1 -W 1 1.1.1.1 > /dev/null 2>&1; then
    INTERNET_STATUS="up"
else
    INTERNET_STATUS="down"
fi

# Get WIFI SSID if connected
echo "  Internet: ${INTERNET_STATUS}"

# Get network info for each active interface
for IFACE in $(ls /sys/class/net/ | grep -E '^(en|wl|wg|tailscale|tun)'); do
    IFACE_STATE=""
    WLAN_SSID=""
    # safe read operstate
    if [ -r "/sys/class/net/$IFACE/operstate" ]; then
        IFACE_STATE=$(cat "/sys/class/net/$IFACE/operstate")
    fi
    if [ "$IFACE_STATE" = "up" ] && [[ "$IFACE" == wl* ]]; then
        # get SSID for wireless interfaces
        WLAN_SSID=$(iwgetid -r "$IFACE" 2>/dev/null)
        if [ -n "$WLAN_SSID" ]; then
            IFACE="${IFACE} (${WLAN_SSID})"
        fi
    fi

    # show physical interfaces when up, and always show virtual interfaces (tailscale/wg/tun)
    if [ "$IFACE_STATE" = "up" ] || [[ "$IFACE" == tailscale* ]] || [[ "$IFACE" == wg* ]] || [[ "$IFACE" == tun* ]]; then
        RX_BYTES=$(cat "/sys/class/net/$IFACE/statistics/rx_bytes" 2>/dev/null || echo 0)
        TX_BYTES=$(cat "/sys/class/net/$IFACE/statistics/tx_bytes" 2>/dev/null || echo 0)
        RX_HR=$(numfmt --to=iec --suffix=B "$RX_BYTES")
        TX_HR=$(numfmt --to=iec --suffix=B "$TX_BYTES")

        echo "  ${IFACE}: 󰇚 ${RX_HR} 󰕒 ${TX_HR}"
    fi
done

# curl https://wttr.in/Canberra\?format\="%l:+%t+UV:+%u"