#/usr/local/bin/perl

if($#ARGV < 0) {&usage();}
sub usage {
	print("Usage:\n");
	print("\tgeo place name\n");
	exit(0);
}


require '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");