git.lirion.de

Of git, get, and gud

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md18
-rwxr-xr-xbin/batman55
-rw-r--r--cron.d/batman1
3 files changed, 74 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..a7585fb
--- /dev/null
+++ b/README.md
@@ -0,0 +1,18 @@
+## I'm Batman!
+
+It's a simple shell script that can do two things:
+1. Report the current battery charge and whether the battery is charging or not.
+2. Shutdown the machine if the charge is below 5%, the battery is discharging,
+ and the user is privileged to do so.
+
+Currently I'm including this in a cron file. This repository should soon host a
+systemd timer file as well.
+
+To-Dos:
+[ ] systemd timer file
+[ ] use [/var]/run instead of /tmp for the "are we rebooting"-file
+
+## Why?
+
+Because I can :) There are other mechanisms out there, but I started this
+script in unintended ignorance of these and now I finished it ;)
diff --git a/bin/batman b/bin/batman
new file mode 100755
index 0000000..5b873d7
--- /dev/null
+++ b/bin/batman
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+declare -x VOERBOOS
+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
+ "Charging")
+ 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"+$?))
+ if [ ! -e "$BATFILE" ];then
+ /usr/sbin/shutdown -P +2 "Battery low and discharging, powering down. (batman)"&&touch "$BATFILE"||RETVAL=$(("$RETVAL"+$?))
+ fi
+ ;;
+ esac
+ else
+ MSG="Battery charged at $BATVAL%, no shutdown."
+ [ ! -z "$VOERBOOS" ]&&[ "$VOERBOOS" -eq 1 ]&&echo "$MSG"||logger -p"info" -t"batman" "$MSG"||RETVAL=$(("$RETVAL"+$?))
+ fi
+}
+function batarang {
+ MSG="$BATVAL% ($BATSTATE)";echo "$MSG";
+}
+
+while getopts :vnp SHOPT;do
+ case "$SHOPT" in
+ 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"
diff --git a/cron.d/batman b/cron.d/batman
new file mode 100644
index 0000000..d0af5a7
--- /dev/null
+++ b/cron.d/batman
@@ -0,0 +1 @@
+* * * * * root /usr/local/bin/batman -p