#! /usr/bin/perl -w

##  $Id: fixlog,v 1.3 2002/12/12 23:43:59 rra Exp $
##
##  Filter cvs log output for cvs2cl.
##
##  Filter the output of cvs log, eliminating entries for files that do not
##  have the tag of the branch that we're filtering for.  Also do some
##  reformatting to deal with some of the CVS commit conventions used by
##  people working on INN, so as to generate nice ChangeLog output.

$| = 1;
my ($file, @data);
my $tag = shift;
sub discard {
    while (<>) {
        last if /^={70}/;
    }
}
while (<>) {
    if (/^Working file: (.*)/) {
        $file = $1;
    } elsif (/^symbolic names:/) {
        push (@data, $_);
        my $found = 0;
        $_ = <>;
        while (defined && /^\s/) {
            my ($checktag) = split /:/;
            $checktag =~ s/^\s+//;
            $found = 1 if (!$tag || $checktag eq $tag);
            push (@data, $_);
            $_ = <>;
        }
        unless ($found) {
            discard;
            undef @data;
            next;
        }
    } elsif (/^-{20}/) {
        print @data;
        undef @data;
        while (defined && !/^={70}/) {
            print;
            print scalar <>;
            print scalar <>;
            $_ = <>;
            my $found = 0;
            my $indent = 0;
            while (defined && !/^-{20}/ && !/^={70}/) {
                if (/^(\S+):\s*$/) {
                    my $check = $1;
                    print, next if $indent == -1;
                    $found = 0 if $found > 1;
                    $indent = 1;
                    $found = 1 if ($check eq $file || $check =~ /\*/);
                    push (@data, $_);
                } elsif (/^\s*- /) {
                    next if ($indent > 0 && !$found);
                    $found++;
                    $indent = 1;
                    undef @data;
                    s/^\s*-\s*//;
                    print "\n$_";
                } elsif (/^\s+/) {
                    next if ($indent > 0 && !$found);
                    if ($indent > 0) {
                        s/^\s+//;
                    } else {
                        print @data;
                        undef @data;
                        $indent = -1;
                    }
                    print;
                } else {
                    $indent = -1;
                    print @data, $_;
                    undef @data;
                }
            } continue {
                $_ = <>;
            }
        }
        print;
        next;
    }
    push (@data, $_);
}
