#!/usr/bin/env perl

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 $section_issue = "";
my $result = "";
my %all = ();
my %no_issue = ();
my $no_issue_no_owner = "";

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

    if ( $what ne "" ) {
        if ( $who ne "" ) {
            if ( !defined( $all{$who} ) ) {
                $all{$who} = $what;
            }
            else {
                $all{$who} .= "+$what" unless "+$all{$who}+" =~ /\+$what\+/;
            }
        }
        else {
            $result .= "+" unless $result eq "";
            $result .= $what;
        }
    }
    elsif ( !( $patch =~ /^(cws-|workspace\.)/ ) ) {
        if ( $who ne "" ) {
            if ( !defined( $no_issue{$who} ) ) {
                $no_issue{$who} = $patch;
            }
            else {
                $no_issue{$who} .= "\n$patch" unless "$no_issue{$who}" =~ /$patch/;
            }
        }
        else {
            $no_issue_no_owner .= "\n" unless $no_issue_no_owner eq "";
            $no_issue_no_owner .= $patch;
        }
    }
}

while ( <APPLY> ) {
    if ( /^\s*#.*/ ) {
        # comment
    }
    elsif ( /^\s*\[\s*(.*)\]/ ) {
        $section = $1;
        $section_owner = "";
        $section_issue = "";
    }
    elsif ( /^SectionOwner\s*=>\s*([^\s]*)/ ) {
        $section_owner = $1;
    }
    elsif ( /^SectionIssue\s*=>.*i#([0-9]*)/ ) {
        $section_issue = $1;
        insert_issue( $section_owner, $section_issue, "" );
    }
    elsif ( /^\s*([^\#,\s]+\.diff)\s*,?\s*(.*)?$/ )
    {
        my $patch = $1;
        my $tmp = $2;
        my $issue = $section_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, $patch );
        }
    }
}

close APPLY;

my $uniq = "";
foreach $who ( sort( keys %all, keys %no_issue ) ) {
    next if ( $who eq $uniq );
    $uniq = $who;

    print "===== $who =====\n";
    if ( defined( $all{$who} ) ) {
	print "\nhttp://www.openoffice.org/issues/buglist.cgi?issue_id=$all{$who}\n";
    }
    if ( defined( $no_issue{$who} ) ) {
	print "\nNo IZ number:\n\n$no_issue{$who}\n";
    }
    print "\n";
}

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

if ( $no_issue_no_owner ne "" ) {
    print "===== No owner and no IZ number =====\n\n$no_issue_no_owner\n";
}
