#!/usr/bin/perl

use strict;

unless (-r '/sys/class/power_supply/AC/online') {
    die "not found '/sys/class/power_supply/AC/online'\n";
}

my $ac = 'off';

if (int `cat /sys/class/power_supply/AC/online`) {
    $ac = 'on';
}



my @bats;
{
    for $b (glob '/sys/class/power_supply/BAT?') {
        next unless -d $b;

        next unless -r "$b/energy_now";
        next unless -r "$b/energy_full";

        my $full = int `cat $b/energy_full`;
        my $now = int `cat $b/energy_now`;
        
        next unless $full and $now;

        push @bats => $now * 100.0 / $full;
    }
}

if (@bats) {
    my $total = 0;
    $total += $_ for @bats;
    printf "battery=%d\n", $total / @bats;
}

print "ac_line=$ac\n";
