#!/usr/bin/perl

use CGI;

###################
# Configurable stuff

@sections = ('Overview', 'Generic', 'Unix', 'Win32');
@colors =   ('#CCFFFF',   '#CCCCFF', '#99CCFF', '#9999FF' );
$MAP = "/home/httpd/html/ssw/site.map";
$URL_root = "http://pandonia.canberra.edu.au/ssw";
$Document_root = '/ssw/';
$yellow = '#FFFF99';
$red = '#FFCFF99';

# end configurable stuff
########################

#######################
# MAP file format
#
# lecture-name | lectureURL & section | sectionURL : section | sectionURL ...
#
# There should be no spaces between words, unlike in the above
#######################


$query = new CGI;
print $query->header;

print $query->start_html(-title=>'System Software Lecture Map',
                                   -author=>'Jan Newmarch',
                                   -base=>'true',
                                   -meta=>{'keywords'=>'',
                                           'copyright'=>'copyright 1996 Jan Newmarch'},
                                   -BGCOLOR=>'white');


print "<h1 align=\"center\"> System Software Lecture Map </h1>\n";

print "This page is a map of the System Software lectures and alternative
routes through the pages. You can select any page and move to it in any
route.";

$lost = $query->param('ImLost');
if ($lost) {
    &setLocation;
}

open(MAP, $MAP) || die "Can't open site map";

&printHeader;

while (<MAP>) {
    chop;
    # head is "lecture-name | url" e.g "Introduction|intro"
    # body is "topic | url |category : topic | url | category..." 
    # e.g. "Overview|overview|overview:Bibliography|biblio|overview"
    ($head, $body) = split(/\&/);
    ($lectureName, $lectureURL) = split(/\|/, $head);
    @contents = split(/:/, $body);
    # print "title: $lectureName, contents: @contents\n";

    &printLecture;
}

print "\n</table>\n";

print $query->end_html;

####################
# end of HTML output
####################


sub printHeader {
    # Uses: @sections

    print "<table border=3 width=\"100%\" cellspacing=10 cellpadding=10>\n";
    print '<thead> <tr> <th bgcolor=FFCCFF colspan=2> Lectures';
    
    foreach $i (@sections) {
	print " <th bgcolor=FFCCFF> $i";
    }
    print "\n</thead>\n";
}

sub printLecture {

    &setLost;

    &printLectureName;

    # for each topic, print it next to the lecture
    foreach $i (@contents) {
	&printTopic($i);
    }
}

sub printLectureName {
    # print the lecture name spanning as many rows as needed
    # Uses: $yellow $contents $bgcolor $URL_root $lectureURL $lectureName

    # print $bgcolor;
    $size = @contents;
    print "\n<tr bgcolor=$yellow align=center>
   <td align=right rowspan=$size $bgcolor>
      <a href=\"$URL_root/$lectureURL/lecture.html\" target=\"_top\">$lectureName</a>
      ";
}

sub printTopic {
    # Uses: $lostInLecture, $URL_root, $lectureURL, $yellow

    # get params
    ($topicList) = @_;

    $n = 0;

    # contents is "topic | url | category"
    ($topic, $topicURL, $topicCategory) = split(/\|/, $topicList);
    
    if ($lostInLecture && $frameContents eq "$lectureURL/$topicURL") {
	$bgcolor = 'bgcolor=LightGreen';
    } else {
	$bgcolor = '';
    }
    
    # Setup to call frameset.pl with frame args
    # args is the lecture_list in frame list, 
    # and the topic in frame contents
    $args = "list=$URL_root/$lectureURL/lecture_list.html&contents=$URL_root/$lectureURL/$topicURL.html";
    print "<td $bgcolor> <a href=\"/cgi-bin/frameset.pl?$args\" target=\"_top\"> $topic </a>\n";
    
    &printTopicRest;

    print "\n\n<tr bgcolor=$yellow align=center>";
}

sub printTopicRest {
    # Uses: $topic, $topicURL, $topicCategory, $lectureURL, $URL_root
    # Uses: $lostInOther, $frameList

    # for each topic, see if it is one of the standard sections
    # and if so print it in the correct column
    $n = 0;
    foreach $match (@sections) {
       
	if ($topicCategory eq $match) {
	    
	    # print "fameList: $frameList, frameContents: $frameContents, topicURL: $topicURL";
	    if ($lostInOther) {
		# lose leading path from frameContents
		$frameContents =~ m/\/(.*)/;
		if ($topicURL eq $1) {
		    $bgcolor = "bgcolor=LightGreen";
		    # $n++;
		}
	    } else {
		$color = $colors[$n];
		$bgcolor = "bgcolor=$color";
	    }
	    
	    # args is the topics list in frame list and
	    # the topic in frame contents
	    # for e.g. Files + C/C++ the site.map entry is
	    # "C/C++ | c | Generic" and we want the URL to
	    # point to "generic". Hack the 3rd entry down to l.c.
	    $category = $topicCategory;
	    $category  =~ tr/A-]/a-z/;
	    $args = "list=$URL_root/$category\_list.html&contents=$URL_root/$lectureURL/$topicURL.html";
	    # should the name printed be $topic ro $lectureName?
	    print "<td $bgcolor> <a href=\"/cgi-bin/frameset.pl?$args\" target=\"_top\"> $topicCategory </a>";
	} else {
	    print '<td> ';
	}
	$n++;
    }
}

sub setLost {
    # Sets: lostInLecture, lostInOther, bgcolor
    # Uses: $lost, $frameList, $contents, $lectureURL

    $lostInLecture = 0;
    $lostInOther = 0;
    if ($lost && $frameList eq "$lectureURL/lecture") {
	$bgcolor = 'bgcolor=LightGreen';
	$lostInLecture = 1;
	# print "<td>LOST IN LECTURE $frameList $frameContents $bgcolor";
    } elsif ($lost && $frameList =~ m/^\w*$/ && $frameContents =~ m/$lectureURL/) {
	# we aren't in a lecture if the frameList has only chars (no /)
	$bgcolor = 'bgcolor=LightGreen';
	$lostInOther = 1;
	# print "<td>LOST IN OTHER $frameList $frameContents $bgcolor";
    } else {
	$bgcolor = '';
    }
}

sub setLocation {

    $contentsURL = $query->param('contentsURL');
    $listURL = $query->param('listURL');
    # print "documentURL: $contentsURL, list: $listURL";

    # print "documentURL: $contentsURL, $indexURL";

    print "<p> You have asked for location information.
           Your location will be shown in green.\n";

    if (! $contentsURL) {
	print "Can't locate you, script called with insufficient information:
sswmap.pl?contentsURL=...&listURL=...";
	$lost = 0;
	return;
    }

    $contentsURL =~ m/$Document_root(.*).html/;
    $frameContents = $1;
    $listURL =~ m/$Document_root(.*)_list.html/;
    $frameList = $1;
}    
