#!/bin/sh
#
# Akku-Stand-Monitoring script für /proc/acpi
# Version 0.06 (C) 2004-04-16 Georg Lukas <georg@op-co.de>
# Redistribution of this file is permitted under the GNU GPL
#

WARN_PERCENT=3

function warn_mp3() {
	mplayer -q /usr/share/akku-monitor/warn_$1.mp3 > /dev/null 2>&1
}

function festival() {
	echo "$@" | aoss /usr/local/festival/bin/festival \
		--tts --language german>/dev/null
}

function warn_festival() {
	festival "Warnung! Akku bei $1%"
}

function status_festival() {
	[ -n "$2" ] && festival "Akku bei $1%; Wird geladen." && return
	festival "Akku bei $1%; Batteriebetrieb."
}

function read_capacity() {
	LIST_FULL=`grep -h 'last full capacity' /proc/acpi/battery/BAT*/info \
		| sed 's/^.* \([0-9]*\) mAh/\1/'`

	LIST_CURR=`grep -h 'remaining capacity' /proc/acpi/battery/BAT*/state \
		| sed 's/^.* \([0-9]*\) mAh/\1/'`

	FULL=`echo $LIST_FULL | sed 's/ /+/g' | bc`
	CURR=`echo $LIST_CURR | sed 's/ /+/g' | bc`
	echo "$CURR * 100 / $FULL" | bc
}


PERC=$WARN_PERCENT
OLD_CHARGING=''
while true ; do
	OLD_PERC=$PERC
	PERC=`read_capacity`

	OLD_CHARGING="$CHARGING"
	CHARGING="`grep -h ' charging$' /proc/acpi/battery/BAT*/state`"
	[ "$CHARGING" != "$OLD_CHARGING" ] && status_festival $PERC "$CHARGING"

	[ "$PERC" -lt "$OLD_PERC" ] && [ "$PERC" -le $WARN_PERCENT ] \
		&& [ -z "$CHARGING" ] && warn_festival $PERC
	sleep 3
done
