#! /usr/bin/perl -w

# HP Serviceability Engineering, Server & OS Tools Team
# Email: roy.main@hp.com
# 1/11/06 v.60

use CGI qw(:standard); # For CGI functions.
use XML::Simple;       # XML parser.
use Net::SMTP;         # SMTP communcations.
use LWP::Simple;       # HTTP communications.


# Initialize the XML parser with a new document object
# and read/parse the service links XML document.

my $xs = new XML::Simple(forcearray=>1, keyattr=>{language=>'value',device=>'model'});
my $links = $xs->XMLin('osem.xml');


# Example for accessing xml values in the doc:
# 
# $release_date = $links->{release};
# $url = $links->{language}->{en}->{device}->{DL380-G3}->{url};

# Exmample for printing the XML data structure:
# 
# use Data::Dumper;
# print Dumper($links);


$quote         = chr(34);            # Variable for the " quote char.
$single_quote  = chr(39);            # Variable for the ' single quote char.
$no_params     = 1;                  # Flag to track input parameters.
$requested_url = $ENV{REQUEST_URI};  # Get the URL that was requested.


# Enables parameters to be passed to the script. 
# Each param listed in this table will cause the 
# corresponding subroutine to be executed.

%param_options = (
    'l' => 'Languages',
    'd' => 'DeviceURL',
);


# The languages that will be recognized by the 
# 'l' param. This param is optional.

%languages = (
    'en' => 'English',
    'sp' => 'Spanish',
    'ja' => 'Japanese',
    'fr' => 'French',
    'it' => 'Italian',
    'ge' => 'German',
);


ProcessParams();
exit 0;


sub DeviceURL
{
    # Points to the proper URL based on the computer
    # model passed from the incoming parameter string.
    
    if (defined(param('d')))
    {
       my $d = param('d');
       $no_params = 0;

       if (exists $links->{language}->{$l}->{device}->{$d})
       {
          # $url = $models{$l}{$d};

          $url = $links->{language}->{$l}->{device}->{$d}->{url};
	  
	  RedirectionImmediate($url);
       }
       else
       {
	  # Device model not found...
	  
	  $message = "Couldnt find ProLiant model.";
	  ErrorPage($message);
          SendMail('Service Links Unknown Model',$message);  
       }
    }

}


sub ErrorPage
{
    my $message = shift;
    
    print header();
    print $message, "<br>";
    print end_html();
}


sub Languages
{
    # Used to point the url to the clip in the proper language.
    # The videos are currently English-only - this was added
    # to accommodate future videos in other languages.

    # Language will always be english for now.
    
    $l = 'en';

    #if (defined(param('l')))
    #{ $l = param('l') }
    #else
    #{ $l = 'en'; }

}


sub ProcessParams
{
    # Cycles through all the documented input parameters and executes
    # the subroutine defined in  %param_options for each one.


    foreach (@{['l','d']})
    {
        if (exists $param_options{$_})
        {
            &{$param_options{$_}}();
        }
    }
    
    if ($no_params)
    {
       # No input parameters...
       
       $message = "You did not supply the correct parameter.";
       ErrorPage($message);
       SendMail('Service Links Param Error',$message);
    }

}


sub RedirectionTimeout {

    # Redirects user's browser after a specified timeout period.

    my $url = shift;

    # setTimout("window.location = http://www.google.com",1000)
    # or document.location

    print header();
   
    print '<head><script type=', $quote, 'text/JavaScript', $quote, '>';
    print 'function delayer() { window.location = ', $quote, $url, $quote, ' }';
    print '</script></head>';

    print '<body onLoad = ', $quote, 'setTimeout(', $single_quote, 'delayer()', $single_quote, ',4000)', $quote, '>';

    print "You will be redirected to the correct page in a few moments.<br>";

    print end_html();

}


sub RedirectionImmediate {

    # Redirects user's browser immediately.

    my $url = shift;

    $good_link = head($url);

    if ($good_link)
    {
    
        print header();

        print '<script type=', $quote, 'text/JavaScript', $quote, '>';
        print 'window.location = ', $quote, $url, $quote, '</script>';

        print end_html();

    }
    else
    {
        $message = "The page was not found: $url.";
        ErrorPage($message);
        SendMail('Service Links Page Not Found',$message);
    }

}


sub SendMail {

    # Sends email to the Serviceability Engineering team
    # with notifications of script errors, using the HP
    # internal SMTP server.

    my ($subj_field, $message) = @_;

    $smtp = Net::SMTP->new('smtp-americas.hp.com');

    $smtp->mail('ServiceLinksApp\@hp.com');
    $smtp->recipient('roy.main\@hp.com');

    $smtp->data();
    
    $smtp->datasend("To: roy.main\@hp.com\n");
    $smtp->datasend("Subject: $subj_field\n");

    $smtp->datasend("\n");
    $smtp->datasend("$message");
    $smtp->datasend("  The requested URL was: $requested_url.");

    $smtp->dataend();

    $smtp->quit;


    #{
    #    $smtp = Net::SMTP->new('smtp-americas.hp.com');
    #    $smtp->mail('service@adu-db.cca.cpqcorp.net');
    #    $smtp->to("$email");
    #    $smtp->data();
    #    #$smtp->datasend("To:$email");
    #    #$smtp->datasend("From:ADU Database");
    #    $smtp->datasend("Subject: ADU Project \"$project[0]\" is Ready");
    #    $smtp->datasend("\n");
    #    $smtp->datasend("$username,\n");
    #    $smtp->datasend("Your uploaded ADU Project \"$project[0]\" is now ready for use.\n");
    #    $smtp->datasend("\n");
    #    $smtp->datasend("Click on the following URL to view your project:\n");
    #    $smtp->datasend("\n");
    #    $smtp->datasend("http://iss-tce.cca.hp.com/eps/epma/adusearch/default.aspx?projectid=$project_id\n");
    #    $smtp->dataend();
    #    $smtp->quit();
    #}


}

