#!/usr/bin/perl -w


			#Declare / Set Global Variables


#prompt for variables

my @myvariables = ('url','realm','username','password','proxy','cookies');
my %vrvariables = ('url' => 'first' ,'realm' => 'sec','username' => 'third','password' => 'fourth','proxy' => 'fifth','cookies' => 'sixth');
my $varcount = @myvariables;
my $ac = 0;
my $store_file = 'files-dash/cds.html';

sub grab_input
{
	print "Store choice made (Y/N): "; chomp(my $choice = <STDIN>);
	print "URL to access          : "; chomp(my $url = <STDIN>); chomp(my $url_credentials = `echo $url | cut -d "/" -f3`);
	print "Realm (If required)    : "; chomp(my $realm = <STDIN>);
	print "Username (If required) : "; chomp(my $username = <STDIN>);
	system(`stty -echo`);
	print "Password               : "; chomp(my $password = <STDIN>);
	system(`stty echo`);
	print "\n";
	print "Proxy server           : "; chomp(my $proxy = <STDIN>);
	print "Cookies File to use    : "; chomp(my $cookies = <STDIN>);

        if($choice eq "Y" || "y")
        {
	my %credentials_store = ( url => $url, realm => $realm, username => $username, password => $password, proxy => $proxy, cookies => $cookies );
        open(STORE, ">store.txt");
        my $i = 0;
        foreach(%credentials_store) 
                {
                        while ($i < $varcount)
                                {
					print STORE $credentials_store{"$myvariables[$i]"}, "\n";
                                        $i++;
                                }
                }
        }
}

sub make_input_global
{
	`cp store.txt temp.txt`;
        open(MKGLB, "<temp.txt");
        my $i = 0;
        while ($stored_var = <MKGLB>)
                                {
					$arr = $myvariables[$i];
					chomp($vrvariables{$arr} = $stored_var);
                                        $i++;
                                }
	#`rm -f temp.txt`;
}

if( -e 'store.txt' )
{
print "Use Stored Choices (Y/N): "; chomp(my $choice1 = <STDIN>); print $choice1," \n";
	if($choice1 eq "Y" || "y")
		{
			make_input_global();
		}
	else	{
			grab_input;
			make_input_global;
		}
}
else
	{
		grab_input;
		make_input_global;
	}

		chomp(my $old_credentials = `echo $vrvariables{'url'} | cut -d "/" -f3`);
		chomp(my $url_credentials = `echo "$old_credentials\:80"`);




			#Declate the packages to be used

#-----First----# #http://search.cpan.org/~gaas/libwww-perl-5.834/lib/LWP/UserAgent.pm

use LWP::UserAgent; #Required for simulating a browser 


			#Construct a new Useragent

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


			#Define the Useragent Browser application

$ua->agent("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; InfoPath.2; MS-RTC LM 8)"); #Internet Explorer
#$ua->agent("Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.7)"); #Mozilla


			#Set the browser options

$ua->proxy(['http','https'],"$vrvariables{'proxy'}");
$ua->no_proxy('localhost', '172.25.203.131', 'server.my.net');
$ua->cookie_jar({file => "$vrvariables{'cookies'}", autosave => 1}); # A short cut to -- use HTTP::Cookies; HTTP::Cookies->new(file => "$cookies");

#$ua->show_progress( TRUE || FALSE );
#$ua->local_address( 'interface to bind to for connections'  );
#$ua->protocols_allowed( ['http','https'....] || undef );
#$ua->protocols_forbidden( ['http','https'....] || undef );


			#Authentication declaration

$ua->credentials("$url_credentials", "$vrvariables{'realm'}", "$vrvariables{'username'}", "$vrvariables{'password'}");

			# Create and make a request and store it to a file

my $response = $ua->get("$vrvariables{'url'}", ':content_file' => "$store_file");

			#Test and / or  print errors for the get request

# if ($response->is_success) 
# 	{
#     		print $response->decoded_content;  # or whatever
# 	}
# else	{
#     		die $response->status_line;
