#!/usr/athena/bin/perl
# $Id: geo,v 1.3 95/03/07 14:18:02 yonah Exp $

require 'our-chat2.pl';
require 'perlwww.pl';

$query = join(' ', @ARGV) if $ARGV[0];
&do_geo($query);

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>

sub do_geo {
    local($request)=@_;
    local($foo, $bar, $rest, $qreq, @els);
    $request =~ s/%([\da-f]{1,2})/pack(C,hex($1))/eig;
    $request =~ s/\+/ /g;

    if(!$request){
        print("Content-Type: text/html\n\n");
        # &main'MIME_header('ok', 'text/html'); # HTTP/1.0 compliance

        print <<"EndOfInfo";
<head>
<title>Geographic Nameserver</title>
<h1><img align=top src="bitmaps/globe.xbm">
  Enter the name of the place you want to look up</h1>

For example, enter "<a href="geo?cambridge,+ma">cambridge, ma</a>" to
look up Cambridge, Massachusetts.  If you enter just
"<a href="geo?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 on martini.eecs.umich.edu<p>

<isindex>
EndOfInfo
        return;
    }

    &chat'open_port('martini.eecs.umich.edu', 3000)
        || die "open: $!";

    while (!/Geographic/) { $_ = &listen(5);}
    &chat'print("$request\n");
    $_=&listen(3);
    split(/\n/);

    if ($#_) {
        print("Content-Type: text/html\n\n");        
        # &main'MIME_header('ok', 'text/html');   # HTTP/1.0 compliance
	print("<h1>Geographic NameServer Query:  \"$request\"</h1><pre>\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");
            }
        }
        print "</pre><isindex>";
    } else {
        print("Content-Type: text/html\n\n");
        # &main'MIME_header('ok', 'text/html');   # HTTP/1.0 compliance
	print("<h1>Geographic NameServer Query:  \"$request\"</h1>\n");
        print "Sorry, no matches found.<p>\n";
        if ($request =~ s/([^,]) ([^ ]+)$/\1, \2/) {
           $qreq = &perlwww'printable($request);
           print "Maybe you mean \"<a href=\"geo?$qreq\">$request</a>\"?<p>\n";
        }
    }
    &chat'print("quit\n");
}

1;
