#!/bin/bash

BAT=$(ls /sys/class/power_supply | grep -E '^BAT' | head -n 1)

while true; do

  capacity=$(cat "/sys/class/power_supply/${BAT}/capacity")
  BATSTATUS=$(cat "/sys/class/power_supply/${BAT}/status")

  if [ -f "/sys/class/power_supply/${BAT}/power_now" ]; then
    power_now=$(cat "/sys/class/power_supply/${BAT}/power_now")
    power_watt=$(echo "scale=1; $power_now / 1000000" | bc)
  else
    power_watt="N/A"
  fi

  if [ "$BATSTATUS" == "Discharging" ]; then
    if [ "$capacity" -le 20 ]; then
      notify-send -u critical "Battery Warning!" \
        "Battery is at ${capacity}% discharging at a rate of ${power_watt}W."
    fi
  fi

  sleep 120
done
