#!/usr/bin/env bash
while true; do
    BATTERY=$(upower -i $(upower -e | grep BAT) | grep percentage | awk '{print $2}' | tr -d '%')
    SOUND_DIR="$HOME/battery_sounds"
    STATE_FILE="/tmp/battery_last_level"

    LAST_LEVEL=$(cat "$STATE_FILE" 2>/dev/null)

    if [ "$BATTERY" != "$LAST_LEVEL" ]; then
        if [ "$BATTERY" -lt "$LAST_LEVEL" ]; then # less - dropping
        #notify-send "Battery Alert" "Hello user; battery is currently at $BATTERY%..." --icon=dialog-warning

        #elif [ "$BATTERY" -gt "$LAST_LEVEL" ]; then # more - charging
        #notify-send "Battery Charging" "Battery is charging; battery is now at $BATTERY%!" --icon=dialog-information
        #fi

        SOUND="$SOUND_DIR/$BATTERY.wav"
        [ -f "$SOUND" ] && paplay "$SOUND"
        #kdialog --passivepopup "Hello user; battery is currently at $BATTERY%..." 5


        echo "$BATTERY" > "$STATE_FILE"
    fi

    sleep 60
done
