#!/usr/local/bin/perl
#
# susi-grep.pl: unified search index
# Version 0.1
# Date: Thu Jan  6 09:44:55 GMT 1994
# 
# Martijn Koster (m.koster@nexor.co.uk)

package susi;

# adjust this for your site.

$hostname = `hostname` =~/victor/ ? 'localweb' : 'web';
$hostname .= '.nexor.co.uk';

&main'debug("susi: hostname for local redirection = $hostname");

# adjust this to your preference
# the single space in the URL is replaced with the query string

%servers = (
	    'ALIWEB',	"http://web.nexor.co.uk/aliwebsimple? ",
	    'W3 Catalog at NEXOR',	"http://$hostname/w3catalog? ",
	    'W3 Catalog',	'http://cui_www.unige.ch/w3catalog? ',
	    'Sinbad', "http://$hostname/sinbadgw/read:mak/sinbad/index.db? ",
	    'RBSE', 'http://rbse.jsc.nasa.gov/htbin/urlsearch? ',
	    'JumpStation', 'http://www.stir.ac.uk/jsbin/h1_js? ',
	    'WWW Nomad', 'http://www.rns.com/cgi-bin/parse?Topic= ',
	    'Archie',	"http://$hostname/archieplex/server=archie.doc.ic.ac.uk/type=substring/order=host? ",
	    'WAIS', 'wais://cnidr.org:210/directory-of-servers? ',
	    'Windows Archive', 'gopher://micros.hensa.ac.uk/77/%2bgopher/%2bmicros/%2bibmpc/%2bwin/%2bwais/index? ',
	    'Mac Archive', "http://$hostname/mac-archive-find/grep? ",
	    'RFC Index', "http://$hostname/rfcindex? ",
	    'ID Index', "http://$hostname/idindex? ",
	    );
	
@fields = ('service', 'query');
@requiredfields = ('service', 'query');

sub main'do_susi
{
    local($query) = @_;       

    $query ? &search($query) : &dialog;
}


sub dialog
{
     &main'MIME_header('ok', 'text/html');

    print <<'DIALOG';
<HEAD><TITLE>SUSI Search</TITLE></HEAD>
<BODY><H1>SUSI Search</H1>

This is a simple unified search interface for several search 
engines in the Web. 

<FORM ACTION="/susigrep">
Search term: <INPUT NAME="query"> 
<inPUT TYPE="submit" VALUE="Submit">
<P>
Search Engine: 

<SELECT NAME="service">
DIALOG
foreach (sort keys %servers)
{
    print "<OPTION>$_\n";
}
print <<'DIALOG';
</SELECT>

<HR>
<ADDRESS><A HREF="/mak/mak.html">
Martijn Koster</A></ADDRESS>
</BODY>
DIALOG
}

sub search
{
    local($query) = @_;

    local(@terms) = split('&', $query);
	
    for(@terms)
    {
        local($tag, $value) = split('=', $_, 2);
    
	&main'error('bad_request', "Unknown tag: '$tag'. ") if (! grep(/^$tag$/, @fields));

	$value =~ s/\+/ /g;

	$value =~ s/%([\da-f]{1,2})/pack(C,hex($1))/eig;
	&main'error('bad_request', "Duplicate tag: '$tag'.") if ($tags{$tag});
	$tags{$tag} = $value;
    }

    # some specific checking to supply useful error messages
    # prevents user problems being reported as internal problems
    # There might be a better facility for this in Plexus 3.0

    if ($tags{'query'} eq '')
    {
	print 'Please specify a search term.';
	return;
    }
    # make sure required tags are there

    for (@requiredfields)
    {				
	&main'error('bad_request', "Missing required tag '<code>$_</code>'.<P>\n" .
	'You probably have an browser that cannot handle forms properly.')
            if  (!$tags{$_});
    }

    &main'error('bad_request', "Please specify a search term.") if (!$query);
    
    $service = $tags{'service'}; # lookup what the user filled in
    $url = $servers{$service};

    &main'error('bad_request', "Unknown Server '<code>$service</code>'. \n")
	    if (!$url);
    $query = $tags{'query'};
    $url =~ s/\s/$query/;
    &main'add_header(*main'out_headers, "Location: $url");
    &main'MIME_header('found', 'text/html');
    print "This document has moved to <A HREF=\"$url\">$service</A>";

    return;
}
