#!/usr/athena/bin/perl -T

$WWWROOT = "/var/www/root";

print <<HTTPHEAD;
Content-Type: text/html
Status: $ENV{'REDIRECT_STATUS'} Gone

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Gone</title>
</head>

<body>
<h1>410 Gone</h1>
HTTPHEAD

$ru = $ENV{'REDIRECT_URL'};

if ($ru =~ m:^/people/([^/]+)$:) {
   $person = $1;
   $actual = readlink "$WWWROOT/people/$person";
}
elsif ($ru =~ m:^/people/([^/]+)/:) {
   $person = $1;
   $actual = readlink "$WWWROOT/people/$person";
}

undef $addr;

if ($actual =~ m|/afs/sipb(\.mit\.edu)?/user/(\w{2,8})|) {
   $addr = $2;
} elsif ($actual =~ m%/afs/athena(\.mit\.edu)?/user/(./.|./other|other)/(\w{2,8})%) {
   $addr = $3;
}

$hesiod = 1;

# safely exec hesinfo to determine if the address we've divined is
# valid
if($addr) {
   $pid = open(CHILD, "-|");
   if(defined $pid) {
      if($pid == 0) {
# keep hesinfo from writing to our stderr
         open(SAVERR, ">&STDERR") || warn "unable to dup stderr\n";
         open(STDERR, ">/dev/null") || warn "stderr redirect failed\n";

         exec "/bin/athena/hesinfo", $addr, "pobox";

# retrieve stderr because we have an error to report
         open(STDERR, ">&SAVERR");
         die "exec failed (/bin/athena/hesinfo): $!\n";
      } else {
         @hesdata = <CHILD>;
         close(CHILD);
# child will return zero on success, nonzero on failure
         if($? != 0) {
	     $hesiod = 0;
         }
      }
   } else {
      warn "unable to fork to exec hesinfo\n";
   }
}

# if all the above tests are passed, $addr will have a value, otherwise
# it'll be undefined
if($hesiod == 1) {
   print <<UCONTACT;

<p>You tried to access</p>
<div align=center><code>http://stuff.mit.edu$ENV{'REDIRECT_URL'}</code></div>
<p>This web page is no longer present on our server. A possible
contact address for the owner of this web page is
<a href="mailto:$addr\@mit.edu">$addr\@mit.edu</a>.
If you send mail, please include the URL you tried to access in
the message you send so the owner of the page can fix the problem more
easily.</p>
UCONTACT
} else {
   print <<WCONTACT;
<p>The person with the username <b>$addr</b> no longer has an MIT account and
their web pages are no longer available on stuff.mit.edu.</p>
<p>Usually this happens because</p>
<ul>
<li>the person was a student but has since graduated
<li>the person formerly worked at MIT but has left
</ul>
<p>The web page you requested was in a personal directory of a former student or
staff member at MIT. Since this person is gone now, their personal
directory has been removed. Their web pages are not located anywhere on
stuff.mit.edu.
We don't know whether this person has a web page at some other site, or
which other site would be likely.
If you happen to be an MIT alumnus/alumna,
you may be able to locate this person via the <a href="http://web.mit.edu/alum/">MIT Alumni Association</a>.</p>
<p>To try to locate this person's web pages, you could also visit search
engines such as</p>
<ul> 
<li><a href="http://www.google.com/">Google</a>
<li><a href="http://www.altavista.com/">altavista</a>
<li><a href="http://www.alltheweb.com/">alltheweb</a>
<li><a href="http://www.hotbot.com/">Hotbot</a>
<li><a href="http://www.lycos.com/">Lycos</a>
</ul>
WCONTACT
}

print <<HTTPTAIL;
</body>
</html>
HTTPTAIL

exit 0;
