#!/usr/athena/bin/perl

# Mozilla sends "GET /home?isindex="
if (substr($ENV{'QUERY_STRING'}, 0, 8) eq "isindex="){
    $who = substr($ENV{'QUERY_STRING'}, 8);
    $who =~ s/\+/ /g;
    $who =~ s/%27/'/g;
}
else {
    $query = join(' ', @ARGV) if $ARGV[0];
    $who = $query;
}

# traditionally http://stuff.mit.edu/home has redirected to
# http://stuff.mit.edu
if ($who eq ""){
    print("Location: http://stuff.mit.edu/\n\n");
    exit;
}

open(HOME, "/afs/sipb.mit.edu/project/www/root-stuff/Home-byUser.html");	

while(<HOME>){
    if(/<a name=\"([^\"]+)\" href=\"([^\"]+)\">/i){
	$name = $1;
	$url = $2;
    }	   
    if($name eq $who){
	if($url !~ /^http/){
	    $url = ("http://stuff.mit.edu" . $url);
	}
	print("Location: $url\n\n");
	exit;
    }
}
close(HOME);

open(HOME, "/afs/sipb.mit.edu/project/www/root-stuff/Home-byUser.html");	

while(<HOME>){
    @entry = grep(/$who/i, <HOME>);
    if ($entry[1]) {
	print <<END ;
Content-Type: text/html

<!doctype HTML public "-//IETF//DTD HTML 2.0//EN">
<html>
<head>
<title>MIT Community Home Pages: Multiple Matches</title>
</head>
<body>
<p><img align="left" src="/gif/MIThome_cropped.GIF" alt="[fireplace]"
width=123 height=124 hspace=10 vspace=10> </p>
<h1>Multiple Matches</h1>
<p> There were multiple matches to your query.
Please choose one of the following: </p>
<br CLEAR=LEFT>
<ul>
END

for ($i = 0; $i <= $#entry; ++$i){
    if ($entry[$i] =~ /<li>/){
        print $entry[$i];
    }
}
	print <<END ;
</ul>
<hr>
<form action="/home">
<p CLEAR=left>Or enter another name/username: <input name="isindex"></p>
</form>
<hr>
<a href="whats-new.html">
<img alt="What's new?" src="/gif/Icon_NEW.GIF" width=72 height=72></a>
<a href="services.html">
<img alt="SIPB services" src="/gif/Icon_GATE.GIF"></a>
<a href="server-pages.html">
<img alt="Other MIT Servers" src="/gif/Icon_SERV.GIF"></a>
<a href="mit-activities.html">
<img alt="Activities" src="/gif/Icon_ORG.GIF"></a>
<a href="documents.html">
<img alt="Documents" src="/gif/Icon_DOCS.GIF"></a>
<a href="fun-stuff.html">
<img alt="Fun Stuff" src="/gif/Icon_FUN.GIF"></a>
<hr>
<address>
MIT Student Information Processing Board |
<a href="/stuffmasters.html">stuffmaster\@mit.edu</a>
</address>
</hr>
</body>
</html>
END
	}
    else {
	@loc = split(/\"/, $entry[0]);
	$url = @loc[3];
	if($url !~ /^http/){
	    $url = ("http://stuff.mit.edu" . $url);
	}
	if (@entry) {
	    print("Location: $url\n\n");
	}
    }
}
if (! @entry) {
    print("Location: http://stuff.mit.edu/Home-not-found.html\n\n");
}
