#!/usr/bin/perl

#####
# carts - Display cartridge lists
# This source code is Copyright 1998 Lee K. Seitz.
# All rights reserved.
# Written by:  Lee K. Seitz
# Written on:  29 Apr 1998
# Modified:    29 Oct 1998 - Moved key to separate file.  Slightly
#                            modified header tags.
#
# To do:
#####

###
# Included Libraries *************************************************
###
require "html32.pl";

###
# Global Variables ***************************************************
###
$GBaseHttp  = "http://home.hiwaay.net/~lkseitz/cvg/";
$GDataDir   = "/data/users/l/lkseitz/public_html/cvg/";
$GDataFile = $GDataDir . $ENV{'QUERY_STRING'} . ".csv";

###
# Local Variables
###
local($System, $Title, $Headers, $Item);
local(@List, @temp);

###
# Begin *************************************************************
###

if (-r $GDataFile) {
   # Load file.
   @List = &ReadFile($GDataFile);

   if (@List) {
      # Print header.
      if    ($ENV{'QUERY_STRING'} eq "2600")         {$System="Atari 2600";}
#      elsif ($ENV{'QUERY_STRING'} eq "5200")         {$System="Atari 5200";}
      elsif ($ENV{'QUERY_STRING'} eq "7800")         {$System="Atari 7800";}
      elsif ($ENV{'QUERY_STRING'} eq "Colecovision") {$System="Colecovision";}
#      elsif ($ENV{'QUERY_STRING'} eq "Gameboy")    {$System="Nintendo Gameboy";}
      elsif ($ENV{'QUERY_STRING'} eq "Intellivision") {$System="Mattel Intellivision";}
#      elsif ($ENV{'QUERY_STRING'} eq "Jaguar")       {$System="Atari Jaguar";}
#      elsif ($ENV{'QUERY_STRING'} eq "JagCD")       {$System="Atari Jaguar CD";}
      elsif ($ENV{'QUERY_STRING'} eq "NES") {$System="Nintendo Entertainment System";}
#      elsif ($ENV{'QUERY_STRING'} eq "SNES")         {$System="Super Nintendo";}
#      elsif ($ENV{'QUERY_STRING'} eq "Vectrex")      {$System="GCE Vectrex";}
      else { &Error() };  # Unknown system.

      $Title = "Lee's $System Games";
      print &HtmlHead( $Title, $Title, "#FFFFFF", "#000000" );

      print &ReadFile( $GDataDir . "carts.htm" );

      # Process list.
      if ($ENV{'QUERY_STRING'} eq "2600" ||
          $ENV{'QUERY_STRING'} eq "7800" ||
          $ENV{'QUERY_STRING'} eq "Colecovision" ||
          $ENV{'QUERY_STRING'} eq "NES") {
         # Remove duplicates & FT column.
         grep( do {
                  @temp = split(/,/);
                  $Item = pop(@temp); pop(@temp); pop(@temp); push(@temp, $Item);
                  $_ = join(',', @temp);
               }, @List );
      }
      else {
         # Remove duplicates column.
         grep( do {
                  @temp = split(/,/);
                  $Item = pop(@temp); pop(@temp); push(@temp, $Item);
                  $_ = join(',', @temp);
               }, @List );
      }

      grep(s/"//g, @List);             # Remove quotation marks.
      grep(s/, *,/,&nbsp;,/g, @List);  # Put spaces in blank cells.
      grep(s/, *,/,&nbsp;,/g, @List);  # Do it again (trust me)
      grep(s/,$/,&nbsp;/, @List);      # Put spaces in blank end cell.
#grep( do { print; print "<BR>"; }, @List );
      grep(s/^/<TR ALIGN="CENTER"><TD ALIGN="LEFT">/, @List); # Begin row.
      grep(s/$/<\/TD><\/TR>/, @List);  # End row.
      grep(s/,/<\/TD><TD>/g, @List);   # Separate entries. 

      $Headers = shift(@List); 
      $Headers =~ s/<TD ALIGN="LEFT">/<TH>/;
      $Headers =~ s/TD>/TH>/g;       # Change entries to headers.

      # Start table.
      print "<TABLE BORDER=1 CELLSPACING=2>\n";
      print $Headers, "\n";
      foreach $Item (@List) {
         print $Item, "\n";
      }
      print "</TABLE>\n";

      # Wrap it up.
      &Tail( $GDataFile );
   }
   else {
      # File is empty.
      &Error();
   }
}
else {
   # File doesn't exist or not readable.
   &Error();
}

######################################################################
# Subroutines                                                        #
######################################################################

###
# ReadFile
# Written by:  Lee K. Seitz
# Written on:  Fri 16 May 1997
# Modified:    Wed 11 Mar 1998 (was IncludeVirtual in html32.pl)
# Description:  This routine returns the text of the filename (with
#    complete path) passed in as an array.  Each line is one element
#    of the array.
# Parameters:
#    File - The text file to return.
# Return Value:
#    [Contents of file specified.]
# Examples:
#    1. @file = &ReadFile("/home/username/public_html/disclaim.htm");
#    2. print &ReadFile($File);
###
sub ReadFile {
   local ($File) = shift(@_);
   local (@rawfile);

   open(FILE, "<$File");
   @rawfile = <FILE>;
   close(FILE);

   return @rawfile;
}

sub Error {
   print &HtmlHead( "Lee's Cartridge List", "Lee's Cartridge List",
                    "#FFFFFF", "#000000" );
   print &HtmlParagraph("File not found or unable to read file.");
   &Tail();
   exit(1);
}

sub Tail {
   local($file) = @_[0];
   local($tmp, $mtime, $mon, $day, $year);
   local(%Mons) = (0, "Jan", 1, "Feb", 2, "Mar", 3, "Apr",
                   4, "May", 5, "Jun", 6, "Jul", 7, "Aug",
                   8, "Sep", 9, "Oct", 10, "Nov", 11, "Dec");


   if ($file) {
      ($tmp, $tmp, $tmp, $tmp, $tmp, $tmp, $tmp, $tmp, $tmp, $mtime) = 
         stat($file);
      ($tmp, $tmp, $tmp, $day, $mon, $year) = localtime($mtime);
   }

   print "<HR>\n\n";

   print "Return to\n",
         "<A HREF=\"$GBaseHttp\">Lee's classic video game info</A>.\n",
         "<BR>\n",
         "<ADDRESS><A HREF=\"mailto:lkseitz\@hiwaay.net\">lkseitz\@hiwaay.net</A>";

   if ($file) {
      print "<BR>\nLast Modified:  $day $Mons{$mon} 19$year</ADDRESS>\n";
   }
   else {
      print "</ADDRESS>\n";
   }
   
   print "</BODY>\n</HTML>\n";
}
