#!/usr/bin/perl
#
# batrem v0.0.2 - Display remaining capacity for battery (ACPI)
# Copyright (C) 2005 Stian Skjæveland
#
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#

use strict;

my $statefile="/proc/acpi/battery/CMB1/state";
my $infofile="/proc/acpi/battery/CMB1/info";
my ($descap,
    $remcap,
    @infolines,
    @statelines
);

open(INFOFILE, "$infofile") || die("$0: can't open $infofile: $!\n");
@infolines=<INFOFILE>;
chomp($descap=substr($infolines[1], 25, 4));
open(STATEFILE, "$statefile") || die("$0: can't open $statefile: $!\n");
@statelines=<STATEFILE>;
chomp($remcap=substr($statelines[4], 25, 4));
$remcap=$remcap * 100 / $descap;
if ($remcap=~/\./) {
  $remcap=substr($remcap, 0, 2);
}
if (!($ARGV[0])) {
  print "$remcap\%\n";
} elsif ($ARGV[0]=~/-n/) {
  print "$0: remaining capacity for \"$ENV{'HOSTNAME'}\" is: $remcap\%\n";
} else {
  print "Usage: $0 [-n]\n";
}

close(INFOFILE);
close(STATEFILE);
