#/usr/bin/perl -w

use strict;

use LWP::UserAgent;
use HTTP::Request::Common;

use CGI::Carp qw(fatalsToBrowser); 
use CGI; 

eval('require "./gsdlCGI.pm"');
if ($@)
{
    print STDOUT "Content-type:text/plain\n\n";
    print STDOUT "ERROR: $@\n";
    exit 0;
}


sub main
{
    # Setup greenstone Perl include paths so additional packages can be found
    my $gsdl_cgi = gsdlCGI->new(); 
    $gsdl_cgi->setup_gsdl();

    require talkback;

    my $rand_string;
    if (defined $gsdl_cgi->param('rand_string')) { 
	$rand_string = $gsdl_cgi->param('rand_string');
    }
    else {
	$rand_string = "b310f690273cdc025e5a9b49fca8eee8";
    }

    my $uploadedfile;
    if (defined $gsdl_cgi->param('uploadedfile')) { 
	$uploadedfile = $gsdl_cgi->param('uploadedfile');
    }
    else {
	$uploadedfile = "C:\\cygwin\\tmp\\test-medium.pdf";
    }

    my $talktoUploadURL;
    if (defined $gsdl_cgi->param('talkbackUpload')) { 
	$talktoUploadURL = $gsdl_cgi->param('talktoUpload');
    }
    else {
	$talktoUploadURL = "http://www.nzdl.org/gsdl-new/cgi-bin/talkback-progressbar.pl";
    }

    my $browser = LWP::UserAgent->new(agent => 'Perl File Upload');

    my $response = $browser->post(
	   $talktoUploadURL,
	   [ 'yes_upload'   => '1',
	     'process'      => '1',
	     'rand_string'  => $rand_string,
	     'uploadedfile' => [$uploadedfile, "test-medium.pdf"] 
	     ],
	   'Content_Type' => 'form-data'
	  );

    if ($response->is_success) {
	print "Content-type:text/html\n\n";
	print $response->content;
    }
    else {
	print $response->error_as_HTML;
    }

}

main();

