#!/usr/bin/perl -w

## this is the primary search engine for the Artemis system

use HTML::Template;
require "db-wrapper.pl";

my $template = HTML::Template->new(filename => 'guitar.tmpl');
## first we need to get our cgi input...

my ($i, $in, @in, $key, $val);

# check what method we're using, GET or POST
if (&MethGet) {
    # for GET, everything's in the query_string environment variable
    $in = $ENV{'QUERY_STRING'};
} elsif (&MethPost) {
    # for post, we have to read in from STDIN
    read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
}

# split it into key/value pairs
# foo=bar&fred=barney becomes foo=bar and fred=barney, etc
@in = split(/[&;]/,$in);

  foreach $i (0 .. $#in) {
    # Split into key and value.
    ($key, $val) = split(/=/,$in[$i],2); # splits on the first =.
   
    # Associate key and value 
    $in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
    $in{$key} .= $val;
    
  }


# quick check to make sure our input isn't garbage
if(!&sanity_check(\%in)){ &fuck_that_shit(); }

# pick out the particular data that we care about right now:

my $query = $in{'query'};
my $hits  = $in{'hits'};

# we'll worry about the other fields later. not in the prototype.

my @results; # this will be an array of hashtables to store
             # the results of the local and meta searches

# search the local database first since that'll be fastest
&local_search();

# then search outside our site...
if(!($in{'local'} eq 'on')){
&meta_search();
}
# return our findings to the user.
&print_results();



# -------- SUBROUTINES ------------ #

sub MethGet {
  return ($ENV{'REQUEST_METHOD'} eq "GET");
}

sub MethPost {
  return ($ENV{'REQUEST_METHOD'} eq "POST");
}

sub local_search{
    # this one will just search the local database. or rather
    # it will ask the database to search itself and hand it the
    # results
    my @records = find_by_artist($query);
    push(@results,@records);
}

sub meta_search{

    # this one searches the outside sites. it doesn't actually
    # search them itself, it just calls all the plugin search
    # modules.
    require "listen.com.pl";
    push(@results, meta_search_listen_com($query));
    
}

sub print_results{
    my $rresults = shift @_;

    # this will just call the proper HTML::Template
    # functions and feed it the data that our searches
    # return.
    print "Content-Type: text/html\n\n";
    my @loop_data = ();
    my $size = 0;
    foreach(@results){
	my %temp;
	$temp{'artist'} = $_->{'artist'} || "unknown";
	$temp{'song'}   = $_->{'song'}   || "unknown";
	$temp{'format'} = $_->{'format'} || '??';
	$temp{'size'}   = $_->{'size'} || '??';
	$temp{'url'}    = $_->{'url'};
#	$temp{'artist'} = &strip_html($temp{'artist'});
#	$temp{'song'}   = &strip_html($temp{'song'});
#	$temp{'format'} = &strip_html($temp{'format'});
#	print STDERR $temp{'song'};
#	print STDERR $temp{'artist'};
	push(@loop_data, \%temp);
	$size++;
    }
    $template->param(size  => $size);
    $template->param(query => $query);
    $template->param(results_loop => \@loop_data);
    print $template->output();
    print $in{'query'};
    return 1;
}
    
sub sanity_check{
    
    # doesn't do much right now, but will eventually do a good
    # job of checking the input for all manner of stupidity
    # that the users could throw at it.

    my $rin = shift @_;
    return 0 unless $rin->{'query'};
    return 0 unless $rin->{'hits'};
    return 1;
}

sub fuck_that_shit{
    # this function is called when the user gives us
    # something bad. like if they ask us to do a query
    # without giving us anything to search for.
    # we basically want to just say something rude and
    # exit.

    print "Content-Type: text/html\n\n";
    print "<html><head><title>idiot</title></head><body>\n";
    print "<br />\n" x 10;
    print "<i>Yo mama's so stupid, she couldn't tell which way an elevator was going if I gave her two guesses.</i>";
    print "<p><b>you really screwed that query up. try it again and get it right or i shall taunt you a second time.</b></body></html>\n";

    die;
}







