#!/usr/bin/perl
#
use Sherch::SherlockSearch;
use CGI;

print "Content-type: text/html\n\n";

opendir(PLUGIN,"plugins") or die "Couldn't open plugins directory";
@files = readdir(PLUGIN);
$filenames = join(' ',@files);
@servicenames = grep(/\.src$/i,@files);
closedir PLUGIN;

print <<END_HEADER;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD>
<TITLE>Sherch</TITLE>
</HEAD>
<BODY BGCOLOR="#ffffff">
<!-- HTML hacked by dan :) -->
<img src="http://www.sherch.com/2001/rdfweb-sherch/mozdata/sherchani.gif" alt="Sherch!" width=18 height=18" />

<DIV ALIGN="center">
<H1>Welcome to Sherch</H1>
<H3>- Sherlock for the rest of us -</H3>
</DIV>
You can <A HREF="submitSRC.pl">add</A> services if you want to.
<HR>
<FORM ACTION="sherch.pl" METHOD="POST">
<H4>Services available -</H4>
<I>Please note: if plugins do not work please confirm that they should work before
emailing me. Sorry, but many plugins out there are out of date :-(</I>
END_HEADER

$query = new CGI;
$queryterm = $query->param('searchterm');
@selectedservices = $query->param('service');

$match = join ('|',@selectedservices);

$maxnumberacross=5;
$counter=0;

print "<TABLE><TR VALIGN=\"top\">\n";

foreach (@servicenames)
  {
    $service = new SherlockSearch;
    $services{$_} = $service;
    $service->loadFile("plugins/".$_);
    print "<TD>";
    # The original version used an old gd which produced gifs
    # This covers those old images
    $pngfile = $_;
    $pngfile=~s/\.src//;
    $giffile = "$_.gif";
    $rssfile = "$pngfile.rss";
    $rssfile=~s/.*\///; #this hack will break
    $pngfile.= '.png';
    if ($filenames =~ /($giffile|$pngfile)/)
      { print "<IMG SRC=\"/~pldms/plugins/$1\"><BR>"; }
    else { print "<IMG SRC=\"/~pldms/plugins/generic.png\"><BR>"; }
    print "<INPUT TYPE=\"CHECKBOX\" NAME=\"service\" VALUE=$_";
    print " CHECKED=\"TRUE\"" if ($match && /($match)/);
    print ">";
    print $service->{search}->{name};
    if ($description = $service->{search}->{description})
      { print "<BR><I>\"$description\"</I>"; }
    print "<BR><A HREF=\"$rssfile\">RSS 1.0 Channel</A>\n";
    print "<BR><A HREF=\"/~pldms/plugins/$_\">Sherlock Source</A>\n";
    print "</TD>\n";
    if (++$counter==$maxnumberacross)
      { print "</TR>\n<TR VALIGN=\"top\">"; $counter=0;}
  }
print "</TR>\n" unless $counter;
print "</TABLE>\n";

print <<END_FORM;
<P><INPUT TYPE="TEXT" NAME="searchterm">
<BR><INPUT TYPE="SUBMIT" VALUE="Find it...">
</FORM>
<HR>
END_FORM

if ($queryterm && !$match)
  {
	print "You need to select services to use for searching";
  }

if ($match)
  {
    print "<H3>Search Results:</H3>";
    if (!$queryterm)
      {
	print "Nothin' to search for. Just kickin' my heels";
      }
    else
      {
	foreach $attempt (@selectedservices)
	  {
	    print "<p><b>Look for</b> \"$queryterm\"";
	    print "<br><b>Service :</b> ",$services{$attempt}->{search}->{name},"\n";
	    eval {$services{$attempt}->find($queryterm);};
	    if ($@) {print "<BR><I>$@</I><BR>\n";}
	    else {
	      $res = $services{$attempt}->getResults;
	      print $res->asHtmlString;}
	  }
      }
  }
print <<END_FOOTER;
<HR>
<I>This is a genuine botched service, written and performed by
<A HREF="mailto:D.M.Steer\@lse.ac.uk">Damian Steer</A>, 
&copy; 2001</I>
</BODY>
</HTML>
END_FOOTER

