From 93994d3407788f476180aecbe888346b52935159 Mon Sep 17 00:00:00 2001 From: Harald Pfeiffer Date: Fri, 25 Jan 2019 11:45:21 +0100 Subject: 2019-01-24 --- localfs/usr/local/bin/batman | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'localfs/usr') diff --git a/localfs/usr/local/bin/batman b/localfs/usr/local/bin/batman index bfa00c0..5b873d7 100755 --- a/localfs/usr/local/bin/batman +++ b/localfs/usr/local/bin/batman @@ -1,9 +1,11 @@ #!/bin/sh declare -x VOERBOOS -export RETVAL=0 -export BATVAL="$(echo "scale=0;$(cat /sys/class/power_supply/BAT0/charge_now)*100/$(cat /sys/class/power_supply/BAT0/charge_full)"|bc -l)" -export BATSTATE="$(cat /sys/class/power_supply/BAT0/status)" +RETVAL=0;export RETVAL +BATVAL="$(echo "scale=0;$(cat /sys/class/power_supply/BAT0/charge_now)*100/$(cat /sys/class/power_supply/BAT0/charge_full)"|bc -l)";export BATVAL +BATSTATE="`cat /sys/class/power_supply/BAT0/status`";export BATSTATE +BATFILE="/tmp/batman.sd";export BATFILE +export OPTCNT=0 function batmobile { if [ "$BATVAL" -le 5 ];then case "$BATSTATE" in @@ -11,12 +13,23 @@ function batmobile { MSG="Battery charged at $BATVAL% with status \"$BATSTATE\", no shutdown." [ ! -z "$VOERBOOS" ]&&[ "$VOERBOOS" -eq 1 ]&&echo "$MSG"||true logger -p"info" -t"batman" "Battery charged at $BATVAL% with status \"$BATSTATE\", no shutdown."||RETVAL=$(("$RETVAL"+$?)) + [ -e "$BATFILE" ]&&/usr/sbin/shutdown -c >/dev/null 2>&1&&rm "$BATFILE" + SDCRV="$?" + case "$SDCRV" in + 0) logger -p"info" -t"batman" "Shutdown cancelled.";; + *) + logger -p"warn" -t"batman" "$BATFILE exists but couldn't cancel shutdown!" + RETVAL=$(("$RETVAL"+"$SDCRV")) + ;; + esac ;; *) MSG="Battery charged at $BATVAL%, powering down." [ ! -z "$VOERBOOS" ]&&[ "$VOERBOOS" -eq 1 ]&&echo "$MSG"||true logger -pwarn -t"batman" "$MSG"||RETVAL=$(("$RETVAL"+$?)) - sudo shutdown -P +2 "$MSG"||RETVAL=$(("$RETVAL"+$?)) + if [ ! -e "$BATFILE" ];then + /usr/sbin/shutdown -P +2 "Battery low and discharging, powering down. (batman)"&&touch "$BATFILE"||RETVAL=$(("$RETVAL"+$?)) + fi ;; esac else @@ -30,12 +43,13 @@ function batarang { while getopts :vnp SHOPT;do case "$SHOPT" in - v)export VOERBOOS=1;; - p)batmobile;; - ""|n)batarang;; - *)echo "Not supported option: -""${OPTARG}" >&2;RETVAL=$(($RETVAL+1)) >&2;WRONGOPT=1;; + v)VOERBOOS=1;export VOERBOOS;((OPTCNT++));; + p)batmobile;((OPTCNT++));; + ""|n)batarang;((OPTCNT++));; + *)echo "Not supported option: -""${OPTARG}" >&2;RETVAL=$(($RETVAL+1)) >&2;WRONGOPT=1;((OPTCNT++));; esac done shift $(( $OPTIND - 1 )) +[ "$OPTCNT" -le 0 ]&&batarang [ ! -z "$WRONGOPT" ]&&[ "$WRONGOPT" -eq 1 ]&&exit 1||true exit "$RETVAL" -- cgit v1.2.3