#!/usr/bin/perl
#
# Get Engine Date
# Copyright (C) 2004 Mike Skyner
#

# get the form data from STDIN
read(STDIN, $input_string, $ENV{'CONTENT_LENGTH'});


print "Content-type: text/html\n\n";

print qq~
      <html><head>
      <title>Engine Database</title></head>
      <body bgcolor=#FFFF99><center><font size=+7 color=#000099>Engine Database</font></center>
      <hr width=100% align=center>
      <table width=100% border=1 align=center>

~;

# Open the Engine Date Database and read it into memory
open(FP, "<../enginedate.txt");

$number = 0;

while(!eof(FP))
{
   $data_array[$number++] = <FP>;
}

close(FP);

# Get the field headers from the first record;
# Split the data into an array at the &

@array = split(/&/, $data_array[0]);

# Print the title fields

print qq~
      <tr bgcolor=#CCCCCC>
      <td><center>No.</center></td>
~;

for($cnt = 0; $cnt < (scalar(@array)); $cnt++)
{
   # Change the + for spaces
   $array[$cnt] =~ s/\+/ /g;
   
   # Split again at the =
   @array1 = split(/=/, $array[$cnt]);
   print "<td><center>", $array1[0], "</center></td>";
}

print "</tr>";

# Print the main body of the table

$colour_count = 0;
$loop_count = 0;
$line_count = 1;

while($loop_count < $number)
{
   #Read in the line of data and split it into an array
   # at the field marks

   @array = split(/&/, $data_array[$loop_count++]);
   
   # Print each line with a different colour background
   # In this case Blue and Gray

   if(($colour_count++ % 2) == 0)
   {
      $colour = "#33CCFF";
   }

   else
   {
      $colour= "#CCCCCC";
   }

   print "<tr bgcolor=$colour>";

   # Print the Index Number

   print "<td><center>", $line_count++, "</center></td>";

   for($cnt = 0; $cnt < (scalar(@array)); $cnt++)
   {
      # Change the + for spaces 
      $array[$cnt] =~ s/\+/ /g;

      # Split each field at the = in to its 2 parts
      @array1 = split(/=/, $array[$cnt]);

      # Replace NULL fields with a . and change the colours
      # to make the table format look OK
      
      $array1[1] =~ s/(%[0-f][0-f])/conv($1)/ge;

      if(length($array1[1]) == 0)
      {
         $array1[1] = ".";
         print "<td><center><font color=$colour><center>", $array1[1], "</center><font color=#00099></td>";
      }
      
      else
      {
         print "<td><center>", $array1[1], "</center></td>";
      } 
   }

   print "</tr>";
}

print qq~
      </table>
      <hr width=100% align=center>
      <a href="index.html"><img src="../../../icons/back.gif" border=0></a>
      </body></html>
~;

sub conv
{
   @value = split(/%/, $_[0]);
   return chr hex $value[1];
}