#!/usr/bin/perl -w

use strict;
use warnings;
use Test::Repocop::Common;
use Test::Repocop::Options;
use Test::Repocop::TestCache;
use Test::Repocop::Metadata;
use Test::Repocop::ACLWrapper;
use File::Basename;
#use File::Path;
use Carp;


my $qareportmode=0;
my $indir;
my $digestmode=1;
my $report_by_leader=1;
my $report_by_packager=0;

&Test::Repocop::Options::get_common_options(
    'qa-robot-report!'=> \$qareportmode,
    "indir=s"    => \$indir,
);

my ($by_acl_dir,$by_nick_dir,$by_leader_dir,$qa_robot_report_dir);

my $repocop_reportdir="$repocop_cachedir/reports/diff";
$indir||=$repocop_reportdir.'/by-srpm';
$by_acl_dir=$repocop_reportdir.'/by-acl';
$by_nick_dir=$repocop_reportdir.'/by-packager';
$by_leader_dir=$repocop_reportdir.'/by-leader';
$qa_robot_report_dir=$repocop_reportdir.'/qa-robot';

die "input directory $indir does not exist\n" if not -d $indir;

my $aclmap=Test::Repocop::ACLWrapper->new();
my $metadata=Test::Repocop::Metadata->new();
my $cache=Test::Repocop::TestCache->new();

rmtree([
    $by_acl_dir,
    $by_nick_dir,
    $by_leader_dir,
    $qa_robot_report_dir,
       ]);

my %srcid;
foreach (glob "$indir/*") {
    next unless -e $_ and -s $_; # skipping empty diffs
    print "linking $_\n" if $verbose and $verbose>2;
    my $diffpath=$_;
    my $diffname=basename($_);
    my $id=&Test::Repocop::Common::patchname2srcid($diffname);
    $srcid{$id}=1;
    if ($aclmap) {
	my $srcname=$metadata->name($id);
	my @acls=$aclmap->name2acl($srcname);
	foreach my $acl (@acls) {
	    mkdir "$by_acl_dir/$acl";
	    link $diffpath, "$by_acl_dir/$acl/$diffname";
	}
	if ($report_by_leader and $acls[0]) {
	    mkdir "$by_leader_dir/$acls[0]";
	    link $diffpath, "$by_leader_dir/$acls[0]/$diffname";
	}
    }
    if ($report_by_packager) {
	my $packager=$metadata->nick($id);
	mkdir "$by_nick_dir/$packager";
	link $diffpath, "$by_nick_dir/$packager/$diffname";
    }
}

# ------------ qa-robot support -------------------------
if ($qareportmode) {
    mkpath([$qa_robot_report_dir]);
    open (my $fh, '>', $qa_robot_report_dir.'/diff.ls') || die "can't open $qa_robot_report_dir/patches.ls: $!";
    foreach (keys(%srcid)) {
	print $fh $metadata->name($_),"\t",$_,"\n";
    }
    close ($fh);
}

=head1	NAME

repocop-report-diff - a tool that arranges generated repocop diffs by acl/packager.

=head1	SYNOPSIS

B<repocop-report-diff>
[B<--indir> I<path to generated diffs>]
[B<-h|--help>]
[B<-v|--verbose>]
[B<-q|--quiet>]
[B<-c|--cachedir> I<cachedir>]
[B<--et|--exclude-test> I<comma separated list of tests>]
[B<--it|--include-test> I<comma separated list of tests>]
[B<--ep|--exclude-packager> I<comma separated list of packager's nicks>]
[B<--ip|--include-packager> I<comma separated list of packager's nicks>]
[B<--pkgcollectors-dir> I<comma separated list of local collectors' dirs>]
[B<--srccollectors-dir> I<comma separated list of local collectors' dirs>]
[B<--pkgtests-dir> I<comma separated list of local tests' dirs>]
[B<--srctests-dir> I<comma separated list of local tests' dirs>]
[B<--ex|--except>] 
[B<-g|--given>] 
[B<-l|--last-run>] 
[B<--newer>] I<filename>
[B<-r|--report> <s[kip]|o[k]|w[arn]|f[ail]>]
[I<DIR>...] [I<FILE>...]

=head1	DESCRIPTION

B<repocop-report-diff> arranges generated repocop diffs by acl/packager.

=head1	OPTIONS

=over

=item	B<-c,--cachedir> I<dir>

Provides alternative location for cachedir. 
Repocop cachedir is a place where test results and 
packages metadata information are stored.

=item	B<--except>, B<--given>

Control processing of rpm arguments. 
B<--given> (default) means processing only given rpm arguments.
B<--except>  means processing all data except given rpm arguments.

=item	B<--et, --exclude-test> I<comma separated list of tests>

Report all processed tests exept the given excluded set.

=item	B<--it, --include-test> I<comma separated list of tests>

Report the given set of tests.

=item	B<--ep, --exclude-packager> I<comma separated list of tests>

=item	B<--it, --include-packager> I<comma separated list of tests>

Exclude/include packages according to Packager: tag.

=item [B<--pkgcollectors-dir> I<comma separated list of local collectors' dirs>]

=item [B<--srccollectors-dir> I<comma separated list of local collectors' dirs>]

=item [B<--pkgtests-dir> I<comma separated list of local tests' dirs>]

=item [B<--srctests-dir> I<comma separated list of local tests' dirs>]

Append user's local tests and collectors to repocop.

=item	B<-h, --help>

Display this help and exit.

=item	B<-v, --verbose>, B<-q, --quiet>

Verbosity level. Multiple -v increase the verbosity level, -q sets it to 0.

=item	B<-l, --last-run>

Use the set of packages processed at last run as an argument.

=item	B<--newer> I<filename>

Process packages newer then I<filename> only.
Note: this filtering does not apply to B<--last-run> option.

=item	B<--acl-file> I<file>

the argument is /path/to/Sisyphus/files/list/list.src.classic
This option is ALTLinux-specific. The file content is ACL db,
which is used to sort result by ALTLinux ACL.


=item	[B<--indir> I<path to generated diffs>]

Path to generated diffs to be arranged.

=back

=head1	AUTHOR

Written by Igor Vlasenko <viy@altlinux.org>.

=head1	ACKNOWLEGEMENTS

To Alexey Torbin <at@altlinux.org>, whose qa-robot package
had a strong influence on repocop. 

=head1	COPYING

Copyright (c) 2008-2012 Igor Vlasenko, ALT Linux Team.

This is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation;
either version 2 of the License, or (at your option) any later version.

=cut

