config_sway/bin/suspend

42 lines
726 B
Bash
Executable file

#!/bin/bash
BAT_PATH=/sys/class/power_supply
BAT=BAT0
MODE=suspend-then-hibernate
# 0: charging
# 1: discharging/unknown
function _state() {
f=$BAT_PATH/$BAT/status
s=1
if [ -e $f ]; then
s=$(grep -c Discharging $f)
fi
echo $s
}
function _suspend() {
state=$(_state)
force=${1:-0}
echo "INFO: state: $state, force: $force"
if [ $state -eq 1 -o $force -eq 1 ]; then
systemctl $MODE
else
echo "ERROR: state: $state, force: $force"
fi
}
mkdir -p $HOME/log
echo "suspend: $(date) args: $*" >> $HOME/log/sway.log
case $1 in
suspend)
_suspend 0
;;
force)
_suspend 1
;;
*)
echo "ERROR: invalid command: \"$1\""
echo "possible options: [suspend|force]"
;;
esac