configfiles/scripts/.config/scripts/batteryobserver.sh

40 lines
970 B
Bash
Executable file

#!/bin/bash
#Config
WARNING_LEVEL=10
SHUTDOWN_LEVEL=2
#End config
STATE=0
while true; do
level=$(acpi -b | awk -F, 'BEGIN{s=0}{s+=$2}END{print int(s/NR)}')
charger_connected=$(acpi -b | grep Charging)
case "$STATE" in
0) # battery ok
if [ "$level" -le "$WARNING_LEVEL" -a -z "$charger_connected" ]; then
zenity --warning --text="Your battery is getting low\!" --no-wrap &
STATE=1
fi
;;
1) # battery low
if [ -n "$charger_connected" ]; then
STATE=0
elif [ "$level" -le "$SHUTDOWN_LEVEL" ]; then
systemctl hibernate;
else
notify-send -u critical -a "BatterySystem" "Battery is low!" "Please connect a charger"
fi
;;
esac
if [ $level -gt 50 ]; then
sleep 40m
elif [ $level -gt 30 ]; then
sleep 10m
else
sleep 2m
fi
done