#!/usr/bin/perl


####################################################################
# Load Modules
#use strict;
use HTML::LinkExtor;
use LWP::UserAgent;
use URI::Heuristic;
use Term::ANSIColor;

####################################################################
# Clear The Screen
#
system (clear);

####################################################################
# Header
#
print   color("red"),   "###############################################################",color("reset"),"\n",
        color("white"), "Presenting:                                                    ",color("reset"),"\n",
        color("white"), "       Final Project                                           ",color("reset"),"\n",
        color("red"),   "###############################################################",color("reset"),"\n"   ;
print   color("magenta"),"\tThis program takes a url as input",color("reset"),"\n",
        color("yellow"),"\tIt will print list of links on the web site.",color("reset"),"\n";

####################################################################
# Check to see if we were run interactively 
#
if ( @ARGV ){
  my $raw_url = shift ;
  our $url = URI::Heuristic::uf_urlstr($raw_url);
} else { 
  #
  # Since @ARGV was empty, we need to ask them for a url
  #
  print color("green"),"Please enter a url:\t",color("reset");
  chomp ( $raw_url=<STDIN> );
  our $url = URI::Heuristic::uf_urlstr($raw_url);
}    

####################################################################
# to flush next line
#    Not sure why exmples use this, but I included it anyway.
$| = 1;   

####################################################################
# Create a Object reference to the UserAgent
#
my $ua = LWP::UserAgent->new();

####################################################################
# Create the Request object with the url from above.
#
my $req = HTTP::Request->new( GET => $url );
$req->header('Accept' => 'text/html');

####################################################################
# Create the Resopnce object to hold the responce to the requst object
#
my $res = $ua->request($req);

###################################################################
# Error checking, Did we get the page?
#
if ( $res->is_success )
{
    #
    #  
    #
    my $page = $res->content();

    #
    # For each line in the responce with a link, Call the print_the_link().
    # This uses a function reference to call the print function.
    #
    my $parser = HTML::LinkExtor->new(\&print_the_link, $url);
    $parser->parse($page);
}
else
{
    die "Error: " , $res->status_line , "\n";
}

##################################################################
# Print function
#
sub print_the_link {
    my ($tag, %links) = @_;
    #
    # New slick little shortcut
    #
    print color("yellow"),"$tag @{[%links]}",color("reset"),"\n";
    
    #
    # Old method of printing
    #
    foreach $type ( keys (%links)){
       print "$tag $type $links{$type}\n";
    } 
    
}
