From d03defeecd0377373ad5c6d8efe0ff8b6aa844ee Mon Sep 17 00:00:00 2001 From: u Date: Sun, 9 Apr 2023 10:31:57 -0400 Subject: initial --- go.sh | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100755 go.sh diff --git a/go.sh b/go.sh new file mode 100755 index 0000000..686127f --- /dev/null +++ b/go.sh @@ -0,0 +1,58 @@ +#!/bin/sh +capacity_threshold=90 + +battery_dir=/sys/class/power_supply/BAT0 +status_file=$battery_dir/status +capacity_file=$battery_dir/capacity + +UNIT=powerloss-handler +CMD=$0 + +self_install() +{ + systemctl --user reset-failed "$UNIT" 2>/dev/null + systemd-run --user \ + --unit "$UNIT" \ + --property Restart="on-failure" \ + -- "$CMD" +} + +go() +{ + + read capacity < "$capacity_file" || return + case "$capacity" in + '' | *[^0-9]*) + echo "Error: unexpected value for battery capacity: $capacity" >&2 + return 1 ;; + esac + + if [ -t 2 ] + then + printf ' %.2d%% \r' "$capacity" >&2 + fi + + read status < "$status_file" || return + [ "$status" = Discharging ] || return 0 + + if [ "$capacity" -lt "$capacity_threshold" ] + then + systemctl suspend + fi +} + +main() +{ + while true + do + go + sleep 1 + done +} + +if [ "$1" = install ] +then + self_install +else + main +fi -- cgit v1.2.3