#!/usr/bin/perl -w

# (c) 2010 by Arno Wagner <arno@wagner.name>
# Distributed under the Gnu Public license v2, 
# see http://www.gnu.org/licenses/gpl-2.0.html for a copy of the license

# Version 1.0

require 5.002;
use Tk;
#use strict;
sub tick;
sub check_batt;

my $MW = MainWindow->new;
$MW->bind('<Control-c>' => \&exit);

my(%tinfo) = ('w' => $MW, 't' => 'color');


my $CW = $MW->Frame();

my $l1 = $MW->Label(
    -relief       => 'raised', 
    -width        => 20,
    -text => '???',
    -bg => 'grey'
);


$l1->pack(-side => 'left', -fill => 'both', -expand => 'yes');

$last_loop = time - 1000;  # for checks slower than the tick-loop

tick;

sub tick {
    check_batt;               # main loop body
    $MW->after(10000, \&tick); # main loop construct: Self-call after n ms
} 


sub check_batt {
    my $time = time;
    my $dtime = $time - $last_loop;

    if ($dtime >= 10) { # loop delay in seconds
        $last_loop = $time;
#        print "loop body reached\n";
        # 
        # Determine variable values
        #

        # read battery info and state files
        open IN,"/proc/acpi/battery/BAT1/info"; @info = <IN>; close IN;
        open IN,"/proc/acpi/battery/BAT1/state"; @state = <IN>; close IN;
        
        # determine design capacity
        if ((grep /^design capacity:/, @info) != 1) { die "desing cap."; }
        $l = (grep /^design capacity:/, @info)[0];
        $l =~ /(\d+)/;
        $dcap = $1;

        # determine remaining capacity
        if ((grep /^remaining capacity:/, @state) != 1) { die "rem. cap."; }
        $l = (grep /^remaining capacity:/, @state)[0];
        $l =~ /(\d+)/;
        $rcap = $1;

        # determine current rate
        if ((grep /^present rate:/, @state) != 1) { die "present rate"; }
        $l = (grep /^present rate:/, @state)[0];
        $l =~ /(\d+)/;
        $rate = $1;
        
        # determine whether we are discharging
        $disch = 0;
        if (grep /^charging state:\s+discharging/, @state) { $disch = 1; } 

        # accu level in percent
        $level = int(100*$rcap/$dcap); 

        # time to full / empty
        if ($rate == 0) { $rtime = 0; } 
        else { $rtime = int(($rcap/$rate)*3600); }
        $rsec = substr (("00".($rtime % 60)), -2 ,2);
        $rmin = substr (("   ".int($rtime / 60)),-3,3);

        
        #
        # Now display 
        #
        
        if ($disch) {
            # case 1: Running on battery
 
            if ($level > 30)  { $l1->configure(-bg => 'green'); } 
            if ($level <= 30) { $l1->configure(-bg => 'yellow'); } 
            if ($level < 10)  { $l1->configure(-bg => 'red'); } 
        
            $text = "BAT";
            $text .= "  ";
            $text .= substr (("    ".$level."%"), -4, 4);
            $text .= "  ";
            $text .= " $rmin:$rsec";

            $l1->configure(-text => $text);

        } else {
            # case 2: Running on external power
            $l1->configure(-bg => 'grey');
            
            $text = "AC ";
            $text .= "  ";
            $text .= substr (("    ".$level."%"), -4, 4);
            $text .= "  ";
            $text .= " $rmin:$rsec";
            
            $l1->configure(-text => $text);
            
        }
    }
    

}

MainLoop;





