#!/usr/comp/bin/perl
# meta.cgi - this perl script demonstrates how to create a cgi program that
#            can access multiple search engines on the web.

use CGI qw(:all);
use CGI::Carp qw (fatalsToBrowser confess);
use strict;

# Define JavaScript function for hit number validation
my $jscript=<<END;
                <!-- Hide script

                function validateHits(form) 
                {
                 var hitnum = parseInt(form.hits.value);
                 if (isNaN(hitnum)) 
                    {
                        alert("Please choose an integer number of hits!");
                        form.hits.focus();
                        form.hits.select();
                        return false;
                    }
                      return true;
                }
                // End script hiding -->
END

# Create the query CGI object
my $query = new CGI;

# Create an html document
print $query->header;
print $query->start_html
                    (
                     -title      => 'THE META SEARCH ENGINE',
                     -author     => 'switzer.alan@decatech.com',
                     -script     => $jscript,
                     -BACKGROUND => 'images/background.jpg'
                    );

# Determine if script called with argument or not...
if (param())
{
    my $subject = $query->param('subject');
    my $hits = $query->param('hits');
    my $engines = $query->param('engines');

    print $query->h1('META SEARCH RESULTS:');

    if ($subject)
    {
         # Display requested query string and number of hits for the search
         print
         "Your subject is: <B>", em($query->param('subject')), "</B>", hr(),
         "You wanted to find <B>", em($query->param('hits')), "</B> hits", hr();

         # Save array of information for each search engine's characteristics
         my @search_engines = 
             (
                 {
                    name => "Yahoo",
                    url  => "http://search.yahoo.com",
                    tag  => "/bin/search?p=$subject",
                    img  => "images/yahoo.gif",
                    txt  => "Yahoo hits for \"<B>$subject</B>\"" 
                    },
                  {
                     name => "Lycos",
                     url  => "http://www.lycos.com",
                     tag  =>
                      "/cgi-bin/pursuit?cat=lycos&query=$subject&matchmode=and",
                     img  => "images/lycos.gif",
                     txt  => "Lycos hits for \"<B>$subject</B>\"" 
                  },
                  {
                     name => "Alta-Vista",
                     url  => "http://www.altavista.digital.com",
                     tag  => "/cgi-bin/query?what=web&fmt=.&pg=q&q=$subject",
                     img  => "images/alta_vista.gif",
                     txt  => "Alta-Vista hits for \"<B>$subject</B>\"" 
                  },
                  {
                     name => "Infoseek",
                     url  => "http://www.infoseek.com",
                     tag  =>
                        "/Titles?col=WW&qt=$subject&sv=IS&lk=noframes&nh=$hits",
                     img  => "images/infoseek.gif",
                     txt  => "Infoseek hits for \"<B>$subject</B>\""
                  },
                  {
                     name => "Excite",
                     url  => "http://www.excite.com",
                     tag  => "/search.gw?trace=a&search=$subject",
                     img  => "images/excite.gif",
                     txt  => "Excite hits for \"<B>$subject</B>\""
                 },
                 {
                     name => "Metacrawler",
                     url  => "http://www.metacrawler.com",
                     tag  => 
       "/crawler?general=$subject&method=0®ion=&rpp=20&timeout=10&hpe=$hits",
                     img  => "images/metacrawler.gif",
                     txt  => "Metacrawler hits for \"<B>$subject</B>\""
                 }
             );
         
         print "<table align=center>\n";

         my $search;         
         foreach $search (@search_engines)
         {
             my ($yc, $xc) = (100,40);
             my ($img, $url, $tag, $txt) =
                 (
                  $search->{'img'}, $search->{'url'},
                  $search->{'tag'}, $search->{'txt'}
                 );

             if ($engines eq "All")
             {
                 # Create links to all search engine results
print <<"EOF_PRINT";
<tr>
    <td align=center> 
        <a href= "$url$tag"> 
        <img src="$img" align=center border=2 height=$xc width=$yc></a>
    </td>
    <td>
        <a href= "$url$tag"> 
        $search->{'txt'}</a>
    </td>
EOF_PRINT
             }
             elsif ($engines eq $search->{'name'})
             {
                 # Create a link to the specific search engine the user
                 # asked for
print <<"EOF_PRINT";
<tr>
    <td align=center> 
    <a href= "$url$tag"> 
    <img src="$img" align=center border=2 height=$xc width=$yc></a>
    </td>
    <td>
        <a href= "$url$tag"></a>
        $txt
    </td>
</tr>
EOF_PRINT
                 last;
             }
         }
         # End the table
         print "</table>\n";
         
         # Provide a link back to the main page
         print p(),
         $query->a({href=>"meta.cgi"}, "Return to META SEARCH home");
    }
    else
    {
         # User didn't input a query string, create page to tell them
         print "You submitted an empty query.", p(), 
         "Hit ", $query->b("back"), " on your",
         " browser, or click below and ", $query->i("try again"), "!", p(),
         $query->a({href=>"meta.cgi"}, "Return to META SEARCH home");
    }
}
else
{
    # Main/Start page creation
    # Create a form to ask user for query, number of hits, and search
    # engines to use
    print $query->h1('META SEARCH ENGINE'),
    start_form(-name=>'form1', -onSubmit=>"return validateHits(this)"),
    "<table>", 
    Tr(), td(), "What subject do you want to look for?", 
    td(), $query->textfield('subject'), 
    p(), Tr(), td(), "How many hits do you want to find?   ",
    td(), $query->textfield(-name=>'hits',-default=>'20'),
    p(), Tr(), td(), "Your choice of search engines?",
    td(),
    $query->radio_group(-name=>'engines',
                              -values=>['All', 'Yahoo', 'Lycos', 'Alta-Vista', 
                                           'Infoseek', 'Excite', 'Metacrawler'],
                              -default=>'All', -linebreak=>'true'),
    "</table>", "<center>", $query->submit(), "</center>",
    p(), end_form();
}

# End the html document
print end_html();