#!/usr/bin/perl
#
# Copyright (C) 2005-2006 Joshua D. Abraham (jabra@spl0it.org)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# battlife
# 1.0
#
# determines the percentage of free battery space at a given time
#
# the latest version can be found at:
# http://www.spl0it.org/files/battlife.pl
#
use strict;
my ($state, $remain, $max, $free_percent);
if ( -r "/proc/acpi/battery/BAT0/state" ){
    $remain =  `cat /proc/acpi/battery/BAT0/state | egrep remaining `;
    $remain = $& if ($remain =~ m/(\d+)/);
}
else {
    print "/proc/acpi/battery/BAT0/state doesn't exist or isn't readable\n";
    exit;
}

if ( -r "/proc/acpi/battery/BAT0/info" ){
    $max =  `cat /proc/acpi/battery/BAT0/info | egrep full `;
    $max = $& if ($max =~ m/(\d+)/);
}
else {
    print "/proc/acpi/battery/BAT0/info doesn't exist or isn't readable\n";
    exit;
}

$free_percent= ($remain/$max) * 100;
if ($free_percent >= 100) {
    print "The percentage of free battery is 100 % \n";
}
else {
    printf ("The percentage of free battery is %.2f \%\n",$free_percent);
}

