#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
use Data::Dumper;

my $code="KSPI";
my $zip="62629";

print "Content-type: text/html\n\n";

my $xml=get("http://weather.gov/data/current_obs/$code.xml")
#my $xml=`cat ~/dev/$code.xml`
    or die "failed to get XML from weather.gov\n";
my $fcast=get("http://www.weather.com/weather/print/$zip")
    or die "failed to get forecast from weather.com";
    
sub xml_content {
    my($tag)=@_;
    $xml =~ /<$tag>(.*?)<\/$tag>/;
    return $1;
}

sub fcast_content {
    $fcast =~ /(<TABLE WIDTH=100% BORDER=0 CELLPADDING=0 CELLSPACING=0  ID=f2>.+?<\/TABLE>)/ms
        or die "failed to match in weather.com html";
    return $1;
}

my($temp,$cond,$hum,$wind,$pressure)=(
    xml_content("temperature_string"),
    xml_content("weather"),
    xml_content("relative_humidity"),
    xml_content("wind_string"),
    xml_content("pressure_string")
);

print "<html>
<head>
<title>weather</title>
<style type=\"text/css\">
<!--
body {
    font-family: verdana;
    font-size: 11px;
}
img.map {

}
td { 
    vertical-align: top;
    font-family: verdana;
    font-size: 11px;
}
#left {
    position: relative;
    float: left;
    clear: left;
    width: 400px;
    border: 1px solid black;
}
#right {
    position: relative;
    width: 610px;
}
-->
</style>
</head>
<body>
<div id=\"left\">
 <p>
  Temp: $temp<br />
  Cond: $cond<br />
  Humidity: $hum<br />
  Wind: $wind<br />
  Pressure: $pressure<br />
  ".fcast_content."
 </p>
</div>
<div id=\"right\">
 <p>
  <img src=\"http://image.weather.com/images/maps/current/curwx_600x405.jpg\" class=\"map\" alt=\"map\" />
  <br />
  <img src=\"http://image.weather.com/images/maps/current/acttemp_600x405.jpg\" class=\"map\" alt=\"map\" />
 </p>
</div>
</body>
</html>";

