summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@cryptonomic.net>2023-04-18 10:46:12 -0400
committerAndrew Cady <d@cryptonomic.net>2023-04-18 10:46:12 -0400
commit3db59345749db3edc45097962a3ade98211aa937 (patch)
tree09f00f71712306838c21aa8e4ae18c2139da652f
parent1e9cb44dc75c6edc83703bb93ca6946368c0fbec (diff)
add "test" subcommand
-rwxr-xr-xgo.sh47
1 files changed, 40 insertions, 7 deletions
diff --git a/go.sh b/go.sh
index e23404a..676718e 100755
--- a/go.sh
+++ b/go.sh
@@ -1,5 +1,6 @@
1#!/bin/bash 1#!/bin/bash
2capacity_threshold=50 2default_capacity_threshold=50
3capacity_threshold_file=$HOME/.config/powerloss-handler/capacity_threshold
3 4
4battery_dir=/sys/class/power_supply/BAT0 5battery_dir=/sys/class/power_supply/BAT0
5status_file=$battery_dir/status 6status_file=$battery_dir/status
@@ -8,6 +9,8 @@ capacity_file=$battery_dir/capacity
8UNIT=powerloss-handler 9UNIT=powerloss-handler
9CMD=$0 10CMD=$0
10 11
12FORCE_SUSPEND=
13
11self_install() 14self_install()
12{ 15{
13 if systemctl --user is-active "$UNIT" >/dev/null 16 if systemctl --user is-active "$UNIT" >/dev/null
@@ -21,27 +24,51 @@ self_install()
21 -- "$CMD" 24 -- "$CMD"
22} 25}
23 26
24go() 27self_test()
28{
29 FORCE_SUSPEND=y
30 go
31 echo
32}
33
34do_suspend()
25{ 35{
36 systemctl suspend
37}
26 38
39go()
40{
27 read capacity < "$capacity_file" || return 41 read capacity < "$capacity_file" || return
28 case "$capacity" in 42 case "$capacity" in
29 '' | *[^0-9]*) 43 '' | *[^0-9]*)
30 echo "Error: unexpected value for battery capacity: $capacity" >&2 44 echo "Error: unexpected value for battery capacity: $capacity" >&2
31 return 1 ;; 45 return 1 ;;
32 esac 46 esac
33
34 read status < "$status_file" || return 47 read status < "$status_file" || return
35 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
36 if [ -t 2 ] 56 if [ -t 2 ]
37 then 57 then
38 printf '\r %20s %.2d%%\r' "$status" "$capacity" >&2 58 printf '\r %20s %.2d%%\r' "$status" "$capacity" >&2
39 fi 59 fi
40 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
41 [ "$status" = Discharging ] || return 0 68 [ "$status" = Discharging ] || return 0
42 if [ "$capacity" -lt "$capacity_threshold" ] 69 if [ "$capacity" -lt "$capacity_threshold" ]
43 then 70 then
44 systemctl suspend 71 do_suspend
45 fi 72 fi
46} 73}
47 74
@@ -61,9 +88,15 @@ main()
61 done 88 done
62} 89}
63 90
64if [ "$1" = install ] 91if [ $# = 0 ]
65then 92then
66 self_install
67else
68 main 93 main
94else
95 case "$1" in
96 install) self_install ;;
97 test) self_test ;;
98 *) echo "Error: unknown command: $*" >&2
99 exit 1
100 ;;
101 esac
69fi 102fi