#!/usr/bin/perl

require "../cgi-lib.pl";
ReadParse(*in);
*params = *in;
use LWP;
if($ENV{HTTP_USER_AGENT}) { $web = 1;} else { $web = 0; }
print "Content-type: text/html\n\n" if($web);
print "<HTML><BODY BACKGROUND=#FFFFFF>\n";

%rtdesc = (
	   '3Na' => "Rte. 3 North (Rte. 128/I-95 to I-495)",
	   '3Nb' => "Rte. 3 North (I-495 to NH Line)",
	   '3Sa' => "Rte. 3 South (I-495 to Rte. 128/I-95)",
	   '3Sb' => "Rte. 3 South (NH Line to I-495)",

	   '495Na' => "I-495 North (Mass Pike/I-90 to Rte. 3)",
	   '495Nb' => "I-495 North (Rte. 3 to I-93)",
	   '495Nc' => "I-495 North (I-93 to I-95)",
	   '495Sa' => "I-495 South (Rte. 3 to Mass Pike/I-90)",
	   '495Sb' => "I-495 South (I-93 to Rte. 3)",
	   '495Sc' => "I-495 South (I-95 to I-93)",

	   '93Na' => "I-93 North (Central Artery to Rte. 128/I-95)",
	   '93Nb' => "I-93 North (Rte. 128/I-95 to I-495)",
	   '93Nc' => "I-93 North (I-495 to NH Line)",
	   '93Sa' => "I-93 South (Rte. 128/I-95 to Central Artery)",
	   '93Sb' => "I-93 South (I-495 to Rte. 128/I-95)",
	   '93Sc' => "I-93 South (NH Line to I-495)",

	   '128Na' => "Rte. 128/I-95 North (Mass Pike/I-90 to Rte. 3)",
	   '128Nb' => "Rte. 128/I-95 North (Rte. 3 to I-93)",
	   '128Nc' => "Rte. 128/I-95 North (I-93 to I-95)",
	   '128Nd' => "Rte. 128 North (I-95 to Gloucester)",
	   '128Sa' => "Rte. 128/I-95 South (Rte. 3 to Mass Pike/I-90)",
	   '128Sb' => "Rte. 128/I-95 South (I-93 to Rte. 3)",
	   '128Sc' => "Rte. 128/I-95 South (I-95 to I-93)",
	   '128Sd' => "Rte. 128 South (Gloucester to I- 95)", #extra space!

	   '95Na' => "I-95 North (Rte. 128 to NH Line)",
	   '95Sa' => "I-95 South (NH Line to Rte. 128)",

	   '90Ea' => "Mass Pike/I-90 East (I-290/I-395 to I-495)",
	   '90Eb' => "Mass Pike/I-90 East (I-495 to the Weston Tolls)",
	   '90Ec' => "Mass Pike/I-90 East (Weston Tolls to Allston/Brighton Tolls)",
	   '90Ed' => "Mass Pike/I-90 East (Allston/Brighton Tolls to SE Expressway)",
	   '90Wa' => "Mass Pike/I-90 West (I-495 to I-290/I-395)",
	   '90Wb' => "Mass Pike/I-90 West (Weston Tolls to I-495)",
	   '90Wc' => "Mass Pike/I-90 West (Allston/Brighton Tolls to the Weston Tolls)",
	   '90Wd' => "Mass Pike/I-90 West (Southeast Expressway/I-93 to Allston/Brighton Tolls)",

	   '9300Nd' => "Southeast Expressway/Rte. 3/I-93 North",
	   '9300Sd' => "Southeast Expressway/Rte. 3/I-93 South",

	   '300Nc' => "Rte. 3 North (Sagamore Bridge to Braintree)",
	   '300Sc' => "Rte. 3 South (Braintree to Sagamore Bridge)",

	   '49500Nd' => "I-495 North (I-95 to Mass Pike/I-90)",
	   '49500Ne' => "I-495 North (Bourne Bridge to I-95)",
	   '49500Sd' => "I-495 South (Mass Pike/I-90 to I-95)",
	   '49500Se' => "I-495 South (I-95 to Bourne Bridge)",

	   '9500Nb' => "I-95 North (I-495 to Rte. 128/I-93)",
	   '9500Nc' => "I-95 North (Rhode Island to I-495)",
	   '9500Sb' => "I-95 South (Rte. 128/I-93 to I-495)",
	   '9500Sc' => "I-95 South (I-495 to Rhode Island)",

	   '12800Ne' => "Rte. 128/I-95 North (I-95 to Mass Pike/I-90)",
	   '12800Nf' => "Rte. 128 North (Southeast Expressway and I-95)",
	   '12800Se' => "Rte. 128/I-95 South (Mass Pike/I-90 to I-95)",
	   '12800Sf' => "Rte. 128 South (I-95 to Southeast Expressway)",

	   );

