#! /s/std/bin/perl -wT

use lib '/s/perl/i386_cent5/lib/site_perl/5.8.8';
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use HTML::Template;
use Switch;

# Some initializations
my $query = new CGI;
my $resource = $query->param('r');
if (!$resource) {
   $resource = "lectureMain";
}
my $template = HTML::Template->new(filename => '/u/c/s/cs302/public/html/index.tmpl', 
				   die_on_bad_params => 0,
				   global_vars => 1);
$template->param('menu' => fileToString("menu.html"));
my $contentFile;

# Find where we're going
switch ($resource) {
   case "courseMain" {
      $contentFile = "/u/c/s/cs302/public/html/main.html";
   }

   case "lecture1" {
      $contentFile = "/u/c/s/cs302-1/public/html/main.html";
   }

   case "lecture2" {
      $contentFile = "/u/c/s/cs302-2/public/html/main.html";
   }

   case "lecture3" {
      $contentFile = "/u/c/s/cs302-3/public/html/main.html";
   }

   case "lecture4" {
      $contentFile = "/u/c/s/cs302-4/public/html/main.html";
   }

   case "lectureMain" {
      $contentFile = "main.html";
   }

   case "styleGuide" {
      $contentFile = "/u/c/s/cs302/public/html/resources/guides/style.html";
   }

   case "commentingGuide" {
      $contentFile = "/u/c/s/cs302/public/html/resources/guides/commenting.html";
   }

   case "policy" {
      $contentFile = "/u/c/s/cs302/public/html/policy/cs302_policy.pdf";
   }

   case "syllabus" {
      $contentFile = "/u/c/s/cs302/public/html/syllabus.html";
   }

   case "coursework" {
      $contentFile = "/u/c/s/cs302/public/html/coursework.html";
   }

   case "examTopics" {
      $contentFile = "/u/c/s/cs302/public/html/examTopics.html";
   }

   case "text" {
      $contentFile = "/u/c/s/cs302/public/html/resources/text.html";
   }

   case "computerLabs" {
      $contentFile = "/u/c/s/cs302/public/html/resources/labs.html";
   }

   case "atHome" {
      $contentFile = "/u/c/s/cs302/public/html/resources/atHome.html";
   }

   case "myPLab" {
      $contentFile = "/u/c/s/cs302/public/html/coursework.html#myPLab";
   }

#   case "CodeLab" {
#      $contentFile = "/u/c/s/cs302/public/html/resources/codeLab.html";
#   }

   case "wi_idea" {
      $contentFile = "/u/c/s/cs302/public/html/resources/wi_idea_intro.html";
   }

   case "visualizer" {
      $contentFile = "/u/c/s/cs302/public/html/resources/visualizer.html";
   }

   case "piazza" {
      $contentFile = "/u/c/s/cs302/public/html/resources/piazza.html";
   }

   case "why" {
      $contentFile = "/u/c/s/cs302/public/html/resources/whyCS.html";
   }

   case "allPrograms" {
      $contentFile = "/u/c/s/cs302/public/html/programs/all_programs.html";
   }

   case "programs" {
      my ($programNumber) = $query->param('programNumber') =~ /^(\d+)$/;
      if (-d "/u/c/s/cs302/public/html/programs/p${programNumber}/") {
	 $contentFile = "/u/c/s/cs302/public/html/programs/p${programNumber}/p${programNumber}.html";
      } else {
   	 if ($query->param('programNumber') ne "") {
   	    $template->param('error_message' => "Invalid program number. You were taken to the course work page.");
	    $contentFile = "/u/c/s/cs302/public/html/coursework.html";
   	 }
      }
   }

   case "labs" {
      my ($labNumber) = $query->param('labNumber') =~ /^(\d+)$/;
      my $labFilename = "lab" . sprintf("%02d", $labNumber);
      if (-d "/u/c/s/cs302/public/html/labs/${labDir}/") {
	 $contentFile = "/u/c/s/cs302/public/html/labs/${labFilename}/${labFilename}.html";
      } else {
   	 if ($query->param('labNumber') ne "") {
   	    $template->param('error_message' => "Invalid lab number. You were taken to the course work page.");
   	 }
	 $contentFile = "/u/c/s/cs302/public/html/coursework.html";
      }
   }

   else {
      $template->param('error_message' => "Invalid page. You were taken to the main page.");
      $contentFile = "main.html",
   }
}

# Now I'll need to parse the file for the title, top contents, and bottom
#   contents. Afterwards, we can dump the three properties altogether and
#   just have one
open CONTENT, $contentFile;
my $toParse = "";
while (<CONTENT>) {
   # Get next line
   $toParse = $toParse . $_;
   
   # Now see if we can parse more tags
   while (my ($templateVar, $content, $remainder) = $toParse =~ /<content\s*for=\"(.*?)\">(.*?)<\/content>(.*)/s) {
      $template->param("$templateVar" => $content);
      $toParse = $remainder;
   }
}
close CONTENT;

# Look at the last modified date/time
my @stat = stat $contentFile;
my @lastModified = localtime($stat[9]);
my $lastModified = ($lastModified[4] + 1) . "/" . $lastModified[3] . "/" . ($lastModified[5] + 1900);

# Now we have enough information to fill the template
$template->param('lastModified' => $lastModified);

# And produce the output
print "Content-Type: text/html\n\n" . $template->output;




# Turns a file into a string. Returns an error string if the file cannot be
#   found.
# @_ = <filename>
sub fileToString {
   my $filename = shift;
   my $string = "";
   if (-e $filename) {
      open FILE, $filename;
      while (<FILE>) {
	 $string = $string . $_;
      }
   } else {
      $string = "<div class=\"error\">The resource you requested is unavailable. This may be because the pages are still under construction. Please check back later. If you keep getting this message, contact your instructor.</div>";
   }
   return $string;
}
