diff options
author | Andrew Cady <d@cryptonomic.net> | 2023-04-18 10:46:12 -0400 |
---|---|---|
committer | Andrew Cady <d@cryptonomic.net> | 2023-04-18 10:46:12 -0400 |
commit | 3db59345749db3edc45097962a3ade98211aa937 (patch) | |
tree | 09f00f71712306838c21aa8e4ae18c2139da652f | |
parent | 1e9cb44dc75c6edc83703bb93ca6946368c0fbec (diff) |
add "test" subcommand
-rwxr-xr-x | go.sh | 47 |
1 files changed, 40 insertions, 7 deletions
@@ -1,5 +1,6 @@ | |||
1 | #!/bin/bash | 1 | #!/bin/bash |
2 | capacity_threshold=50 | 2 | default_capacity_threshold=50 |
3 | capacity_threshold_file=$HOME/.config/powerloss-handler/capacity_threshold | ||
3 | 4 | ||
4 | battery_dir=/sys/class/power_supply/BAT0 | 5 | battery_dir=/sys/class/power_supply/BAT0 |
5 | status_file=$battery_dir/status | 6 | status_file=$battery_dir/status |
@@ -8,6 +9,8 @@ capacity_file=$battery_dir/capacity | |||
8 | UNIT=powerloss-handler | 9 | UNIT=powerloss-handler |
9 | CMD=$0 | 10 | CMD=$0 |
10 | 11 | ||
12 | FORCE_SUSPEND= | ||
13 | |||
11 | self_install() | 14 | self_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 | ||
24 | go() | 27 | self_test() |
28 | { | ||
29 | FORCE_SUSPEND=y | ||
30 | go | ||
31 | echo | ||
32 | } | ||
33 | |||
34 | do_suspend() | ||
25 | { | 35 | { |
36 | systemctl suspend | ||
37 | } | ||
26 | 38 | ||
39 | go() | ||
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 | ||
64 | if [ "$1" = install ] | 91 | if [ $# = 0 ] |
65 | then | 92 | then |
66 | self_install | ||
67 | else | ||
68 | main | 93 | main |
94 | else | ||
95 | case "$1" in | ||
96 | install) self_install ;; | ||
97 | test) self_test ;; | ||
98 | *) echo "Error: unknown command: $*" >&2 | ||
99 | exit 1 | ||
100 | ;; | ||
101 | esac | ||
69 | fi | 102 | fi |