%rtpage = (
	   '3' => 'http://www.smartraveler.com/scripts/bostraffic.asp?index=5&city=bos&cityname=Boston',
	   '495' => 'http://www.smartraveler.com/scripts/bostraffic.asp?index=9&city=bos&cityname=Boston',
	   '93' => 'http://www.smartraveler.com/scripts/bostraffic.asp?index=7&city=bos&cityname=Boston',
	   '128' => 'http://www.smartraveler.com/scripts/bostraffic.asp?index=6&city=bos&cityname=Boston',
	   '90' => 'http://www.smartraveler.com/scripts/bostraffic.asp?index=10&city=bos&cityname=Boston',
	   '95' => 'http://www.smartraveler.com/scripts/bostraffic.asp?index=8&city=bos&cityname=Boston',
	   '300' => 'http://www.smartraveler.com/scripts/bostraffic.asp?index=26&city=bos&cityname=Boston',
	   '49500' => 'http://www.smartraveler.com/scripts/bostraffic.asp?index=27&city=bos&cityname=Boston',
	   '9300' => 'http://www.smartraveler.com/scripts/bostraffic.asp?index=11&city=bos&cityname=Boston',
	   '12800' => 'http://www.smartraveler.com/scripts/bostraffic.asp?index=29&city=bos&cityname=Boston',
	   '9500' => 'http://www.smartraveler.com/scripts/bostraffic.asp?index=28&city=bos&cityname=Boston',
	   );

$ua = new LWP::UserAgent;
$ua->agent("Foo/0.1 ".$ua->agent);
$ua->timeout(60);

my(%pagedata) = ();
#print "About to load pages\n";
foreach $route (keys %rtdesc) {
    my($baseroute) = $route;
    $baseroute =~ s/[A-Za-z]+//g;
    $url = $rtpage{$baseroute};
    #print "I want to load $url\n";
    if(!defined($pagedata{$url}) || !$pagedata{$url}->is_success) {
	#print "Loading $baseroute ...";
	my($request) = new HTTP::Request GET => $url;
	$request->content_type('application/x-www-form-urlencoded');
	$request->content('match=www&errors=0');
	$pagedata{$url} = $ua->request($request);
	#print "Done!<br>\n";
    }
    my($response) = $pagedata{$url};
    if($response->is_success) {
	#CONDITION OF SUBROUTE
	if($response->content() =~ /\/(\w+)\.gif ALIGN=left VALIGN=bottom\>\<\/td\>\s*\<td\>\<b\>\Q$rtdesc{$route}\E\<\/B\>/i) {
	    $img = $1;
	    if($img =~ /ok/i) { $condition{$route} = 0; }
	    elsif($img =~ /slow/i) { $condition{$route} = 1; }
	    elsif($img =~ /delay/i) { $condition{$route} = 2; }
	    elsif($img =~ /alert/i) { $condition{$route} = 3; }
	} else {
	    print "Ack! $route had no associated gif!<br>\n";
	    $errflag++;
	}
    } else {
	print "Ack! Could not load $url<br>\n";
	$errflag++;
    }
}

if($errflag) { print "<font color=red size=+1><b>Warning!</b></font> SmartTraveler's website contained unexpected and/or incomplete data. All times are assumed to be normal. Encountered $errflag errors.<br><br>\n"; }

rmap();

print "</BODY></HTML>\n";

