#!/usr/bin/perl -w

use strict 'vars';

sub WritePlayer($);

%::gValidKeys = (
  'age'  => 'Age',
  'doom' => 'DOoM',
  'file' => 1,
  'hist' => 'History',
  'hobb' => 'Hobbies',
  'home' => 'Residence',
  'link' => 'Links',
  'mail' => 'E-Mail',
  'name' => 1,
  'page' => 1,
  'phot' => 1,
  'qual' => 'Qualified',
  'rtng' => 'Rating',
  'work' => 'Occupation',
);

my $lastkey = undef;
my $p = {};

while (<>) {
  s/\s*#.*// && /^$/ && next;
  if (/^(\w+)\s+(\S.*)/) {
    my $key   = "\L$1";
    my $value = $2;

    die "Bad key ($key) in: ${_}Aborting" unless defined $::gValidKeys{$key};
    $p->{$key} = $value;
    $lastkey = $key;
    }
  elsif (/^$/) {
    $lastkey = undef;
    next unless %$p;
    WritePlayer $p;
    $p = {};
    }
  elsif (s/^\s+//) {
    die "Bad continuation: ${_}Aborting" unless defined $lastkey;
    chomp;
    $p->{$lastkey} .= "\n$_";
    }
  else {
    die "Can't parse: ${_}Aborting";
    }
  }

WritePlayer $p if %$p;

sub WritePlayer { my($p) = @_;
  my ($name, $file);

  $name = $p->{'name'};
  ($file = $p->{'file'}) 
    || (($file = "\L$name") =~ s/.* //);

  die "Player has no name!\nAborting" unless defined $name;
  die "$name hasn't qualified!\nAborting" unless defined $p->{'qual'};
  die "Duplicate entry for $file\nAborting"
    if defined $::gPlayers{$file};
  die "${name}'s history doesn't end with a period.\nAborting"
    if (defined $p->{'hist'}) && $p->{'hist'} !~ /\.$/;
  $::gPlayers{$file} = $p;

  open(FILE, ">$file.new") || die "open($file.new) failed:$!\nAborting";

  print FILE
    "<html>\n".
    "<head>\n".
    "<title>$name</title>\n".
    "<link rev=made href=\"mailto:jjchew\@math.utoronto.ca\">\n".
    "</head>\n".
    "<body>\n".
    "<i><a href=\"..\">Back to WSC97</a></i>\n".
    "<table>\n";

  if (defined $p->{'phot'}) {
    print FILE "<tr><td rowspan=12>".
      "<img src=\"$p->{'phot'}\" alt=\"[photo]\" border=2>\n"; 
    }
  print FILE "<tr valign=top><td align=right><b>Name:</b><th align=left>$name\n";

  my $key;
  for $key ('qual', 'home', 'age', 'work', 'hist', 'hobb', 'mail', 'rtng') {
    print FILE "<tr valign=top><td align=right><b>$::gValidKeys{$key}:</b><td>$p->{$key}\n"
      if defined $p->{$key};
    }

  print FILE "<tr valign=top><td align=right><b>DOoM:</b><td><a href=\"".
    "http://eel.st.usm.edu:7777/look/$p->{'doom'}\">$p->{'doom'}</a>\n"
    if defined $p->{'doom'};
  print FILE "<tr valign=top><td align=right><b>Home Page:</b><td>".
    "<a href=\"$p->{'page'}\">$p->{'page'}</a>\n"
    if defined $p->{'page'};
  print FILE 
    "</table>\n";
  print FILE "<h2>Other links</h2>$p->{'link'}\n" if defined $p->{'link'};
  print FILE
    "<h2>Note</h2>See <a href=\"Notes.html\">legend</a> for an explanation of each field.\n".
    "</body>\n".
    "</html>\n";

  chmod 0644, "$file.new" || die "chmod(0644, $file.new) failed:$!\nAborting";
  close(FILE);
  rename "$file.new", "$file.html" 
    || die "rename $file.new $file.html failed:$!\nAborting";
  
  print "Created $file.html.\n";
  }
