#!/usr/athena/bin/perl -wT
# whats.cgi
# Whats acronym database gateway.
# $Id: whats.cgi,v 1.9 2009/09/01 01:12:05 root Exp $
# Chris Laas (golem@mit.edu) 12/98

# Requires CGI.pm version 2.42
use CGI;
#BEGIN { require '/var/www/cgi/lib.updates/CGI.pm'; }
$q = new CGI;

sub webdie {
  my ($msg) = @_;
  print $q->header('text/html', '500 Internal Server Error');
  print $q->start_html('Internal Server Error');
  print $q->h1('Internal Server Error');
  print $q->code($msg);
  print $q->p(<<'EOT');
For help, please send mail to
<A HREF="mailto:stuffmaster\@mit.edu">stuffmaster\@mit.edu</A>,
and include this error message, this URL, and the time and
date of this error.
EOT
  print $q->end_html();
  print "\n";

  # Ripped vaguely out of CGI::Carp.
  my ($pack, $file, $line) = caller(0);
  $msg .= " at $file line $line.\n" unless $msg =~ /\n$/;

  my $time = scalar(localtime);
  my $frame = 0;
  do {
	$id = $file;
	($pack, $file) = caller($frame++);
  } until !$file;
  ($id) = $id =~ m|([^/]+)$|;
  my $stamp = "[$time] $id: ";
  $msg =~ s/^/$stamp/gm;
  die($msg);
}

if ($q->param) {
  ($lookup = $q->param('keywords')) =~ tr/a-z/A-Z/;
  eval { $_ = ""; /^($lookup)\s+-\s*/oi };
  if ($@) {
	print $q->header;
	print $q->start_html('Whats Acronym Database Interface');
	print $q->h1('Invalid search string');
	print "The search keyword you specified was not a valid acronym\n";
	print "or regular expression.  Try again, using only alphanumeric\n";
	print "characters.\n";
  } else {
	@hits = ();
	$lasthit = '';

#	@acron = glob '/afs/.sipb.mit.edu/project/sipb/share/acron/acron*';
	@acron = map("/afs/.sipb.mit.edu/project/sipb/share/acron/$_",
				 qw(acron acron.sipb acron.vera));
	while (@acron) {
	  $acron = shift @acron;
	  open ACRON, $acron
		  or webdie "Internal error reading acronym database: $!";
	  while (<ACRON>) {
		if (/^($lookup)\s+-\s*/oi) {
		  push @hits, $q->dt($q->b($1)) if $lasthit ne $1;
		  $lasthit = $1;
		  push @hits, $q->dd($');
		}
	  }
	  close ACRON;
	}

	print $q->header;
	print $q->start_html('Results of whats query');
	print $q->h1('Results of whats query:');
	if (@hits) {
	  print $q->dl(@hits);
	} else {
	  print "Nothing in the database matched your query.<BR><BR>\n";
	  print "If you would like to submit a definition for $lookup, you\n";
	  print "can use this form:<BR>\n";
	  print "<FORM METHOD=POST ACTION=\"http://stuff.mit.edu/cgi/comment\">\n";
	  print "<INPUT TYPE=HIDDEN NAME=to VALUE=\"sipb-whats\@mit.edu\">\n";
	  print "<INPUT TYPE=HIDDEN NAME=url VALUE=\"New acron entry: $lookup (submitted from ", $q->url(), ")\">\n";
	  print "Your email address: <INPUT NAME=email SIZE=30><BR>\n";
	  print "\"$lookup\" stands for: <INPUT NAME=body SIZE=60><BR>\n";
	  print "<INPUT TYPE=SUBMIT VALUE=\"Submit\">\n";
	  print "</FORM>\n";
	}
  }
} else {
  print $q->header;
  print $q->start_html('Whats Acronym Database Interface');
  print $q->h1('Web-based interface to the "Whats" Acronym Database');
}
print $q->isindex($q->url());
print <<'EOT';
<I><FONT SIZE=-2>
This uses, in part, an acronym compilation copyrighted by
<A HREF="http://cgi.snafu.de/ohei/user-cgi-bin/veramain-e.cgi">
Oliver Heidelbach</A>.<BR>
Also uses an acronym compilation by
Dave Sill (dsill@nswc-oas.arpa) (de5@ornl.gov as of 6/90).<BR>
In addition, it uses acronyms compiled by the
<A HREF="http://stuff.mit.edu/sipb/sipb.html">
MIT Student Information Processing Board</A>.<BR>
</FONT></I>
<HR>
<I><A HREF="http://stuff.mit.edu/help/before-you-mail.html">
stuffmaster@mit.edu</A></I>
EOT
print $q->end_html;
print "\n";
