#!/bin/ksh
#
# Battery frontend for APM -> to be used with laptops
#
# Version 1.0
# - Initial release
# Version 1.1
# - Improved color routines
#
# Feb 20, 2005, (c) Peter van Eerten
#       Tested with Slackware 10.1 and GTK 2.6
#
#------------------------------------------------------
#
# Required tools:
# -apm
# -GNU Awk
# -APM module loaded by kernel
#
#------------------------------------------------ CHECK

function Check_Software
{
# Check on availability of AWK
AWK=`which gawk 2>/dev/null`
if [[ -z $AWK ]]
then
        echo "GNU Awk not found on your system! Exiting..."
        exit 1
fi

# Check on availability of apm
APM=`which apm 2>/dev/null`
if [[ -z $APM ]]
then
        echo "apm not found on your system! Exiting..."
        exit 1
fi

# Check if APM module is loaded
MODULE=`dmesg 2>&1 | grep -i apm`
if [[ -z $MODULE ]]
then
        echo "Your kernel is currently not using APM!"
        exit 1
fi
}

#------------------------------------------------ GTK

# Set color of progressbar
function Create_Colors
{
# Determine color on basis of argument $1
if [[ $1 -lt 0.1 ]]
then
        COLOR="65535, 0, 0"
else
        COLOR="0, 52480, 0"
fi

# Parse resource for the progressbar - foregroundcolor
STR="\"style \\\"mystyle\\\" { bg[NORMAL] = {30464, 34816, 39168} }\""
gtk "gtk_rc_parse_string  $STR"
STR="\"style \\\"mystyle\\\" { fg[PRELIGHT] = {0, 0, 0} }\""
gtk "gtk_rc_parse_string  $STR"
STR="\"style \\\"mystyle\\\" { bg[PRELIGHT] = {$COLOR} }\""
gtk "gtk_rc_parse_string $STR"
STR="\"widget \\\"*.progress\\\" style \\\"mystyle\\\"\""
gtk "gtk_rc_parse_string $STR"
gtk "gtk_rc_reset_styles $GTKSETTINGS"
}

#------------------------------------------------ GTK

# Communication function; $1 contains the string to be sent
function gtk
{
print -p $1
read -p RESULT
}

#------------------------------------------------ GUI

function Build_Gui
{
# Setup environment for GTK server
export LC_ALL=C
export LD_LIBRARY_PATH=/usr/X11R6/lib

# Start GTK-server in STDIN mode with low CPU usage
nice -n 19 gtk-server stdin |&

# Check GTK version
gtk "gtk_check_version 2 4 0"
if [[ -n $RESULT ]]
then
        echo "Your GTK installation is too old!"
        echo "GTK version 2.4.0 or higher is required. Exiting..."
        gtk "gtk_exit 0"
        exit 1
fi

# Define GUI - mainwindow
gtk "gtk_init NULL NULL"
gtk "gtk_window_new 0"
WINDOW=$RESULT
gtk "gtk_window_set_title $WINDOW \"Battery Watcher\""
gtk "gtk_window_set_position $WINDOW 1"
gtk "gtk_widget_set_usize $WINDOW 250 32"
# Set progressbar
gtk "gtk_progress_bar_new"
PROGRESS=$RESULT
# Save info to set colors
gtk "gtk_widget_set_name $PROGRESS progress"
gtk "gtk_settings_get_default"
GTKSETTINGS=$RESULT
# Create layout
gtk "gtk_container_add $WINDOW $PROGRESS"
gtk "gtk_widget_show_all $WINDOW"
}

#------------------------------------------------ MAIN

# Check available packages
Check_Software

# Create GUI
Build_Gui

# Initialize variables
EVENT=0

# Mainloop
while [[ $EVENT -ne $WINDOW ]]
do
        # Get event
        gtk "gtk_server_callback update"
        EVENT=$RESULT

        # Get status
        STATUS=`$APM | $AWK '{for (a=1; a<NF+1; a++) {if ($a ~ "%") print $a}}'`
        VALUE=`$AWK -v VAR=$STATUS 'BEGIN {print (VAR+0)/100}'`

        # Update progressbar
        gtk "gtk_progress_bar_set_fraction $PROGRESS $VALUE"
        gtk "gtk_progress_bar_set_text $PROGRESS \"Power left: $STATUS\""

        # Determine colors
        Create_Colors $VALUE

        # Sleep with low CPU load - use 'sleep' of GNU coreutils
        nice -n 19 /usr/bin/sleep 0.1

done

# Exit GTK
gtk "gtk_exit 0"