#!/usr/bin/perl
#
# My custom-hacked ACPI battery monitor, since I could find nothing of
# the sort already written.  I run it like this:
#
# xterm -geometry 60x1 -e ./batt.pl
#
# I make no promises this thing will work; i t seems that the proc
# interface to acpi has yet to be solidified.  It works for me on kernel
# 2.4.19 with the latest patch of acpi.
# 
# (C) 2002 John F. Waymouth IV

my $battery=BAT1;

sub battinfo {
open(INFO,"</proc/acpi/battery/$battery/info");
$info=join("\n",<INFO>);
close(INFO);

open(STATUS,"</proc/acpi/battery/$battery/state");
$status=join("\n",<STATUS>);
close(STATUS);

$info =~ /^last full capacity:\s*([0-9]*)\smWh/mi;
$max=$1;
$status =~ /^remaining capacity:\s*([0-9]*)\smWh/mi;
$level=$1;

print "Battery at ";
printf "%2.1f", ($level / $max * 100);
print "%";

$status =~ /^present rate:\s*([0-9]*)\smW/mi;
$rate=$1;

$status =~ /^charging state:\s*([^\n]*)\n/mi;

print " $1 at ";
printf "%2.1f", ($rate / $max * 100);
print "%/hour";

if ($1 eq "discharging") {
	$hours = $level/$rate;
	print " " . int($hours) . ":";
	printf "%0.2d",(($hours * 60)%60);
	print " remaining";
}

print "\n";
}

while() {
battinfo();
sleep 30;
}
