#! /usr/bin/perl -w

sub usage()
{
    print STDERR "show-patches - list issues of patches without the not intended for upstream
Usage: show-patches\n";

    exit 1;
}

$help = shift @ARGV;
if ( defined( $help ) && ( $help eq "-h" || $help eq "--help" ) ) {
    usage();
}

open APPLY, "patches/src680/apply" || die "Cannot open patches/src680/apply\n";

my $section = "";
my $section_owner = "";
my $result = "";
my %all = ();

sub insert_issue($$)
{
    my ( $who, $what ) = @_;

    if ( $what ne "" ) {
        if ( $who ne "" ) {
            if ( !defined( $all{$who} ) ) {
                $all{$who} = $what;
            }
            else {
                $all{$who} .= "+$what";
            }
        }
        else {
            $result .= "+" unless $result eq "";
            $result .= $what;
        }
    }
}

while ( <APPLY> ) {
    if ( /^\s*#.*/ ) {
        # comment
    }
    elsif ( /^\s*\[\s*(.*)\]/ ) {
        $section = $1;
        $section_owner = "";
    }
    elsif ( /^SectionOwner\s*=>\s*([^\s]*)/ ) {
        $section_owner = $1;
    }
    elsif ( /^SectionIssue\s*=>.*i#([0-9]*)/ ) {
        insert_issue( $section_owner, $1 );
    }
    elsif ( /^\s*([^\#,\s]+\.diff)\s*,?\s*(.*)?$/ )
    {
        my $patch = $1;
        my $tmp = $2;
        my $issue = "";
        my $owner = $section_owner;
        if ( $tmp =~ /i#([0-9]*)/ ) {
            $issue = $1;
        }
        foreach $o ( split( /\s*,\s*/, $tmp ) ) {
            $owner = $o unless $o =~ /#/;
        }

        if ( !( $section =~ /FixesNotForUpstream/ ) &&
            !( $section =~ /Defaults/ ) )
        {
            insert_issue( $owner, $issue );
        }
    }
}

close APPLY;

foreach $who ( sort( keys %all ) ) {
    print "$who: http://www.openoffice.org/issues/buglist.cgi?issue_id=$all{$who}\n\n";
}

if ( $result ne "" ) {
    print "Without owner: http://www.openoffice.org/issues/buglist.cgi?issue_id=$result\n";
}
