use strict;

my $append;
my $mark;
my $merge;
my $ref;

my $resetthis;

while (<>) {

    my $line = $_;

    if ($line =~ /^commit (.*)$/)
    {
	$ref = $1;
    }
    if ($line =~ /^mark :(\d+)$/)
    {
	$mark = $1;
    }
    if ($line =~ /^merge :(\d+)$/)
    {
	$merge = $1;
    }

    # cvs2svn created an empty empty commit to create the following branches.
    # Skip them.
    if ($line =~ /^This commit was manufactured by cvs2svn to create branch 'PRE_LIBPQ'./)
    {
	$resetthis = 1;
    }
    if ($line =~ /^This commit was manufactured by cvs2svn to create branch 'REL6_4'./)
    {
	$resetthis = 1;
    }
    if ($line =~ /^This commit was manufactured by cvs2svn to create branch 'REL6_5_PATCHES'./)
    {
	$resetthis = 1;
    }
    if ($line =~ /^This commit was manufactured by cvs2svn to create branch 'REL7_0_PATCHES'./)
    {
	$resetthis = 1;
    }
    if ($line =~ /^This commit was manufactured by cvs2svn to create branch 'REL7_1_STABLE'./)
    {
	$resetthis = 1;
    }
    if ($line =~ /^This commit was manufactured by cvs2svn to create branch 'REL7_2_STABLE'./)
    {
	$resetthis = 1;
    }
    if ($line =~ /^This commit was manufactured by cvs2svn to create branch 'REL8_1_STABLE'./)
    {
	$resetthis = 1;
    }

    # Fix commit with this log message:
    #
    #  [7.03.0204]
    #  1) Clear statements' cursors on commit/rollback.
    #  2) Change SQLColumns, SQLTablePrivileges to take show_system_tables
    #     option into account.
    #  3) When an ODBC 2.x application working with an ODBC 3.x driver
    #  ...
    #
    # The commit message contains some strange 0xc2 0x81 bytes at the beginning
    # of some lines. I'm not sure how that happened, but replace them with
    # spaces.
    $line =~ s/^\xc2\x81 /   /;

    # A commit containing a name with the "small s with caret" char. Not sure
    # what encoding it was in originally, but cvs2svn messes it up. Replace it
    # with the correct UTF-8 char.
    $line =~ s/(Robert Jire[[:^ascii:]]*)/Robert Jire\xC5\xA1/;

    print $line;

    if (($resetthis) && ($line eq "\n"))
    {
	print "# begin injected\n";
	print "reset $ref\n";
	print "from :$merge\n";
	print "# end injected\n";
	$resetthis = 0;
	$mark = '';
	$merge = '';
	$ref = '';
    }
}
