| 1 |
#!/bin/bash |
| 2 |
# FVWM-Crystal helper script that put the omputer in hibernation |
| 3 |
# when the battery is low |
| 4 |
# Usage: Exec exec $[FVWM_SYSTEMDIR]/scripts/AutoHibernate <rate [%]> |
| 5 |
|
| 6 |
# the PID to kill if fvwm is interrupted |
| 7 |
TMPFILE="/tmp/crystal_autohibernate_$$" |
| 8 |
touch ${TMPFILE} |
| 9 |
|
| 10 |
cleanup() { |
| 11 |
rm ${TMPFILE} |
| 12 |
exit 0 |
| 13 |
} |
| 14 |
|
| 15 |
trap cleanup INT QUIT TERM |
| 16 |
|
| 17 |
if [[ ! "$1" ]]; then |
| 18 |
cleanup |
| 19 |
fi |
| 20 |
|
| 21 |
# write the preference file |
| 22 |
echo "Exec exec ${FVWM_SYSTEMDIR}/scripts/AutoHibernate $1" > ${FVWM_USERDIR}/preferences/AutoHibernation |
| 23 |
|
| 24 |
# the main loop |
| 25 |
CurrentCharge=$(($(cat /sys/class/power_supply/BAT0/energy_now)/(($(cat /sys/class/power_supply/BAT0/energy_full)/100)))) |
| 26 |
MinCharge=$(($1+1)) |
| 27 |
Resumed="0" |
| 28 |
|
| 29 |
while : |
| 30 |
do |
| 31 |
sleep "5" |
| 32 |
# fvwm is started by exec; be sure it is running |
| 33 |
pidof fvwm 1>/dev/null || cleanup |
| 34 |
|
| 35 |
if [[ "$CurrentCharge" -lt "$MinCharge" ]]; then |
| 36 |
# avoid hibernation loop |
| 37 |
if [[ "${Resumed}" == "0" ]] ; then |
| 38 |
# mplayer doesn't like hibernation |
| 39 |
${FVWM_SYSTEMDIR}/scripts/killmplayer 9 |
| 40 |
killall -9 mplayer 2>/dev/null |
| 41 |
sudo pm-hibernate |
| 42 |
# we must kill that script after rebbot to avoid loop |
| 43 |
Resumed="1" |
| 44 |
else |
| 45 |
CurrentCharge=$(($(cat /sys/class/power_supply/BAT0/energy_now)/(($(cat /sys/class/power_supply/BAT0/energy_full)/100)))) |
| 46 |
fi |
| 47 |
else |
| 48 |
CurrentCharge=$(($(cat /sys/class/power_supply/BAT0/energy_now)/(($(cat /sys/class/power_supply/BAT0/energy_full)/100)))) |
| 49 |
# return to normal loop after resuming when CurrentCharge > $1 |
| 50 |
if [[ "$CurrentCharge" -gt "$MinCharge" ]]; then |
| 51 |
Resumed="0" |
| 52 |
fi |
| 53 |
fi |
| 54 |
done |