sub rmap {
    my(@rmap) = (
		 "-------AB-CD-EF-GH-",
		 "-IIIIII--J--EEF-GH-",
		 "-IKKKKK--L--FFF-GH-",
		 "-IK----MN-OP----GH-",
		 "-IK-QQQ--R--SSSS--w",
		 "-IK-QTT--U--VVVV--x",
		 "-IK-QT----WX-------",
		 "Y--Z--a--b---------",
		 "c--d--e--f---------",
		 "-gh-ij----kl-------",
		 "-gh---mmmm---------",
		 "-gh---nnnn---------",
		 "-gh-op----qr-------",
		 "-ghh--sss-qr-------",
		 "-ggg--tts-qr-------",
		 "----uv-ts-qr-------",
		 );
    my(%key) = (
		A => '3Sb',
		B => '3Nb',
		C => '93Sc',
		D => '93Nc',
		E => '495Sc',
		F => '495Nc',
		G => '95Sa',
		H => '95Na',
		I => '495Sa',
		J => '495Sb',
		K => '495Na',
		L => '495Nb',
		M => '3Sa',
		N => '3Na',
		O => '93Sb',
		P => '93Nb',
		Q => '128Sa',
		R => '128Sb',
		S => '128Sc',
		T => '128Na',
		U => '128Nb',
		V => '128Nc',
		W => '93Sa',
		X => '93Na',
		Y => '90Wa',
		Z => '90Wb',
		a => '90Wc',
		b => '90Wd',
		c => '90Ea',
		d => '90Eb',
		e => '90Ec',
		f => '90Ed',
		g => '49500Sd',
		h => '49500Nd',
		i => '12800Se',
		j => '12800Ne',
		k => '9300Sd',
		l => '9300Nd',
		m => '12800Nf',
		n => '12800Sf',
		o => '9500Sb',
		p => '9500Nb',
		q => '300Sc',
		r => '300Nc',
		s => '49500Ne',
		t => '49500Se',
		u => '9500Sc',
		v => '9500Nc',
		w => '128Sd',
		x => '128Nd',
		);
    @labels = ();
    $labels[2] = " valign=center align=right>3";
    $labels[3] = " valign=center align=right>93";
    $labels[4] = " valign=bottom align=left><b>H</b>";
    $labels[5] = " valign=center align=left>495";
    $labels[6] = " valign=bottom align=left>95";
    $labels[7] = " valign=bottom align=right>495";
    $labels[9] = " valign=bottom align=left>128/95";
    $labels[10] = " valign=bottom align=left><b>W</b>";
    $labels[12] = " valign=bottom align=left>128/95";
    $labels[20] = " valign=top align=center>128";
    $labels[18] = " valign=center align=left>93";
    $labels[32] = " valign=bottom align=left>3";
    $labels[31] = " valign=top align=left>128";
    $labels[38] = " valign=center align=left>495";
    $labels[29] = " valign=center align=right>95";
    $labels[16] = " valign=bottom align=right>90";

    $w = 3;
    $l = 50;
    $tall = 0;
    my(%color);
    foreach $k (keys %key) {
	$rt = $key{$k};
	if(defined $condition{$rt}) {
	    $color{$rt} = ("green","orange","red","blue")[$condition{$rt}];
	} else {
	    $color{$rt} = ($rt =~ /S|W/) ? "#AAAAAA" : "#CCCCCC";
	}
    }
    print "<table cellpadding=0 cellspacing=0 border=0>\n";
    foreach $line (@rmap) {
	print "<tr>\n";
	$wide = 0;
	if($tall % 3) { $x = $w; }
	else { $x = $l; }
	foreach $td (split //,$line) {
	    print "<td";
	    if($key{$td}) {
		print " bgcolor=$color{$key{$td}}";
	    }
	    if($wide % 3) { $y = $w; }
	    else { $y = $l; }
	    if($td eq "-" && $y == $l && $x == $l) {
		if($labels[$lnum]) { print $labels[$lnum]; }
#		else { print " align=center valign=center><b><font size=-1>$lnum</font></b>"; }
		else { print ">"; }
		$lnum++;
	    } else {
		print ">";
		limg($key{$td},$x,$y);
	    }
	    print "</td>\n";
	    $wide++;
	}
	print "</tr>\n";
	$tall++;
    }
    print "</table>\n";
}

sub limg {
    my($route,$height,$width) = @_;
    my($base) = $route;
    $base =~ s/\D//g;
    if($rtpage{$base}) { print "<a href=\"$rtpage{$base}\">"; }
    print "<img src=\"/~dave/images/spacer.gif\" border=0 width=$width height=$height>";
    if($rtpage{$base}) { print "</a>"; }
}









