#!/bin/bash

## author moonarch.de
## date 2024-08-16
## loop battery devices and send notification on low percentage

NOTIFY_AT_PERCENTAGE=70

ICON="/usr/share/icons/Tela-purple-dark/symbolic/status/battery-empty-symbolic.svg"

for d in $(upower -e);
do 
  DEVICE_DATA=$(upower -i "$d");
  PERCENTAGE=$(echo $DEVICE_DATA | grep -Po '(?<=(percentage: )).*(?= icon)')
  PER_INT=$(echo "${PERCENTAGE//%}")
  DEVICE_NAME=$(echo $DEVICE_DATA | grep -Po '(?<=(model: )).*(?= serial)')

  if [ ! -z $DEVICE_NAME ] && [ $PER_INT -lt $NOTIFY_AT_PERCENTAGE ]; then
    notify-send -t 5000 -e "Low battery $DEVICE_NAME $PER_INT%" -i "$ICON" -h string:x-canonical-private-synchronous:battery -h int:value:"$PER_INT" -u critical
  fi
done