2022-05-27 21:07:07 +02:00
#!/bin/bash
# battmon, the battery monitor 🦇
BAT_LEVEL_WARN=${BAT_LEVEL_WARN:=20}
BAT_LEVEL_CRITICAL=${BAT_LEVEL_CRITICAL:=10}
function _acpib() {
acpi -b | grep -Fv ' 0%'
}
function isDischarging() {
[[ "$(_acpib)" == *"Discharging"* ]] && echo true || echo false
}
function getLevel() {
_acpib | sed -ne 's/.* [0-9].* \([0-9]*\)%.*/\1/p'
}
function getLevelMessage() {
echo "The battery level is $(getLevel)%"
}
function levelIsWarn() {
[[ $(getLevel) -le $BAT_LEVEL_WARN ]] && echo true || echo false
}
function levelIsCritical() {
[[ $(getLevel) -le $BAT_LEVEL_CRITICAL ]] && echo true || echo false
}
2022-05-29 00:32:02 +02:00
if ! [[ $(_acpib | wc -l) -gt 0 ]]; then
2022-05-27 21:07:07 +02:00
echo "No batteries installed."
exit 0
fi
while true; do
if eval "$(isDischarging)"; then
if eval "$(levelIsCritical)"; then
i3-nagbar -t warning -m "$(getLevelMessage) which is below the critical level of $BAT_LEVEL_CRITICAL%. The computer will be suspended in 30 seconds unless charged."
sleep 30
eval "$(levelIsCritical)" && systemctl suspend
elif eval "$(levelIsWarn)"; then
notify-send "$(getLevelMessage)"
fi
fi
sleep 3
done