#!/afs/athena/contrib/perl/p

$basecampURL = "http://toys.media.mit.edu/weather/14835";
$camp2URL = "http://toys.media.mit.edu/weather/14837";
$southcolURL = "http://toys.media.mit.edu/weather/14836";
$otherURL = "http://toys.media.mit.edu/weather/14838";

if(!defined $ARGV[0]){
$basecamp = &wwwfetch($basecampURL);
$camp2 = &wwwfetch($camp2URL);
$southcol = &wwwfetch($southcolURL);
$other = &wwwfetch($otherURL);
$lbasecamp = length($basecamp);
$lcamp2 = length($camp2);
$lsouthcol = length($southcol);
}

while(1){
 $basecamp = &wwwfetch($basecampURL);
 $camp2 = &wwwfetch($camp2URL);
 $southcol = &wwwfetch($southcolURL);
 $other = &wwwfetch($otherURL);
 $rep = "-------------------------------------------\n";
 $repdata = "";
 &report("BASE CAMP", $basecamp) if(length($basecamp) > $lbasecamp);
 $bct = $cur[3];
 &report("CAMP 2", $camp2) if(length($camp2) > $lcamp2);
 &report("the SOUTH COL", $southcol) if(length($southcol) > $lsouthcol);
 
 print "Lengths: $lbasecamp $lcamp2 $lsouthcol\n";
 print "Now: ", length($basecamp), " ", length($camp2), " ", length($southcol), "\n";
 if( (length($basecamp) > $lbasecamp) ||
    (length($camp2) > $lcamp2) ||
    (length($southcol) > $lsouthcol)){
     &sendreport();
 }

$lbasecamp = length($basecamp);
$lcamp2 = length($camp2);
$lsouthcol = length($southcol);
sleep(3500);
}

sub wwwfetch {
    local($URL) = @_;
    $URL =~ /http:\/\/([^\/]+)\/(.*)/;
    $hostport = $1; $path = $2;
    $resp = "";
    ($them, $port) = split(/:/, $hostport);
    $port = 80 unless $port;
    print("Connecting to $them on port $port to get $path\n");
    $AF_INET = 2;
    $SOCK_STREAM = 1;
    
    $sockaddr = 'S n a4 x8';
    
    chop($hostname = `hostname`);
    
    ($name,$aliases,$proto) = getprotobyname('tcp');
    ($name,$aliases,$port) = getservbyname($port,'tcp')
        unless $port =~ /^\d+$/;;
    ($name,$aliases,$type,$len,$thisaddr) =
        gethostbyname($hostname);
    ($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);
    
    $this = pack($sockaddr, $AF_INET, 0, $thisaddr);
    $that = pack($sockaddr, $AF_INET, $port, $thataddr);
    # Make the socket filehandle.
 
    if (socket(S, $AF_INET, $SOCK_STREAM, $proto)) { 
    }
    else {
        return(0);
    }
    
# Give the socket an address.
 
    if (bind(S, $this)) {
    }
    else {
        return(0);
    }
    
    select(S); $|=1;
    select(STDOUT);
    if (connect(S,$that)) {
        print(S "GET /$path perlWWW\n\n\n");
        while(<S>){
            $resp .= $_;
        }
        close(S);
        $resp;
    }
    else{
        return(0);
    }
}

sub report {
    $where = shift;
    $data = shift;
    @data = split("\n", $data);
    $cur = "";
    $x = $#data;
    while($cur !~ /\d/){
	$cur = $data[$x];
	$x--;
	print "Current conditions: $cur\n";
    }
    (@cur) = split(" ", $cur);
    (@cur2) = split(" ", substr($cur, index($cur, ")")+1));
    @cur = (@cur[0..5], @cur2);

    while(($cur[9] < 0) || ($cur[7] <-100)){
	$rep .= "Report from $where at $cur[3] contains errors, using older report\n";
	$repdata .= "Bad data from $where: $cur\n";
	$x--;
	$cur = $data[$x];
	(@cur) = split(" ", $cur);
	(@cur2) = split(" ", substr($cur, index($cur, ")")+1));
	@cur = (@cur[0..5], @cur2);
    }

    $rep .= "Weather report for $where at $cur[3] $cur[2] local Nepal time\n";
    $cel = int($cur[7]+0.5);
    $far = int(($cel*1.8)+32.5);
    if(int($cur[5]) == 0){
	$skies = "very bright";
    }
    elsif(int($cur[5]) == 1){
	$skies = "bright";
    }
    elsif(int($cur[5]) == 2){
	$skies = "dark";
    }
    elsif(int($cur[5]) == 3){
	$skies = "very dark";
    }

    $rep .= "Temperature: $cel C ($far F)\nBarometric Pressure: $cur[9] inches of mercury\n";
    $rep .= "Windspeed $cur[10]\nSkies are $skies\n";
    $rep .= "-------------------------------------------\n";
    $repdata .= "From $where: $cur\n";
}

sub sendreport {
    $them = "outgoing.mit.edu";
    $port = 25;
    $AF_INET = 2;
    $SOCK_STREAM = 1;
    
    $sockaddr = 'S n a4 x8';
    
    chop($hostname = `hostname`);
    
    ($name,$aliases,$proto) = getprotobyname('tcp');
    ($name,$aliases,$port) = getservbyname($port,'tcp')
        unless $port =~ /^\d+$/;;
    ($name,$aliases,$type,$len,$thisaddr) =
        gethostbyname($hostname);
    ($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);
    
    $this = pack($sockaddr, $AF_INET, 0, $thisaddr);
    $that = pack($sockaddr, $AF_INET, $port, $thataddr);
    # Make the socket filehandle.
 
    if (socket(S, $AF_INET, $SOCK_STREAM, $proto)) { 
    }
    else {
        return(0);
    }
    
# Give the socket an address.
 
    if (bind(S, $this)) {
    }
    else {
        return(0);
    }
    
    select(S); $|=1;
    select(STDOUT);
    if (connect(S,$that)) {
	print S "MAIL From: everest-weather@media.mit.edu\n";
	print S "RCPT To: everest-weather@media.mit.edu\n";
	print S "DATA\n";
	print S "From: Everest Weather Stations <everest-weather@media.mit.edu>\n";
	print S "To: everest-weather@media.mit.edu\n";
	print S "Subject: Weather report for $bct Nepal Time\n\n";
	print S "$rep";
	print S "\n\n$repdata\n";
	print S ".\n";
    }
    print $rep;
    print "\nRaw data\n$repdata\n";
}
