#!/usr/athena/bin/perl

$|=1;
require '/stuff/civil.pl';
$searchf = join(" ", @ARGV);
($search, $state) = split(",", $searchf);
$state =~ s/\s//g;
$matchstate = "";
if($state){
    $state=~y/a-z/A-Z/;
    $matchstate = $abbrev{$state};
}
@matches = `grep -i "$search" /stuff/geonames`;
#open(DB, "/stuff/geonames");
#while(<DB>){
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;
    }
    
    $ct++;
    if($feature == 19){
	if($county == 0){
	    print "\$abbrev{\"$remark\"} = \"$locname\";\n";
	}
    }
    if((!$matchstate) || ($statename{$state} eq $matchstate)){
    print "Location: $locname\n";
    print "Nation: $nation\n";
    print "State: $statename{$state}\n" if $statename{$state};
    print "County: $countyname{$state.$county}\n" if $countyname{$state.$county};
    print "Lat/Long: $lat $long\n" if ($lat && $long);
    print "Population: $pop\n" if $pop=~/\S/;
    print "Elevation: $elev\n" if $elev=~/\S/;
    print "Remark: $remark\n" if $remark;
    print "Postal code: $postal\n" if $postal;
    print "\n";
}
    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;
}
}


exit;
require '/mit/mkgray/perl/chat2.pl';


sub listen {
        local($secs) = @_;
        local($return,$tmp) = "";
        while (length($tmp = &chat'expect($secs, '(.|\n)+', '$&'))) {
                print $tmp if $trace;
                $return .= $tmp;
        }
	$return;
}
# 0 <city name>
# 1 <county FIPS code> <county name>
# 2 <state/province abbreviation> <state/province name>
# 3 <nation abbreviation> <nation name>
# A <telephone area code>
# E <elevation in feet above mean sea level>
# F <feature code> <feature name>
# L <latitude DD MM SS X> <longitude DDD MM SS X>
# P <1980 census population>
# R <remark>
# T <time zone>
# Z <postal ("ZIP") code>


$request = join(' ', @ARGV);
&chat'open_port('martini.eecs.umich.edu', 3000) || die "open: $!";
$_=&listen(2);
while(!/Geographic/){
$_=&listen(5);
}
print("Geographic NameServer Query:  \"$request\"\n");
&chat'print("$request\n");
$_=&listen(3);
split(/\n/);
foreach $dataline (@_){
	@els = split(/[ ]+/, $dataline);
	$foo =shift(@els);
	if($foo=~/0/){
		$rest = join(' ',@els);
		chop($rest);
                print("\nLocation Name:  $rest\n");
                }
        if($foo=~/1/){
		$bar = shift(@els);
                $rest = join(' ', @els);
		chop($rest);
                print("County:         $rest ($bar)\n");
		}
	if($foo=~/2/){
		$bar = shift(@els);
		$rest = join(' ', @els);
		chop($rest);
		print("State/Province: $rest ($bar)\n");
	}
	if($foo=~/3/){
		$bar = shift(@els);
		$rest = join(' ', @els);
		chop($rest);
		print("Country:        $rest ($bar)\n");
	}
	if($foo=~/A/){
		$rest = join(' ', @els);
		chop($rest);
		print("Area Code:      $rest\n");
	}
	if($foo=~/E/){
		$rest = join(' ', @els);
		chop($rest);
		print("Elevation:      $rest\n");
	}
	if($foo=~/L/){
		print("Latitude:       $els[0] $els[1]' $els[2]\" $els[3]\n");
		print("Longitude:      $els[4] $els[5]' $els[6]\" $els[7]\n");
	}
	if($foo=~/P/){
		print("Population:     $els[0]\n");
	}
	if($foo=~/T/){
		print("Time Zone:      $els[0]\n");
	}
	if($foo=~/Z/){
		print("Zip Code:       $els[0]\n");
	}
	if($foo=~/R/){
		$rest = join(' ', @els);
		print("Comment:	       $rest\n");
	}

}
&chat'print("quit\n");
