#!/usr/athena/bin/perl

$|=1;
use lib '/var/www/cgi-bin/lib';
require '/var/www/data/geo/civil.pl';
require 'parseform.pl';
%form = &parseform($ENV{'QUERY_STRING'});
$searchf = $form{'location'};
$searchf =~ s/[^A-Za-z0-9 \t_',.&\/\+-]//g;

&mainscreen();
if(!$searchf){
    exit;
}

($search, $state) = split(",", $searchf);
$state =~ s/\s//g;
$matchstate = "";
if($state){
    $state=~y/a-z/A-Z/;
    $matchstate = $abbrev{$state};
}
open(MATCHES, "/bin/grep -i -- \"$search\" /var/www/data/geo/geonames|");;
print "<h2>Search for $searchf</h2>\n";
print <<HEADER;
<table border width=100%><tr>
<th>Placename</th>
<th>State</th>
<th>County</th>
<th>Lat/Long</th>
<th>Elevation</th>
<th>Population</th>
<th>Remark</th>
<th width=10%>Postal code</th>
<th>Nation</th>
</tr>
HEADER

foreach (<MATCHES>){
    /(..).(..).(..)(...).(.......)(........).(.......).(.....)(.+)/;
    $nation = $1;
    $feature = $2;
    $state = $3;
    $county = $4;
    $lat = $5;
    $long = $6;
    $pop = $7;
    $elev = $8;
    $rest = $9;

    $lat =~ s/\s//g;
    $long =~ s/\s//g;

    if($lat =~ /(..)(..)(..)(.)/){
	$lat = "$1:$2:$3 $4";
    }

    if($long =~ /(...)(..)(..)(.)/){
	$long = "$1:$2:$3 $4";
    }

    $rest=~/\"([^\"]+)\"(.*)/;
    $locname = $1;
    $other = $2;

    $postal = $remark = "";
    if($other=~/\(([^\)]+)\)/){
	$remark = $1;
    }
    if($other=~/\[([^\]]+)\]/){
	$postal = $1;
	$postal =~ s/,/, /g;
    }
    
    if($feature == 19){
	if($county == 0){
	    print "\$abbrev{\"$remark\"} = \"$locname\";\n";
	}
    }
    if((!$matchstate) || ($statename{$state} eq $matchstate)){
    $ct++;
    if($ct%10 == 0){
	print "<tr>\n<th>Placename</th>\n<th>State</th>\n<th>County</th>\n<th>Lat/Long</th>\n<th>Elevation</th>\n<th>Population</th>\n<th>Remark</th>\n<th width=10%>Postal code</th>\n<th>Nation</th>\n</tr>\n";

    }

	print <<REC;
<tr>
<td>$locname</td>
<td>$statename{$state}</td>
<td>$countyname{$state.$county}</td>
<td>$lat $long</td>
<td>$elev</td>
<td>$pop</td>
<td>$remark</td>
<td width=10%>$postal</td>
<td>$nation</td>
</tr>
REC

}
    if(0){
    push(@places, $locname);
    $nation{$locname} = $nation;
    $feature{$locname} = $feature;
    $state{$locname} = $state;
    $county{$locname} = $county;
    $lat{$locname} = $lat;
    $long{$locname} = $long;
    $pop{$locname} = $pop;
    $elev{$locname} = $elev;
    $remark{$locname} = $remark;
    $postal{$locname} = $postal;
}
}
print "</table>\n</body>\n</html>\n";

exit;

sub mainscreen {
        print <<"EndOfInfo";
Content-type: text/html

<html>
<head>
<title>Geographic Nameserver</title>
</head>
<body>
<h1><img align=top src="/bitmaps/globe.xbm">Geographic Nameserver</h1>

<h1>The information on this server is not current.  For current information see:</h1>

<h2><a href="http://geonames.usgs.gov/pls/gnis/web_query.gnis_web_query_form">
http://geonames.usgs.gov/pls/gnis/web_query.gnis_web_query_form</a></h2>
<p>If you are looking for listings by city or zip code information, you could 
also try:</p><p>
<a href="http://www.census.gov/cgi-bin/gazetteer/">http://www.census.gov/cgi-bin/gazetteer/</a></p>
<h2>Enter the name of the place you want to look up</h2>
<p>For example, enter "<a href="/geo?location=cambridge,+ma">cambridge,
ma</a>" to look up Cambridge, Massachusetts.  If you enter just "<a
href="/geo?location=cambridge">cambridge</a>", you will get information
on all towns called Cambridge.<p> This server contains mostly
information on locations within the US.  It uses the information from
the geographic nameserver database formerly on martini.eecs.umich.edu.</p>
<h2>This data was last considered current circa 1987.</h2>

<form method=GET action=/geo>
Placename: <input name=location>
<input type=submit value="Submit">
</form>
<hr>
</body>
</html>
EndOfInfo
}
