summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoru <u@billy>2023-04-09 10:31:57 -0400
committeru <u@billy>2023-04-09 10:31:57 -0400
commitd03defeecd0377373ad5c6d8efe0ff8b6aa844ee (patch)
treec634c1bd5e2c7f67310171503e7324ada3664e8f
initial
-rwxr-xr-xgo.sh58
1 files changed, 58 insertions, 0 deletions
diff --git a/go.sh b/go.sh
new file mode 100755
index 0000000..686127f
--- /dev/null
+++ b/go.sh
@@ -0,0 +1,58 @@
1#!/bin/sh
2capacity_threshold=90
3
4battery_dir=/sys/class/power_supply/BAT0
5status_file=$battery_dir/status
6capacity_file=$battery_dir/capacity
7
8UNIT=powerloss-handler
9CMD=$0
10
11self_install()
12{
13 systemctl --user reset-failed "$UNIT" 2>/dev/null
14 systemd-run --user \
15 --unit "$UNIT" \
16 --property Restart="on-failure" \
17 -- "$CMD"
18}
19
20go()
21{
22
23 read capacity < "$capacity_file" || return
24 case "$capacity" in
25 '' | *[^0-9]*)
26 echo "Error: unexpected value for battery capacity: $capacity" >&2
27 return 1 ;;
28 esac
29
30 if [ -t 2 ]
31 then
32 printf ' %.2d%% \r' "$capacity" >&2
33 fi
34
35 read status < "$status_file" || return
36 [ "$status" = Discharging ] || return 0
37
38 if [ "$capacity" -lt "$capacity_threshold" ]
39 then
40 systemctl suspend
41 fi
42}
43
44main()
45{
46 while true
47 do
48 go
49 sleep 1
50 done
51}
52
53if [ "$1" = install ]
54then
55 self_install
56else
57 main
58fi