#!/usr/bin/perl -w
use strict;

use constant ERROR => 0;
use constant NEWTEST => 1;
use constant TEST => 2;
use constant RESULT => 3;

my @data;

my $mode = ERROR;

open(FILE, '<', 'tests.dat') or die;
while (<FILE>) {
    chomp;
    if (s/^Test: //) {
        $mode = NEWTEST;
    } elsif (s/^Result: //) {
        die unless $mode == NEWTEST or $mode == TEST;
        $mode = RESULT;
    } elsif ($mode == ERROR and m/^\s*$/) {
        next;
    }
    if ($mode == NEWTEST) {
        push(@data, { test => $_, result => '(demo - no expected result)' });
        $mode = TEST;
    } elsif ($mode == TEST) {
        $data[$#data]->{test} .= "\n$_";
    } elsif ($mode == RESULT) {
        $data[$#data]->{result} = "$_";
        $mode = ERROR;
    } else {
        die;
    }
}
close(FILE);

my $count = 0;
foreach (@data) {
    ++$count;
    my $test = $_->{test};
    my $escapedTest = $test;
    $escapedTest =~ s/&/&amp;/gos;
    $escapedTest =~ s/</&lt;/gos;
    my $expected = $_->{result};
    open(FILE, '>', sprintf('%03d.html', $count)) or die;
    print FILE "$test\n<p>Test:\n<pre>$escapedTest</pre>\n<p>Expected result: <span id=\"expected\">$expected</span>\n<!--#include virtual=\"detector.inc\"-->\n";
    close FILE;
}

open(FILE, '>', 'max.inc') or die;
print FILE $count;
close FILE;
