blob: 114cf9b0b0a7f0bcb59d7a2d87e48f20c5d69d56 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#!/bin/bash
default_capacity_threshold=50
if [ -e /usr/lib/bash/sleep ]
then
enable -f /usr/lib/bash/sleep sleep
else
echo 'Warning: not found: /usr/lib/bash/sleep (try: "sudo apt install bash-builtins")' >&2
fi
main()
{
capacity_threshold=${1:-$default_capacity_threshold}
while true
do
check_batteries /sys/class/power_supply/BAT*
sleep 1
done
}
check_batteries()
{
for dir in "$@"
do
[ -d "$dir" ] || continue
check_battery "$dir"
done
}
check_battery()
{
battery_dir=$1
read status < "$battery_dir"/status || return
[ "$status" = Discharging ] || return 0
read capacity < "$battery_dir"/capacity || return
case "$capacity" in
'' | *[^0-9]*)
echo "Error: unexpected value for battery capacity: $capacity" >&2
return 1
;;
esac
if [ "$capacity" -lt "$capacity_threshold" ]
then
systemctl suspend
fi
}
main "$@"
|