#!/usr/bin/perl -w
#
# This is purely used to generate a page on the website; it's not intended
# to be distributed or installed.

use HTML::Entities;

my $tmpl = q{<tr __BGCOLOR__><td><strong>__TYPE__</strong></td>
<td><font size=-1>__LANG__</font></td>
<td><font size=-1>__DESCRIPTION__</font></td>
<td><font size=-1>__NAME__</font></td><td>__SCORE__</td></tr>};

my %test = ();
my %type = ();
my %lang = ();
my %desc = ();
while (<>) {
  s/(?<!\\)#.*$//; # remove comments
  s/^\s+//; s/\s+$//;

  my $lang ='';
  if (s/^lang\s+(\S+)\s+//) { $lang = $1; }

  if (/^(header|body|rawbody|uri|full)\s+(\S+)\s+(.+)$/) {
    if (!defined $test{$2}) {
      push (@testnames, $2);
      $type{$2} = $1;
      $lang{$2} = $lang;
    }
    $test{$2} = $3;

  } elsif (/^describe\s+(\S+)\s+(.+)$/) {

    # no locale; overrides any other desc
    if ($lang eq '') {
      $desc{$1} = $2;
    }

    # uses the test's "native" locale
    if (defined ($lang) && defined($lang{$1}) && $lang{$1} eq $lang) {
      if (!defined $desc{$1}) { $desc{$1} = $2; }
    }

  } elsif (/^score\s+(\S+)\s+(.+)$/) {
    $score{$1} = $2;
  }
}

my $n = 0;
foreach my $t (@testnames) {
  next if ($t =~ /^__/);
  next if ($t =~ /^T_/);

  $n++;
  $_ = $tmpl;
  my $desc = $desc{$t};
  if (!defined $desc) { $desc = $test{$t}; }

  $desc =~ s,\|,| ,gs;		# allow line breaks on pipes
  $desc =~ s,\$,(dollar) ,gs;
  $desc = HTML::Entities::encode ($desc);

  my $bgcolor = '';
  if ($n % 2 == 0) { $bgcolor = ' bgcolor=#ccffcc'; }

  s/__BGCOLOR__/$bgcolor/gs;
  s/__NAME__/$t/gs;
  s/__DESCRIPTION__/$desc/gs;
  s/__TYPE__/$type{$t}/gs;
  s/__LANG__/$lang{$t}/gs;
  s/__SCORE__/$score{$t} or 1/ges;
  print;
}

