#! /usr/bin/perl

# Construct a table of Champaign-Urbana MTD bus route/schedule info links,
# referring concisely to the primary pages for each route at
#    http://www.cumtd.com/
# Stuart Levy, slevy@ncsa.uiuc.edu, June 2006.
# This script is in the public domain.

print <<EOF;
<HEAD><TITLE>C-U MTD Schedules</TITLE></HEAD>
<BODY>
<BASE HREF="http://www.cumtd.com/routeschedules/">
Here is a table of links for all bus routes run by the
<A HREF="http://www.cumtd.com/">Champaign-Urbana MTD http://www.cumtd.com/</A>
<BR>
These are links to the always-current CUMTD page for each route, not to copies of their pages.
Effectively this is a table of shortcuts around the two layers of menus on CUMTD's
<A HREF="http://www.cumtd.com/routeschedules/Default.aspx"><I>Routes and Schedules</I></A> page.
 -- <small><I>Stuart Levy</I></small>
EOF

@coltags = ("WkDay/1", "WkDay/1", "WkEve/2", "WkLate/10", "WkCamp/6",
	    "SatDay/11",  "SatEve/4",  "SatLate/9", "SatCamp/7",
	    "SunDay/5",  "SunLate/12", "SunCamp/8");


while(<DATA>) {
   if(/(\S+)\s*=\s*(.+)/) {
	my($thing, $name) = ($1, $2);
	$title{$thing} = $name;
   } elsif(/(\S+)\s*->\s*(.+)/) {
	my($name, $id) = ($1, $2);
	push(@routes, $name);
	push(@routeids, $id);
   }
}

foreach $_ ( @coltags ) {
    ($name, $id) = split(m'/');
    push(@columns, $name);
    push(@columnids, $id);
}

print "<TABLE BORDER=1>\n";

$maxrow = 0;

for($cno = 0; $cno < @columns; $cno++) {
    $col = $columns[$cno];
    for($rno = 0; $rno < @routes; $rno++) {
	$route = $routes[$rno];
	if($cno == 0) {	# WkDay
	    push(@{$colent[$cno]}, $rno) if $route =~ /^([1-5])[A-Z]/;
	} elsif($cno == 1) { # also WkDay
	    push(@{$colent[$cno]}, $rno) if $route =~ /^([6-9]|10|13)[A-Z]|^15$|^24/;
	} elsif($route =~ /21QdLtd/) {			# oddball
	    push(@{$colent[$cno]}, $rno) if $col eq "WkCamp";
	} elsif($col =~ /Camp/) {
	    push(@{$colent[$cno]}, $rno) if $route =~ /^2[123567]/;
	} elsif($col =~ /WkEve/) {
	    push(@{$colent[$cno]}, $rno) if $route =~ /^([578]0|1\d0)/;
	} elsif($col =~ /Eve|Sat|Sun/) {
	    push(@{$colent[$cno]}, $rno) if $route =~ /^([2-9]0|1\d0)/;
	} elsif($col =~ /Late/) {
	    push(@{$colent[$cno]}, $rno) if $route =~ /^(50|100)/;
	}
    }
    $nrows = scalar @{$colent[$cno]};
    $maxrow = $nrows if $maxrow < $nrows;
}


print "<TR>\n";
for($cno = 0; $cno < @columns; $cno++) {
    print "  <TD>$columns[$cno]";
}
print "\n";

for($row = 0; $row < $maxrow; $row++) {
    print "<TR>\n";
    for($cno = 0; $cno < @columns; $cno++) {
	print "  <TD>";
	$rno = ${$colent[$cno]}[$row];
	if(defined($rno)) {
	    my($ti) = $title{$routes[$rno]};
	    unless($ti) {
		my($number) = ($routes[$rno] =~ /(\d+)/g);
		$ti = $title{$number};
	    }
	    if($ti) {
		$ti = " title=\"$ti\"";
	    }
	    print "<A HREF=\"ByRoute.aspx?routeID=$routeids[$rno]&routegroupID=$columnids[$cno]\"$ti>$routes[$rno]</A>";
	} else {
	    print "&nbsp;";
	}
	print "\n";
    }
}
print "</TABLE>\n";

# http://www.cumtd.com/routeschedules/ByRoute.aspx?routeID=48

__END__
1 = Yellow
2 = Red
3 = Lavender
4 = Blue
5 = Green
6 = Orange
7 = Grey
8E = Campus to Orchard Downs
8W = Orchard Downs to Campus
9 = Brown
10 = Gold
15 = Pink
27 = Air Bus

20 = Red
30 = Lavender
50 = Green
70 = Grey
80 = Bronze Campus/Orchard Downs
100 = Yellow
120 = Teal
130 = Silver
150 = Urbana Link
220 = Illini

1N -> 136
1S -> 137
2N -> 138
2S -> 139
3N -> 140
3S -> 141
4E -> 142
4W -> 143
5E -> 145
5W -> 146

5Wexp -> 148
5Eexp -> 149
6E -> 150
6W -> 151
7E -> 152
7W -> 154

8W -> 155
8E -> 156
9A -> 158
9B -> 159
10E -> 160
10W -> 161

12 -> 162
13 -> 163
14E -> 164
14W -> 165
15 -> 166
22 -> 266

27 -> 171
331NEdir -> 172
334Wdir -> 173

# weekday evening
50E -> 178">50 Green East</option>
50W -> 176">50 Green West</option>
70E -> 180">70 Grey East</option>
70W -> 181">70 Grey West</option>

100N -> 182">100 Yellow North</option>
100S -> 183">100 Yellow South</option>
120 -> 185">120 Teal</option>
130 -> 187">130 Silver</option>
150 -> 256">150 Pink</option>
220 -> 263">220 Illini</option>

331NEdir -> 234">331 Northeast Direct</option>
333SWdir -> 235">333 Southwest Direct</option>
335safe -> 236">335 SafeRides</option>

# weekday late night
50E -> 177
50W -> 179
100 -> 184
120 -> 186

130 -> 188
220 -> 265
335safe -> 238
335safe -> 260


20N -> 41
20S -> 42
30N -> 43
30S -> 44

50E -> 31
50W -> 30
70E -> 33
70W -> 32
80E -> 55
80W -> 34
100N -> 35
100S -> 36
130E -> 37
130W -> 56
150Link -> 38

21 -> 46
21QdLtd -> 47
22EngtoFP -> 52
22FPtoEng -> 48
23W -> 49
23E -> 50
26 -> 51
