#!/bin/bash
# FVWM-Crystal helper script that put the omputer in hibernation
# when the battery is low
# Usage: Exec exec $[FVWM_SYSTEMDIR]/scripts/AutoHibernate <rate [%]>

# the PID to kill if fvwm is interrupted
TMPFILE="/tmp/crystal_autohibernate_$$"
touch ${TMPFILE}

cleanup() {
	rm ${TMPFILE}
	exit 0
}

trap cleanup INT QUIT TERM

if [[ ! "$1" ]]; then
	cleanup
fi

# write the preference file
echo "Exec exec ${FVWM_SYSTEMDIR}/scripts/AutoHibernate $1" > ${FVWM_USERDIR}/preferences/AutoHibernation

# the main loop
CurrentCharge=$(($(cat /sys/class/power_supply/BAT0/energy_now)/(($(cat /sys/class/power_supply/BAT0/energy_full)/100))))
MinCharge=$(($1+1))
Resumed="0"

while :
do
	sleep "5"
	# fvwm is started by exec; be sure it is running
	pidof fvwm 1>/dev/null || cleanup

	if [[ "$CurrentCharge" -lt "$MinCharge" ]]; then
		# avoid hibernation loop
		if [[ "${Resumed}" == "0" ]] ; then
			# mplayer doesn't like hibernation
			${FVWM_SYSTEMDIR}/scripts/killmplayer 9
			killall -9 mplayer 2>/dev/null
			sudo pm-hibernate
			# we must kill that script after rebbot to avoid loop
			Resumed="1"
		else
			CurrentCharge=$(($(cat /sys/class/power_supply/BAT0/energy_now)/(($(cat /sys/class/power_supply/BAT0/energy_full)/100))))
		fi
	else
		CurrentCharge=$(($(cat /sys/class/power_supply/BAT0/energy_now)/(($(cat /sys/class/power_supply/BAT0/energy_full)/100))))
		# return to normal loop after resuming when CurrentCharge > $1
		if [[ "$CurrentCharge" -gt "$MinCharge" ]]; then
			Resumed="0"
		fi
	fi
done
