#!/usr/bin/perl -w

use strict;
use Data::Array2ArrayMap::Hash::XSTree;
use Test::Repocop::Common;
use Test::Repocop::Options;
use Test::Repocop::TestCache;
use Test::Repocop::Metadata;

my ($diffmode,$nmumode);

&Test::Repocop::Options::get_common_options(
    'diff'=>\$diffmode,
    'nmu'=>\$nmumode,
);
&Test::Repocop::Options::die_if_nothing_to_report();

my $RPM2TEST=Data::Array2ArrayMap::Hash::XSTree->new();
my $RPM2NAME=Data::Array2ArrayMap::Hash::XSTree->new();

my $metadata=Test::Repocop::Metadata->new();
my $cache=Test::Repocop::TestCache->new();
while (my ($rpm,$test,$status)=$cache->iterate3_filtered()) {
    #print STDERR "$rpm:$test:$status\n";
    my $sourceid=$metadata->sourceid($rpm);
    my $name=$metadata->name($rpm);
    if ($status eq 'warn' || $status eq 'fail') {
	$RPM2TEST->append([$sourceid],[$test]);
	$RPM2NAME->append([$sourceid],[$name]);
    }
}

my $outdir='./OUT';
mkdir $outdir;

foreach my $rpmfile (@Repocop::arg::pkglist) {
    my $sourceid=&Test::Repocop::Common::rpmfile2key($rpmfile);
    unless ($sourceid =~ /src$/) {
	warn "$sourceid: not a src.rpm!\n";
	next;
    }
    my @tests=$RPM2TEST->get([$sourceid]);
    if (scalar @tests) {
	print "$sourceid:\n";
	my @names=$RPM2NAME->get([$sourceid]);
	my $cmd="repocop-fix-backend --out=$outdir --srpm $rpmfile" .
	    ($diffmode ? " --diff " : "") .
	    ($nmumode ? " --nmu " : "") .
	    " --test " . join(',',@tests) .
	    " --pkgname=".join(',',@names);
	print $cmd, "\n" if $verbose and $verbose>1;
	system ($cmd)==0 or die "error: $!";
    }
}

=head1	NAME

repocop-report-stdout - a tool that reports on repocop unit tests results.

=head1	SYNOPSIS

B<repocop-report-stdout>
[B<-h|--help>]
[B<-c|--cache> I<cachedir>]
[B<-e|--exclude> I<comma separated list of tests>]
[B<-i|--include> I<comma separated list of tests>]
=head1	DESCRIPTION

B<repocop-report-txt> processes results of repocop unit tests, created with 
repocop-run command, stored in <cachedir> and creates results in txt form.
Presize subset of tests can be selected using B<--include>
and B<--exclude> options.

=head1	OPTIONS

=over

=item	B<-a, --all>

Report successful/skipped tests along with failed tests.

=item	B<-b, --bad>

Report only failed/warned tests.

=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<-e, --exclude> I<comma separated list of tests>

Report all processed tests exept the given excluded set.

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

Report the given set of tests.

=item	B<-h>

Display this help and exit.

=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 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

