#!/bin/bash

# Query upower for the BAT1 battery; awk strips the "%" so we get a plain number.
perc=$(upower -i /org/freedesktop/UPower/devices/battery_BAT1 | awk '/percentage/ {gsub("%",""); print $2}')
# State can be "charging", "discharging", "fully-charged", etc.
state=$(upower -i /org/freedesktop/UPower/devices/battery_BAT1 | awk '/state/ {print $2}')

# Check if values are not empty
if [ -z "$perc" ] || [ -z "$state" ]; then
    echo "Battery info unavailable"
    exit 1
fi

# Strip any fractional part for integer comparisons below (upower may return "42.00").
num=${perc%%.*}  # In case perc is float

# Show a plug/charging icon regardless of level while the charger is connected.
if [ "$state" == "charging" ]; then
    echo "󰂄 ${perc}%"
else
    # Map the level to a Nerd Font battery icon — 10% steps, critical at ≤10%.
    if [ "$num" -gt 95 ]; then
        echo "󰁹 ${perc}%"
    elif [ "$num" -gt 90 ]; then
        echo "󰂂 ${perc}%"
    elif [ "$num" -gt 80 ]; then
        echo "󰂁 ${perc}%"
    elif [ "$num" -gt 70 ]; then
        echo "󰂀 ${perc}%"
    elif [ "$num" -gt 60 ]; then
        echo "󰁿 ${perc}%"
    elif [ "$num" -gt 50 ]; then
        echo "󰁾 ${perc}%"
    elif [ "$num" -gt 40 ]; then
        echo "󰁽 ${perc}%"
    elif [ "$num" -gt 30 ]; then
        echo "󰁼 ${perc}%"
    elif [ "$num" -gt 20 ]; then
        echo "󰁻 ${perc}%"
    elif [ "$num" -gt 10 ]; then
        echo "󰁺 ${perc}%"
    else
        # At ≤10% fire a critical urgency desktop notification via dunst/libnotify.
        notify-send --urgency=critical -t 2000 "󱃍 low battery, please charge"
        echo "󰂎 ${perc}%"
    fi
fi

