#!/usr/bin/perl

use Cwd;
use CGI::FastTemplate;

my $boards;
my $calls;

my $boards_dir = "./data/boards/";
my $calls_dir = "./data/calls/";

opendir DIR, $boards_dir;
my @bfiles = readdir DIR;
foreach $file (@bfiles) {
    if ( $file ne "." && $file ne ".." ) {
        $boards .= "\t\t" . "<tr>\n";
        $boards .= "\t\t\t" . "<td> $file </td>\n";
        $boards .= "\t\t\t" . "<td> <a href=\"./bingoBoard.pl?din=$file\">Board</a> </td>\n";
        $boards .= "\t\t\t" . "<td> <a href=\"./read.pl?din=./data/boards/$file\">download</a> </td>\n";
        $boards .= "\t\t" . "</tr>\n";
    }
}

if ( $boards eq "" ) {
    $boards = " ";
} else {
    $boards = "<table cellpadding=4 border=1> <tr> <td> File </td> <td> Board </td> <td> Download </td> </tr>" .
             $boards . "</table>";
}

opendir DIR, $calls_dir;
my @cfiles = readdir DIR;
foreach $file (@cfiles) {
    if ( $file ne "." && $file ne ".." ) {
        $calls .= "\t\t" . "<tr>\n";
        $calls .= "\t\t\t" . "<td> $file </td>\n";
        $calls .= "\t\t\t" . "<td> <a href=\"./callSheet.pl?din=$file\">Call Sheet</a> </td>\n";
        $calls .= "\t\t\t" . "<td> <a href=\"./read.pl?din=./data/calls/$file\">download</a> </td>\n";
        $calls .= "\t\t" . "</tr>\n";
    }
}

if ( $calls eq "" ) {
    $calls = " ";
} else {
    $calls = "<table cellpadding=4 border=1> <tr> <td> File </td> <td> Call Sheet </td> <td> Download </td> </tr>" .
             $calls . "</table>";
}

## create the output form the template
my $tpl = new CGI::FastTemplate();
   $tpl->set_root("./");
   $tpl->define( main   => "/templates/index.template" );

my %defaults = (
                   BOARDS => $boards,
                   CALL_SHEETS => $calls
                );
   $tpl->assign(%defaults);
   $tpl->parse(CONTENT  => "main");
   print "Content-type: text/html" . "\n\n";
   $tpl->print("CONTENT");

exit 0;
