#!/home/web/bin/perl
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#.IDENTIFICATION  rgosearch
#.LANGUAGE	  Perl Script
#.PURPOSE	  WWW cgi script to search RGO lists
#                 addresses
#.AUTHOR	  Michael Naumann [mn] 
#.KEYWORDS	  WWW, CGI, directory, RGO
#.ENVIRONMENT	  UNIX
#
#.COMMENTS	  Calling conventions :
#                 e.g.
#		  /misc/astrosearch/bin/rgosearch?mode=result&word=naumann&list=person&case=ignore&words=substring&fuzzy=0
#
# don't forget to treat *.lis file after download
# ./mk-list astroplaces.lis > m
#  mv m astroplaces.lis
# ./mk-list astropersons.lis > m
#  mv m astropersons.lis
# ./mk-list astropostal.lis > m
#  mv m astropostal.lis

#		  
#.VERSION	  $Id: rgosearch,v 1.2 1999/07/05 14:11:54 web Exp web $
#
#.COPYRIGHT       none
#------------------------------------------------------------------------------

use CGI qw(:standard :html3 :dump);
use File::Basename;

$DEBUG = "0";

open(STDERR,">&STDOUT") || die "Can't dup stdout: $!\n";
select(STDERR); $| = 1; # Make unbuffered.
select(STDOUT); $| = 1; # Make unbuffered.

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

$root    = "/home/web/eso/docs/misc/astrosearch";
chdir ($root);

$glimpse = "/home/web/bin/agrep";

$selfURL = "$ENV{'SCRIPT_NAME'}";
$URI="http://$ENV{'SERVER_NAME'}:$ENV{'SERVER_PORT'}$ENV{'SCRIPT_NAME'}$ENV{'PATH_INFO'}?$ENV{'QUERY_STRING'}";


$word  = param("word")  ? param("word") : "";
$list  = param("list")  ? param("list") : "person";
$case  = param("case")  ? param("case") : "ignore";
$fuzzy = param("fuzzy") ? param("fuzzy"): "0";
$qtype = param("mode")  ? param("mode") : "query";

$listname = $personselected =  $placeselected = $postalselected = "";
@fselect = "";
$considerselected = $ignoreselected = $substringselected = $wholeselected = "";

$gopt  = " -d '\$\$' "; # record separator is paragraph

if ($list eq "person") {
    $listfile = "$root" . "/astropersons.lis";
    $personselected = "selected";
}
elsif ($list eq "place") {
    $listfile = "$root" . "/astroplaces.lis";
    $placeselected = "selected";
}
elsif ($list eq "postal") {
    $listfile = "$root" . "/astropostal.lis";
    $postalselected = "selected";
}

if ($case eq "ignore") {
    $gopt = "$gopt" . " -i ";
    $ignoreselected = selected;
}
elsif ($case eq "consider") {
    $gopt = "$gopt" . "";
    $considerselected = selected;
}

if ($fuzzy eq "0") {
    $gopt = "$gopt" . "";
    $s0selected = "selected";
}
else {
    $gopt = "$gopt" . " -$fuzzy";
    $fselect[$fuzzy] = selected;
}

$lm = &LastModified("$listfile");

if ($DEBUG) {print &dump};

&PrintHeader;
&PrintBody;
&PrintFooter;


