#!/usr/bin/perl -w

use strict;


# First page of scorecards:
#<tr class="top1"><th class="number" colspan=3>#1</th><th class="name" colspan=5><span class="label">Name:</span> Whitmarsh, Bradley</th><th class="rating" colspan=2><span class="label">Rating:</span> 1849</th></tr>
#<tr class="top2"><th class="round">Round</th><th class="table">Board</th><th class="onum">Opp. #</th><th class="orat">Rating</th><th class="onam">Opponent Name</th><th class="won">Won</th><th class="lost">Lost</th><th class="for">Player<br>Score</th><th class="against">Opponent<br>Score</th><th class="spread">Spread</th></tr>

# Inner round, where we're looking to split:
#<tr class="round1"><td class="round">18</td><td class="table" rowspan=2>1</td><td class="onum" rowspan=2>10</td><td class="orat" rowspan=2>1426</td><td class="onam" rowspan=2>Berg, Verna Richards*2</td><td class="won" rowspan=2>&nbsp;</td><td class="lost" rowspan=2>&nbsp;</td><td class="for" rowspan=2>&nbsp;</td><td class="against">&nbsp;</td><td class="spread">&nbsp;</td></tr>
#<tr class="round2"><td class="p12">&nbsp;&nbsp;&nbsp;&nbsp;2nd</td><td class="cumelabel"><span class="label">cumulative</span></td><td class="cume"><span class="strut">&nbsp;</span></td></tr>

# Top of a scorecard in the middle of the file:
#</table><p class=notice>This report was generated by <cite>tsh</cite> version 3.320.  For more information about <cite>tsh</cite>, please contact John Chew.</p><div style="page-break-after:always">&nbsp;</div><h1>Wilmington DE Madness 2013-08-31 .. -09-02</h1><table class=scorecard border=0 align=center cellspacing=0><tr class="top1"><th class="number" colspan=3>#2</th><th class="name" colspan=5><span class="label">Name:</span> Rosenberg, Mark</th><th class="rating" colspan=2><span class="label">Rating:</span> 1739</th></tr>
#<tr class="top2"><th class="round">Round</th><th class="table">Board</th><th class="onum">Opp. #</th><th class="orat">Rating</th><th class="onam">Opponent Name</th><th class="won">Won</th><th class="lost">Lost</th><th class="for">Player<br>Score</th><th class="against">Opponent<br>Score</th><th class="spread">Spread</th></tr>


# Insert this matter to split the table:


#</table><p class=notice>This report was generated by <cite>tsh</cite> version 3.320.  For more information about <cite>tsh</cite>, please contact John Chew.</p><div style="page-break-after:always">&nbsp;</div><table class=scorecard border=0 align=center cellspacing=0><tr class="top1"><th class="number" colspan=3>#2</th><th class="name" colspan=5><span class="label">Name:</span> Rosenberg, Mark</th><th class="rating" colspan=2><span class="label">Rating:</span> 1739</th></tr>
#<tr class="top2"><th class="round">Round</th><th class="table">Board</th><th class="onum">Opp. #</th><th class="orat">Rating</th><th class="onam">Opponent Name</th><th class="won">Won</th><th class="lost">Lost</th><th class="for">Player<br>Score</th><th class="against">Opponent<br>Score</th><th class="spread">Spread</th></tr>


my $player_number;
my $player_name;
my $player_rating;

my $break_after_next_line = 0;
my $break_after_this_line = 0;

while (<>)
  {
    if ($break_after_next_line)
      {
	$break_after_next_line = 0;
	$break_after_this_line = 1;
      }

    /class=\"number\" colspan=3\>\#(\d+)\</
      && do
	{
	  $player_number = $1;
	};

    /class\="label\">Name:\<\/span\>([^\<]+)\</
      && do
	{
	  $player_name = $1;
	};

    /class=\"label\"\>Rating:\<\/span\>\s+(\d+)\</
      && do
	{
	  $player_rating = $1;
	};

    /class=\"round\"\>11\</  # 19 for Mad, 10 for Mild
      && do
	{
	  $break_after_next_line = 1;
	};

    print;

    if ($break_after_this_line)
      {
	$break_after_this_line = 0;
print <<__EOF__
</table><div style="page-break-after:always">&nbsp;</div><table class=scorecard border=0 align=center cellspacing=0>
<tr class="top2"><th class="round">Round</th><th class="onum">Opp. #</th><th class="orat">Rating</th><th class="onam">Opponent Name</th><th class="won">Won</th><th class="lost">Lost</th><th class="for">Player<br>Score</th><th class="against">Opponent<br>Score</th><th class="spread">Spread</th></tr>
__EOF__
      }

  }
#<p class=notice>This report was generated by <cite>tsh</cite> version 3.320.  For more information about <cite>tsh</cite>, please contact John Chew.</p><div style="page-break-after:always">&nbsp;</div><table class=scorecard border=0 align=center cellspacing=0><tr class="top1"><th class="number" colspan=3>#$player_number</th><th class="name" colspan=5><span class="label">Name:</span> $player_name</th><th class="rating" colspan=2><span class="label">Rating:</span> $player_rating</th></tr>
