#! /bin/sh

# Warn user through 'wall' (to reach every open console device) about battery
# status. Also print a more informative message on devices TTY and PTY. 
# To be invoked by 'powermon'.  Reads percentage from /proc/apm.
# Synopsis:  'powermesg rmain power TTY [PTS]'
power=$1	# status 'low' or 'critical'
rmain=$2	# remaining minutes (calculted)
TTY=$3		# e.g. tty8
PTS=$4 		# e.g. /dev/console on xterm -C
actual=`cat /proc/apm | cut -d " " -f 7`;

HEADER="
 ===================== Battery power is $actual ( $rmain min ) ====================
"

echo "$HEADER" | wall

# Arrange pieces....

case $power in
    low)
    MESG="
Depending on how power-consuming the running processes are, you've got only
about $rmain minutes of battery power left now. You've got to finish your work, or 
find a main power to plug in !
To suspend the running system to disk (hibernation), hit the keys \"Fn+q\". 
This may last up to 80 seconds, so shutting down may be faster.

There will be one more warning when the battery status is critical.



 ================  General notes to keep your battery healthy  ===============

This Li-Ion battery does not have a 'memory effect'; so it does not need to be
powered down completely. Don't recharge it while working on main power; 
it's better to eject the battery then ! This can be safely done while on main. 
A Lithium-Ion battery is best stored at about 40% power; don't forget to 
recharge it at least to this level.
" 

    ;;
    
    critical)
    MESG="
Now you've got to finish your work straight, or plug into main power !
You've got only about $rmain MINUTES until power off.
Shutdown the system, or send it to hibernation with 'Fn+q'.
Note:  
Once a laptop starts blinking red and beeps every few seconds, there are
possibly only seconds left ! In this case, don't try hibernation, since
this will last too long - you have to shut down IMMEDIATELEY and very fast 
instead ! Shortest way is to type in 'halt' here.
"
    ;;
    
    *) 
    MESG="
/usr/local/bin/powermesg: bad parameter. Perhaps a typo ?"
    ;;
    
esac



# ..... and go.

echo "$HEADER $MESG" > $TTY;

[ $PTS ] && echo "$HEADER $MESG 

It's safe to close this window.



" > $PTS;