sub search {
    if ($DEBUG) {print "$glimpse $gopt -e \"$word\" $listfile\n"};
    open(TMP,"$glimpse $gopt -e \"$word\" $listfile 2> /dev/null|");
    while (<TMP>) {
	$line = $_;

	$line =~ s,(http://.*?\s),<a href="$1">$1</a>,g;
	print $line unless /^\s+$/;
	if ($line =~ /working-directory/) { next };
    }
    close (TMP);
}

sub PrintHeader {
    if ($qtype eq "query") {
	$title = 'Search RGO Astro-persons, -places, -addresses lists';
	$page = 'Search RGO Astro-persons, -places, -addresses lists';
    }
    elsif ($qtype eq "result") {
	$listname =  basename("$listfile", ".lis");
	$title = "Search result of<br>RGO $listname list";
	$page = "Search result of  RGO $listname list";
    }

    print qq (<html>
              <head>
              <link rel="StyleSheet" href="/styles/eso.style" type="text/css">
              
              <meta name="keywords" content="contact, address, email">
              <meta name="author" content="Michael.Naumann\@eso.org">
              <meta name="editor" content="Michael.Naumann\@eso.org">
              <meta name="maintainer" content="webmaster\@eso.org">
              <meta name="organisation" content="ESO, DMD, USG">
              
              <title>$page</title>
              </head>
              
       <body bgcolor="#ffffff" text="#000000" link="#0033cc">

<table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr><th rowspan="3" align="left" valign="top"><a href="/"
    title="Visit the ESO Web Site"><img
    align="bottom" width="80" height="100" src="/images/eso-logo.gif"
    border="0" alt=" [ ESO ] "></a></th>

<tr><td valign="bottom" align="right"><!-- #BeginEditable "HeadingTop" -->
<h1>Search Persons, Places, Addresses in Astronomy</h1>
<!-- #EndEditable --></td></tr>
 <!-- topnavstart --><tr> 
    <td nowrap align="right" valign="bottom"> 
      <table cellpadding="2" cellspacing="2" border="0" align="right">
        <tr align="right" valign="middle">
          <td class="nav-top" valign="bottom"><A class="nav-top" href="/"
       title="Visit the ESO Web Site">HOME</a></td>
      <td class="nav-top"><A class="nav-top" href="/toc/"
       title="ESO Web Contents">INDEX</a></td>
      <td class="nav-top"><A class="nav-top" href="/search/"
       title="Search the ESO Web Site">SEARCH</a></td>
      <td class="nav-top"><A class="nav-top" href="/help/"
       title="Need Help?">HELP</a></td>
      <td class="nav-top"><A class="nav-top" href="/news/"
       title="News and Updates">NEWS</a></td></tr>
     </table></td></tr>

<!-- topnavend --> 

</table>

              <hr size="2" noshade>);
}

sub PrintBody {
    if ($qtype eq "query" || $qtype eq "") {
	print qq (
                  <form 
		  action="http://rlspc5.bnsc.rl.ac.uk/cgi-bin/astrosearch.cgi">
		  <!-- <form method="get" action="$selfURL"> -->
		  <input type="hidden" name="mode" value="result">

		  <p>The data base for this search engine comprises 
                  the mirrored files astropersons.lis, astroplaces.lis, and 
                  astropostal.lis originally collected by Chris Benn
                  (La Palma) and Ralph Martin (Royal Greenwich 
                  Observatory), and from 1999, maintained 
                  <a href="mailto:kpd\@star.rl.ac.uk">Kevin Duffey</a>
                  (Starlink).

		  <p>Alternatively, <A 
		  HREF="http://star-www.rl.ac.uk/astrolist/astrosearch.html">http://star-www.rl.ac.uk/astrolist/astrosearch.html</A>
		  points to a search engine that goes for the most up to 
		  date file.

<p><table cellspacing="2" cellpadding="2" align="center" border="0">
  <tr valign="top">
      <td align=left><STRONG>Search string:</STRONG> </td>
      <td colspan=2 align=left><INPUT
      name="word" size="50" maxlength="50" value="$word"> <br></td>
      
  <tr valign="top">
      <td align=left><strong>Category:</strong></td>
      <td colspan=2 align=left><SELECT NAME="list">
	<OPTION value="person" $personselected>Persons in Astronomy
	<OPTION value="place" $placeselected>Astronomical Institutions
	<OPTION value="postal" $postalselected>Postal Addresses
      </SELECT><br></td>

  <tr valign="top">
      <td align=left><strong>Case:</strong></td>
      <td colspan=2 align=left><SELECT NAME="case" >
	<OPTION value="consider" $considerselected>consider
	<OPTION value="ignore" $ignoreselected>ignore
      </SELECT><br></td>
      
  <tr valign="top">
      <td align=left><strong>Match:</strong></td>
      <td align=left><SELECT NAME="words">
	<OPTION value="substring" $substringselected>substring
	<OPTION value="whole" $wholeselected>whole words
      </SELECT></td>
      <td align=right><SELECT NAME="fuzzy"> 
	<OPTION value="0" $fselect[0]>exact
	<OPTION value="1" $fselect[1]>1 speling error
	<OPTION value="2" $fselect[2]>2 spelang errors
	<OPTION value="3" $fselect[3]>3 spilllang errors
      </SELECT></td>
      
  <tr valign="top">
      <td>&nbsp;</td><td colspan=2><hr></td>
  <tr valign="top">
      <td>&nbsp;</td>
      <td colspan=2><INPUT TYPE="submit" VALUE="Search">
      <INPUT TYPE="reset" VALUE="Clear form">
</table></p>
</FORM>);

    }
    elsif ($qtype eq 'result') {

	$URI =~ s,mode=result,mode=query,g;

	print qq (
		  <p>Not found what you were looking for? just <a 
		  href="$URI">search again!</a></p>);

	print "<pre>";
	&search;
	print "</pre>";
	
    }
}


sub PrintFooter {
    print qq (
	      <p>You can make an <a href="http://star-www.rl.ac.uk/astrolist/astrosubmit.html">addition or amendment to the &quot;Persons 
in Astronomy&quot; database

<hr size="1" noshade>

<table width="100%" cellpadding="0" cellspacing="0" align="center" border="0">
    
<tr><td colspan="2" align="left" nowrap><table
    width="100%" cellpadding="0" cellspacing="0"  align="center"
    border="0"><tr><td align="left"><div class="footer"><small><a
       href="mailto:webmaster\@eso.org?subject=Page:$selfURL?$listname">Send
       comments to <tt>&lt;webmaster\@eso.org&gt;</tt></a><br>
Last update: $lm</small></div></td>
    <td nowrap align="right"><A href="/gen-fac/"
    title="Visit the ESO General Facilities"><img
       border="0" hspace="1" vspace="0" width="30" height="35"
       src="/images/icon-gen-fac.gif" alt=" [ESO General Facilities] " 
       align="bottom"></a><A href="/gen-fac/contact-info.html"
    title="Back to ESO Contact Info"><img
       border="0" hspace="1" vspace="0" width="30" height="35"
       src="/images/icon-up.gif" alt=" [Overview page for this document] "
       align="bottom"></a><A href="/"
    title="Visit the ESO Web Site"><img
       border="0" hspace="1" vspace="0" width="30" height="35"
       src="/images/icon-home.gif" alt=" [ESO] " 
       align="bottom"></a><A href="/toc/toc.html"
       title="ESO Web Index"><img
       border="0" hspace="1" vspace="0" width="30" height="35"
       src="/images/icon-index.gif" alt=" [Index] "
       align="bottom"></a><A href="/search/search.html"
       title="Search the ESO Web Site"><img
       border="0" hspace="1" vspace="0" width="30" height="35"
       src="/images/icon-search.gif" alt=" [Search] "
       align="bottom"></a><A href="/help/help.html"
       title="Need Help?"><img
       border="0" hspace="1" vspace="0" width="30" height="35"
       src="/images/icon-help.gif" alt=" [Help] "
       align="bottom"></a><A href="/news/news.html"
       title="News and Updates"><img
       border="0" hspace="1" vspace="0" width="30" height="35"
       src="/images/icon-whatsnew.gif" alt=" [News] "
       align="bottom"></a></td></tr></table></td></tr></table>
</body>
	      </html>);
}

sub LastModified {
    local ($file) = @_;

    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,
     $mtime,$ctime,$blksize,$blocks) = stat("$file");
    
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($ctime);

    $mon  = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$mon];
    $wday = (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[$wday];

    if ($year > 90) {
	$year = "19" . $year;
    }
    return "$wday, $mon $mday $year";
}

exit 1;

__END__




