#!/usr/bin/perl

## Modify below to the absolute path of your input file:
open(INPUT, "/hsphere/local/home/ssscc/ssscc.org/registered.bout") or Error("Can't open input: $!");

## Here we pull in the data and build a simple structure, we'll sort
## it later during output:

while(<INPUT>) {
  @line = split(/\|/);

  my ($class, $scca, $first, $last, $make, $model, $year, $number);

  $class = (defined($line[1]) ? "\U$line[1]" : ""); # force uppercase
  $scca = (defined($line[5]) ? "\U$line[5]" : ""); # force uppercase
  $first = (defined($line[6]) ? $line[6] : "");
  $last = (defined($line[7]) ? $line[7] : "");
  $make = (defined($line[2]) ? $line[2] : "");
  $model = (defined($line[3]) ? $line[3] : "");
  $year = (defined($line[4]) ? $line[4] : "");
  $number = (defined($line[0]) ? $line[0] : "");

  $entries{"$class"}{"$number"} = "<tr><td>$class</td><td>$scca</td><td>$first $last</td><td>$number</td><td>$year $make $model</td></tr>\n";

  # make it easy to get a unique sorted list of classes:
  $classes{"$class"} = "$class";

}

close(INPUT);

##Print out the starting html stuff:

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

print "<html><head><title>Entry list</title></head><body>\n\n";
print  "<table border='1' width='90%'>\n";

## Now, we'll sort them and spit them out:

print "<tr><th>SSSCC Class</th><th>SCCA Class</th><th>Name</th><th>Number</th><th>Car</th></tr>\n";

foreach $class (sort(keys(%classes))) {
  print "<tr><td colspan='5'><b>$class</b></td></tr>\n";
  $entryref = $entries{"$class"};
  foreach $key (sort(keys(%{$entryref}))) { # entries are sorted by car number
    print $$entryref{$key};
  }
}

print "</table>\n\n";
print "</body></html>\n";

exit(0);			# That's it!

sub Error {
  my (@msg) = shift;
  print "Content-type: text/html\n\n";
  print "<html><head><title>Error!</title></head><body>\n\n";
  print "Uh oh, something went wrong.  The following might be a clue:<p>\n";
  print join("<br>", @msg);
  print "<br><br>\n";
  print "PATH: $ENV{'PATH'}<br>\n";
  print "PWD: " . `pwd`;
  print "</body></html>";
  die(@msg);
}




