98 lines
2.1 KiB
Bash
Executable File
98 lines
2.1 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
rundir="$RUNDIR/batwarn"
|
|
logfile="$LOGDIR/batwarn.log"
|
|
|
|
_PATH_WARN_1="$rundir/batwarn1"
|
|
_PATH_WARN_2="$rundir/batwarn2"
|
|
|
|
_THRESHOLD1=25
|
|
_THRESHOLD2=5
|
|
|
|
acpi_output=$(acpi -b)
|
|
|
|
log() {
|
|
printf '%s\n' "[$(date +%FT%T)] $*" >> $logfile
|
|
}
|
|
|
|
charging() {
|
|
[[ "$status" == "C" ]]
|
|
}
|
|
|
|
discharging() {
|
|
[[ "$status" == "D" ]]
|
|
}
|
|
|
|
threshold1() {
|
|
(( $percent <= $_THRESHOLD1 ))
|
|
}
|
|
|
|
threshold2() {
|
|
(( $percent <= $_THRESHOLD2 ))
|
|
}
|
|
|
|
notify_threshold1() {
|
|
notify-send --icon dialog-warning "Battery below ${_THRESHOLD2}%" --expire-time 0
|
|
}
|
|
|
|
notify_threshold2() {
|
|
notify-send --icon dialog-warning "Battery below ${_THRESHOLD1}%" --expire-time 30000
|
|
}
|
|
|
|
if [[ -n "$acpi_output" ]] ; then
|
|
has_battery=1
|
|
percent="$(printf '%s' "$acpi_output" | grep -oP '\d+(?=%)')"
|
|
if [[ $percent == 100 ]] ; then
|
|
status="F"
|
|
else
|
|
status="$(printf '%s' "$acpi_output" | grep -oP '(?<=: )\w+(?=,)' | cut -c 1 )"
|
|
fi
|
|
else
|
|
has_battery=0
|
|
fi
|
|
|
|
if (( has_battery )) ; then
|
|
if discharging ; then
|
|
if threshold2 ; then
|
|
if [[ ! -f "$_PATH_WARN_2" ]] ; then
|
|
log "battery fell below $_THRESHOLD2 percent. issuing warning."
|
|
touch "$_PATH_WARN_2"
|
|
notify_threshold1
|
|
fi
|
|
elif threshold1 ; then
|
|
if [[ ! -f "$_PATH_WARN_1" ]] ; then
|
|
log "battery fell below $_THRESHOLD1 percent. issuing warning."
|
|
touch "$_PATH_WARN_1"
|
|
notify_threshold2
|
|
fi
|
|
fi
|
|
elif charging ; then
|
|
if [[ -f "$_PATH_WARN_1" ]] ; then
|
|
log "charging now. resetting warnings."
|
|
rm "$_PATH_WARN_1"
|
|
fi
|
|
[[ -f "$_PATH_WARN_2" ]] && rm "$_PATH_WARN_2"
|
|
fi
|
|
|
|
if threshold2 ; then
|
|
color="#FF0000" # red
|
|
urgent=1
|
|
elif threshold1 ; then
|
|
color="#FFFF00" # yellow
|
|
urgent=0
|
|
else
|
|
color="#FFFFFF" # white
|
|
urgent=0
|
|
fi
|
|
|
|
printf '%s\n' "${status} ${percent}%"
|
|
printf '%s\n'
|
|
printf '%s\n' $color
|
|
(( $urgent )) && exit 33
|
|
exit 0
|
|
else
|
|
printf '%s\n' "no battery"
|
|
printf '%s\n'
|
|
printf '%s\n' "#FFFFFF"
|
|
fi
|