#!/usr/bin/perl

use strict;
use Data::Dumper;

open FILE, "< $ARGV[0]";
my $userid = $ARGV[1];
my $password = $ARGV[2];

my $url;
my $method;
my $idparam;
my $pwdparam;
my $end = 0;
my $back;
while (<FILE>) {
    my $line = $_;
    $line =~ s/\r?\n$//;
    if ($line =~ /^https?:\/\//) {
        $url = $line;
    } elsif ($line =~ /^(GET|POST) /) {
        $method = $1;
    } elsif ($line =~ /^Location: (.+)/i) {
        $back = $1;
    } elsif ($line =~ /^----------/) {
        if ($end) {
            last;
        }
    } elsif ($line =~ /([^&=]+)=$userid(&.+|)$/ && $method eq 'POST') {
        $idparam = $1;
        $line =~ s/$idparam=$userid&?//;
        if ($line =~ /([^&=]*)=$password(&.+|)$/) {
            $pwdparam = $1;
            $back = $url;
            $end = 1;
        } else {
            undef($idparam);
        }
    }
}

print "$url $back $idparam $pwdparam\n";
