
use strict;

my @lines = ();
my ($content, $header) = (-3, 1);

sub flush() {
    my $proplength = 0;
    for my $l (@lines) {
	if ($l =~ /^Prop-content-length:\s*(\d+)/) {
	    $proplength = $1;
	}
	if ($l =~ /^Text-content-length:\s*(\d+)/) {	    
	    my $oldtxt = $1;
	    my $txt = $content - $proplength;
	    if (abs($oldtxt - $txt) > 2) {
		print STDERR $oldtxt, " -> ", $txt, "\n"; 
		$l = "Text-content-length: " . $txt . "\n";
	    }
	}
	if ($l =~ /^Content-length:\s*(\d+)/) {
	    my $oldcontent = $1;
	    if (abs($oldcontent - $content) > 2) {
		print STDERR $oldcontent, " -> ", $content, "\n" ;
		$l = "Content-length: " . $content . "\n";
	    }
	}
	print $l unless $l =~ /^Text-content-md5:/;
    }
    @lines = ();
}

for my $l (<>) {
    if ($l =~ /^Node-path:/ or $l =~ /Revision-number: /) { 
	flush();
	$header = 1;
    }
    #print STDERR $l if ($header);
    push @lines, $l;
    $content += length($l);
    if ($l =~ /^Content-length:/) {
	print STDERR "????? $l", @lines if !$header;
	$content = -3;
	$header = 0;
    }
}

flush();



