#!/usr/bin/perl

# login and retrieve page
$res = `wget --no-check-certificate https://www.telecom.co.nz/xtralogin.fcc --post-data="USER=XXXX&PASSWORD=YYYY&target=https://www.telecom.co.nz/jetstreamum/xtra" -O /dev/stdout 2>/dev/null`;

@lines = split(/\n/, $res);

for (@lines)
{
        # strip html tags
        $_ =~ s/<(?:[^>'"]*|(['"]).*?\1)*>//gs;
        if($_ =~ /MB/)
        {
		# search for MB and take used MB first
		# followed by total MB
                $_ =~ s/\s*(\d*[\.]*\d*) MB.*/$1/;
                if($used == "")
                {
                        $used = $_;
                }
                else
                {
                        $total = $_;
                }
        }
}

$percent = $used/$total;

# display the first line as the percentage used
printf "Xtra BW: %.02f%%", $percent*100;

# display the second line as the megabytes used...
# ...account for the variable size of the percentage
# by adding or subtracting a space depending on
# the number of digits
if($percent*100 < 10)
{
        $y = 1;
}
elsif($percent*100 > 100)
{
        $y = -1;
}

# print spaces as a hack to get to the right spot on
# the next line
for($i=0;$i<25+$y;$i++)
{
        print " ";
}
printf ("%d / %d MB\n",$used,$total);
