#!/usr/local/bin/perl
#
# showPage.cgi
#
# Prints a web page for a particular What Jail Is Like show.
#
# Reads in the show date as a piece of the URL.
# Imports the information for that show from file showSettings.inc.
# Merges that show info into an HTML template (show_template.html).
#
# Uses library cgi-lib.pl (non-object-oriented) and package
# Perlfect::Template.perl (object-oriented and renamed PerlfectTemplate.pm).
# Both files exist in the current directory.
# This script (showPage.cgi) is an opportunistic mash which could be more elegant.
# Should figure out convenient place to store scripts, and how to access them
# (append dir path to @INC)?
#
# Kurt Tuohy: May 2001

# Use package to handle HTML templates
use PerlfectTemplate;

# Set constants
my $baseShowDir = "radio";
my $showInfoFile = "showSettings.inc";
my $showTemplateFile = "show_template.html";

my $showDirKey = "show";   # Key for HTML-form input of show-directory name

# Get HTML-form input. Should just be the name of the directory containing the
# desired radio show.
# my %inputParams = {};
require "cgi-lib.pl";
&ReadParse(*inputParams);
my $showSubDir = $inputParams{$showDirKey};

# Get info particular to the desired show.
my $errorMsg;
my $showSettings = require "$showSubDir/$showInfoFile";
my %showSettings = %$showSettings;   # Dereference the wanker -- otherwise trouble!

# Convert variable-length list of show segments to displayable HTML.
$showSettings{"segments"} = &convertShowSegsToHTML(@{$showSettings{"segments"}});
# Read HTML template for show.
my $showTemplate = new PerlfectTemplate("$showTemplateFile");
# Merge show particulars into template.
my $showPage = $showTemplate->cast(\%showSettings);

# Print merged HTML.
print &PrintHeader;
print $showPage;

################

# The show segment list is an array of hashes.
# Parse it and wrap it in the appropriate HTML tags.
# Messy -- but the Perlfect::Template package can't handle lists on its own.
#
sub convertShowSegsToHTML
{
	my(@showSegs) = @_;
	my($showSeg, $showSegStr);

	foreach $showSeg (@showSegs)
	{
		$showSegStr .= "<TR>\n" .
			"<TD CLASS=\"contentNameCell\">" . $showSeg->{"segTitle"} . "</TD>\n" .
			"<TD CLASS=\"segLinkCell\"><A HREF=\"" . &convertFileToURL($showSeg->{"segListen"}) . "\">Listen</A></TD>\n" .
			"<TD CLASS=\"segLinkCell\"><A HREF=\"" . &convertFileToURL($showSeg->{"segDownload"}) . "\">Download</A></TD>\n" .
			"</TR>\n"
	}
	return $showSegStr;
}

# Turn a filename into a link, minus the "A" tag.
#
sub convertFileToURL
{
	my($fileName) = @_;
	my $baseURL = "http://www.whatjailislike.com/$baseShowDir/$showSubDir";
	return "$baseURL/$fileName";
}