#!/usr/bin/perl

use HTML::Form;
use LWP::UserAgent;

# NB, for auth to work, realm must include :80, so the
# URL here does too
my ($url, $user, $pass) = (
		'http://rmsched.mh.bbc.co.uk:80/cgi-bin/scheduler/sched.pl',
		'user',
		'squidtentaclerape');

my $realm = ($url =~ m#https?://([^/]+)/#)[0];

$ENV{HTTP_PROXY}= 'http://www-cache:80/';

my $ua = LWP::UserAgent->new();

$ua->env_proxy; # use proxy ENV varaible.

$ua->credentials($realm, 'Blanking', $user, $pass);
my $res = $ua->get('http://rmsched.mh.bbc.co.uk/cgi-bin/scheduler/sched.pl');

die "No joy fetching initial page" unless $res->is_success;


my $form = HTML::Form->parse($res->content, $res->base);

# right, now click on generate
# Generate is the fourth 'submit' input
# Damn you, maddy.
my $input = $form->find_input(undef,undef , 6);

die "Generate button not found!" unless $input->value ne 'Generate';

my $req = $input->click($form);

# send that back
my $res = $ua->request($req);
print "All done" if $res->is_success;
