summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoru <u@billy>2023-05-01 10:55:01 -0400
committeru <u@billy>2023-05-01 10:55:01 -0400
commit8bd7314490e12d5501818e597144ffd5acc75564 (patch)
treece8462c3b5b8a704d6e33055cbffdd241f40d21a
parenta1d4d87f480e932960d037e6401db87b92fdf4f6 (diff)
separate service, executable, and installer
-rw-r--r--Makefile22
-rwxr-xr-xgo.sh104
-rwxr-xr-xsuspend-below-battery-capacity51
-rw-r--r--suspend-below-battery-capacity.service9
4 files changed, 82 insertions, 104 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..c876ade
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,22 @@
1UNIT = suspend-below-battery-capacity
2BINDIR = /usr/local/bin
3BINARIES = $(UNIT)
4.PHONY: all install-bin install
5
6all:
7 @echo "run 'make install' to install"
8
9ifeq ($(shell id -u),0)
10
11install: $(UNIT).service install-bin
12 systemctl enable --now $(shell realpath -e $<)
13
14install-bin:
15 install -t $(BINDIR) -- $(BINARIES)
16
17else
18
19install install-bin:
20 sudo $(MAKE) -$(MAKEFLAGS) $@
21
22endif
diff --git a/go.sh b/go.sh
deleted file mode 100755
index b9d6aea..0000000
--- a/go.sh
+++ /dev/null
@@ -1,104 +0,0 @@
1#!/bin/bash
2default_capacity_threshold=50
3capacity_threshold_file=$HOME/.config/powerloss-handler/capacity_threshold
4
5battery_dir=/sys/class/power_supply/BAT0
6status_file=$battery_dir/status
7capacity_file=$battery_dir/capacity
8
9UNIT=powerloss-handler
10CMD=$0
11
12FORCE_SUSPEND=
13
14self_install()
15{
16 if systemctl --user is-active "$UNIT" >/dev/null
17 then
18 systemctl --user stop "$UNIT"
19 fi
20 systemctl --user reset-failed "$UNIT" 2>/dev/null
21 systemd-run --user \
22 --unit "$UNIT" \
23 --property Restart="on-failure" \
24 -- "$CMD"
25}
26
27self_test()
28{
29 FORCE_SUSPEND=y
30 go
31 echo
32}
33
34do_suspend()
35{
36 systemctl suspend
37}
38
39go()
40{
41 read capacity < "$capacity_file" || return
42 case "$capacity" in
43 '' | *[^0-9]*)
44 echo "Error: unexpected value for battery capacity: $capacity" >&2
45 return 1 ;;
46 esac
47 read status < "$status_file" || return
48
49 if [ "$FORCE_SUSPEND" ]
50 then
51 status=Discharging
52 capacity=0
53 echo 'Warning: test: pretending battery is discharging at 0% capacity' >&2
54 fi
55
56 if [ -t 2 ]
57 then
58 printf '\r %20s %.2d%%\r' "$status" "$capacity" >&2
59 fi
60
61 if [ -f "$capacity_threshold_file" ]
62 then
63 read capacity_threshold < "$capacity_threshold_file"
64 else
65 capacity_threshold=$default_capacity_threshold
66 fi
67
68 [ "$status" = Discharging ] || return 0
69 if [ "$capacity" -lt "$capacity_threshold" ]
70 then
71 do_suspend
72 fi
73}
74
75main()
76{
77 if [ -e /usr/lib/bash/sleep ]
78 then
79 enable -f /usr/lib/bash/sleep sleep
80 else
81 echo 'Warning: not found: /usr/lib/bash/sleep (try: "sudo apt install bash-builtins")' >&2
82 fi
83
84 while true
85 do
86 go
87 sleep 1
88 done
89}
90
91if [ $# = 0 ]
92then
93 main
94else
95 case "$1" in
96 run) main ;;
97 install) self_install ;;
98 test) self_test ;;
99 *) echo "Error: unknown command: $*" >&2
100 echo "Valid commands: run install test" >&2
101 exit 1
102 ;;
103 esac
104fi
diff --git a/suspend-below-battery-capacity b/suspend-below-battery-capacity
new file mode 100755
index 0000000..114cf9b
--- /dev/null
+++ b/suspend-below-battery-capacity
@@ -0,0 +1,51 @@
1#!/bin/bash
2default_capacity_threshold=50
3
4if [ -e /usr/lib/bash/sleep ]
5then
6 enable -f /usr/lib/bash/sleep sleep
7else
8 echo 'Warning: not found: /usr/lib/bash/sleep (try: "sudo apt install bash-builtins")' >&2
9fi
10
11main()
12{
13 capacity_threshold=${1:-$default_capacity_threshold}
14 while true
15 do
16 check_batteries /sys/class/power_supply/BAT*
17 sleep 1
18 done
19}
20
21check_batteries()
22{
23 for dir in "$@"
24 do
25 [ -d "$dir" ] || continue
26 check_battery "$dir"
27 done
28}
29
30check_battery()
31{
32 battery_dir=$1
33
34 read status < "$battery_dir"/status || return
35 [ "$status" = Discharging ] || return 0
36
37 read capacity < "$battery_dir"/capacity || return
38 case "$capacity" in
39 '' | *[^0-9]*)
40 echo "Error: unexpected value for battery capacity: $capacity" >&2
41 return 1
42 ;;
43 esac
44
45 if [ "$capacity" -lt "$capacity_threshold" ]
46 then
47 systemctl suspend
48 fi
49}
50
51main "$@"
diff --git a/suspend-below-battery-capacity.service b/suspend-below-battery-capacity.service
new file mode 100644
index 0000000..bd42596
--- /dev/null
+++ b/suspend-below-battery-capacity.service
@@ -0,0 +1,9 @@
1[Unit]
2Description = Suspend machine when battery capacity drops below threshhold
3
4[Service]
5ExecStart = suspend-below-battery-capacity 50
6Restart = on-failure
7
8[Install]
9WantedBy = default.target