#!/usr/bin/perl

$addr = $ARGV[0];
$addr =~ s/;//g;

use HTTP::Cookies;
use HTTP::Request;
use HTTP::Request::Common;
use HTML::LinkExtractor;
use HTML::Form;
use URI::URL;
use LWP::UserAgent;

{
    package RequestAgent;
    @ISA = qw(LWP::UserAgent);

    sub new
    {
        my $self = LWP::UserAgent::new(@_);
        $self->agent("lwp-request/$main::VERSION");
        $self;
    }

    sub get_basic_credentials_remove_this_to_work
    {
	my $username = `cat ~/.user`;
	my $password = `cat ~/.pass`;
	chomp $username;
	chomp $password;
	return ($username, $password);
    }
}

my $ua = RequestAgent->new;

sub get_form 
{
	( $form_name, $content, $base_page ) = @_;
	#print STDERR "form_name = $form_name\n";
	#print STDERR "base_page = $base_page\n";
	my @forms = HTML::Form->parse($content, $base_page);
	my $form;
	foreach $form (@forms) {
		#print STDERR "form action: ".$form->action."\n";
		if ( $form->action =~ /$form_name$/ ) {
			return $form;
		}
	}
}

my $cookie_pak = HTTP::Cookies->new;
$ua->cookie_jar($cookie_pak);

my $signin_page = 'http://www.sprintpcs.com/';
my $res = $ua->request(GET $signin_page);

$group_name_form = get_form ("/Manage", $res->content, $signin_page);
$group_name_form->value("min", $ARGV[0]);
$group_name_form->value("password", ARGV[1]);
$request = $group_name_form->click();
$res = $ua->request($request);

$res = $ua->request(GET "https://manage1.sprintpcs.com/Manage?target=MyCurrentUsage&action=current_usage");
print $res->content;
