#!/usr/athena/bin/perl
# mkgray.pl -- gateway to weather
#
# $Id: weather,v 1.8 95/05/17 23:18:27 wwwmaint Exp $
#
# Matthew Gray (mkgray@athena.mit.edu)
#

require 'our-chat2.pl';
require 'citycode.pl';
require 'ourfork.pl';

$| = 1;
open(STDERR,">&STDOUT");

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

&ourfork'safefork(15, "do_weather", $query);

# package weather;

sub do_weather {
    local($station) = @_;

    print("Content-Type: text/html\n\n");

    $cmd = $ENV{'PATH_INFO'};
    $cmd =~ s%^/%%;

    if ($cmd eq "usmap") {
	&do_usmap($station);
    }
    elsif ($station) {
	print "<isindex>".&get_weather($station)."</pre>\n";
    }
    else{
	print <<"EndOfInfo";
<TITLE>Weather</TITLE>
<h1>Weather</H1>
This is the weather gateway.  If you find any bugs,
send mail to <a href="/webmasters.html"><em>webmaster@mit.edu</em></a>
or enter your <a href="/comment">comments here</a>.<p>
Lately,
<a href="telnet://downwind.sprl.umich.edu:3000/">
the server from which we get our weather data</a>
has been kind of flaky. If your weather queries time out, 
this is most likely the reason why.<p>

If you know the <a href="/city-codes.html">city code</a> of the forecast
you want, enter it in the search field.<p>
<H2><A HREF="/usa.html">Click here for current weather map and forecasts
across the nation</A></H2><P>
<H2><A HREF="/weather?bos">Click here for Boston forecast</A></H2><P>
<ISINDEX>
EndOfInfo
    }
}

sub do_usmap {
    local($args) = @_;
    local($x, $y);

    if ($args !~ /,/) {
	print "<isindex>" . &get_weather($args) . "</pre>\n";
    }

    ($x, $y) = split(',', $args);

    @name = ('BOS','LAX','MCO','TOP','NYC','SLC','BWI','FTW','CHI','NEW',
	     'DSM','DEN','MEM','PHX','SEA','ATL');
    @x = (750,100,675,440,700,225,700,400,550,500,475,300,600,200,150,613);
    @y = (175,325,475,320,200,250,250,450,250,475,260,300,350,375,100,382);

    $min = 1000000;
    foreach $n (0..$#x){
	$locale = $name[$n];
	if(($dist=&distance($x, $y, $x[$n], $y[$n])) < $min){
	    $min = $dist;
	    $whereami = $locale;
	}
    }

    print("<title>Nearest Weather Station is $whereami</title><isindex>\n");
    print &get_weather($whereami)."</pre>\n";
}

sub distance {
    local($x1, $y1, $x2, $y2) = @_;
    local($xd) = (($x1-$x2)*($x1-$x2));
    local($sqd)=$xd + (($y1-$y2)*($y1-$y2));
    $sqd;
}

sub get_weather {
    local($station, $more_than_one_match) = @_;

    $rawstation = $station;

    $station =~ tr/[A-Z]/[a-z]/;
    $station =~ tr/[_\-\.\+]/ /;
    $station =~ s/%20/ /g;
	print <<"EndOfInfo2";
Lately,
<a href="telnet://downwind.sprl.umich.edu:3000/">the server from which we get
our weather data</a> has been kind of flaky. If your weather queries time
out, this is most likely the reason why. The weather server should be
working again, eventually.<p>
EndOfInfo2

    print"<title>Weather Conditions for $rawstation</title>";

    if (((length($station) > 3) || ($station eq "rio") || ($station eq "key")) && ($station ne "boston")) {

        %citycodes = &citycode'citycode_assoc_array; #'
	print"<h1>Weather Conditions for $rawstation</h1>";

	if ($city_code = $citycodes{$station}) {
	    @matches = split('\:', $city_code);

	    $more_than_one_match = "yup" if ($#matches != 0);
	 
	    print"<hr>\nThis gateway is designed to accept three-letter ";
	    print"<a href=\"/city-codes.html\">city codes</a>. <p>\n";
	    print"Your query was not a valid city code. However, it\n";
	    print"matched one ", $more_than_one_match?"or more ":"";
	    print"of the entries we have in our\n";
	    print"local table of cities.\n<p>";
	    print"For your convenience, you may select ";
	    print $more_than_one_match?"one of these links.":"this link.","\n";
	    print"However, in order to minimize the load on our server, we\n";
	    print"ask that, if you wish to repeat this query in the future,\n";
	    print"you use ";
	    print $more_than_one_match?"one of these three-letter codes":"this three-letter code";
	    print" rather than the\n";
	    print"query you just made (\"$rawstation\").<p>\n";
	    print"<ul>\n";
	    for (@matches) {
		print "<li><a href=\"/weather?";
		print($_."\">".$_." (".$rawstation,")</a>\n");
	    }
	    return;
	}
	else {
	    print"Your query was not a three-letter ";
	    print"<a href=\"/city-codes.html\">city code</a>,\n";
	    print"and it did not match any of the city-to-city-code\n";
	    print"mappings we have on our server. If you have a city\n";
	    print"code for the query you just submitted, please please\n";
	    print"let us know about it so that we can add it to our\n";
	    print"database. You can either send mail to\n";
	    print"<a href=\"/webmasters.html\">webmaster@mit.edu</a> or\n";
	    print"use the forms-based ";
	    print"<a href=\"/comment\">comment gateway</a>\n";
	    print"on our server.\n<p>";
	    return;
	}
    }

    $closeme = &chat'open_port('downwind.sprl.umich.edu', 3000);
    &listen(2);
    &chat'print($station."\n");
    while($forecast!~/CITY FORECAST MENU/){
	$forecast .= &listen(1);
	&chat'print("\n");
    }
    &chat'print("x\n");
    &chat'close($closeme);
    $forecast=~s/Press Return to continue, M to return to menu, X to exit://g;
    $forecast=~s/CITY FORECAST MENU[\w\W]+//;
    $forecast=~s/\n\W\W([^\.\n]+)\.\.\./\n<h2>\1:<\/h2>/g;
    $forecast=~s/(Weather Conditions at .+)/<h1>\1<\/h1><pre>/;
    $forecast=~s/\n(.+)FORECAST\n/\n<h2>\1FORECAST<\/h2>\n/;
    $forecast=~s/Press Return for menu://;
    return $forecast;
}
sub listen {
        local($secs) = @_;
        local($return,$tmp) = "";
        while (length($tmp = &chat'expect($secs, '(.|\n)+', '$&'))) {
                print $tmp if $trace;
                $return .= $tmp;
        }
        $return;
}

1;
