#!/bin/sh

# source of first version: https://www.reddit.com/r/suckless/comments/m6r71v/comment/gr7t2z1/?utm_source=reddit&utm_medium=web2x&context=3
# My changes:
# * fixed double notifications for critical level
# * added dunst stack tag
# * added dunst value for progress bar

# Control variable
# Possible values: NONE, FULL, LOW, CRITICAL
last="NONE"

# Default values for LOW/CRITICAL status
low=15
critical=10

while true; do

  # If battery is plugged, do stuff
  battery="/sys/class/power_supply/BAT0"
  if [ -d $battery ]; then

    capacity=$(cat $battery/capacity)
    status=$(cat $battery/status)

    # If battery full and not already warned about that
    if [ "$last" != "FULL" ] && [ "$status" = "Full" ]; then
      notify-send -h string:x-dunst-stack-tag:battery_notif -h "int:value:$last" "Battery full"
      last="FULL"
    fi

    # If low and discharging
    if [ "$last" != "LOW" ] && [ "$status" = "Discharging" ] && [ $capacity -le $low ]  && [ $capacity -gt $critical ]; then
      notify-send -h string:x-dunst-stack-tag:battery_notif -h "int:value:$last" "Battery low: $capacity%"
      last=LOW
    fi

    # If critical and discharging
    if [ "$status" = "Discharging" ] && [ $capacity -le $critical ]; then
      notify-send -h string:x-dunst-stack-tag:battery_notif -h "int:value:$last" -u critical "Battery very low: $capacity%"
      last=CRITICAL
    fi
  fi
  sleep 60
done