#! /bin/bash
# Copyright 2003, Paul Sladen <r31@paul.sladen.org>, 
# Dean Jones <dean at cleancode dot org>
# GPL'ed, v0.99rc1 :)
#
#   Monitors the battery status via APM and spits the
#   results out via osd_cat.
#
#   If battery is good display is in gray.
#   If the battery is over 20%, display is in yello, else
#   If the battery is over 10%, display is in orange red, else
#   If the battery is under 10%, display is in Red.
#
#   If you have the osd_cat binary in a strange area,
#   You can specify the full path in the variable
#   OSD_CAT (below), or put the path to it in $PATH
#
#   If you do not have the JMK Neep font, you can change
#   the FONT variable (below) to the font you'd like to use.
#
# 2.5 hours = 9000 seconds.
# ~1% battery per  90 second update
# ~2% battery per 180 second update

OSD_CAT=
#FONT="neep-14"

if [ -z "$OSD_CAT" ]
then
  OSD_CAT=`which osd_cat 2>/dev/null`
  test -z "$OSD_CAT" \
   && echo "debug: $OSD_CAT" \
   && echo "Error: Can't find osd_cat" \
   && echo "Either you don't have osd_cat installed or " \
   && echo "it is not in your \$PATH.  Please either install" \
   && echo "osd_cat (Part of the xosd package), or add the" \
   && echo "path to the binary in your \$PATH" \
   && exit
#
##debug: this here is a comment made by Paul Sladen, doesn't work 
##for me though and I have no time to fix right now...
#  [ -z "$OSD_CAT" ] && cat << EOF && exit
#  debug: $OSD_CAT
#  Error: Can't find osd_cat
#  Either you don't have osd_cat installed or
#  it is not in your \$PATH.  Please either install
#  osd_cat (Part of the xosd package), or add the
#  path to the binary in your \$PATH
#  EOF
fi
DELAY=180

while true ; do
  LEVEL=`awk '{print $7}' /proc/apm`
  case "$LEVEL" in
    -1%) COLOUR='gray' LEVEL='no batt' ;;
     ?%) COLOUR='red' ;;
    1?%) COLOUR='orange red' ;;
    2?%) COLOUR='yellow' ;;
    *?%) COLOUR='gray' ;;
  esac
  echo "debug: $OSD_CAT $LEVEL" 
  echo "$LEVEL" | $OSD_CAT -d $DELAY -c $COLOUR -o "-3" &
  wait # background and wait to allow Ctrl-C to work
done

