#!/usr/bin/perl -w

use strict;
use warnings;
use Test::Repocop::PkgId;
use Data::Array2ArrayMap::Hash::XSTree;
use Test::Repocop::TestDB;
use Test::Repocop::CLI::Base;
use Test::Repocop::CLI::Report;
use Test::Repocop::CLI::IN::PkgId;
our @ISA=qw/Test::Repocop::CLI/;
our @LONGOPT_POD2USAGE=qw/-verbose 1 -exitval 1/;

__PACKAGE__->get_and_process_cli_options();
&Test::Repocop::Workdir::die_if_nothing_to_report();
&Test::Repocop::CLI::IN::PkgId::process_arguments();

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

my $testdb=Test::Repocop::TestDB->new();
my $cache=$testdb->get_pkg_test_status_result_iterator();
while (my ($pkgid,$test,$status,$result)=$cache->iterate4_filtered()) {
    my $rpm=Test::Repocop::PkgId::pkgid2reportid($pkgid);
    $RPM2TEST->set([$rpm,$test],[$status,$result]);
}

my @rpms=sort $RPM2TEST->keys_at([]);

print "Tests passed. To view details, use --report-level skip option.\n" unless @rpms;

foreach my $rpm (@rpms) {
    print "$rpm:\n";
    print "------------------------------------------\n";
    foreach my $test (sort $RPM2TEST->keys_at([$rpm])) {
	my ($status,$result)=$RPM2TEST->get([$rpm,$test]);
	#chomp $result; print "$test\t$status\t$result\n";
	print "$test\t$status\n";
	print $result if $result;
    }
    print "------------------------------------------\n";
}

__END__

=head1	NAME

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

=head1	SYNOPSIS

B<repocop-report-stdout>
[B<-h|--help>]
[B<-v|--verbose>]
[B<-q|--quiet>]
[B<--workdir> I<workdir>]
[B<--report> I<warn|fail|skip|experimental|ok>]
[B<--report-et|--report-exclude-test> I<comma separated list of tests>]
[B<--report-it|--report-include-test> I<comma separated list of tests>]
[B<--report-include-distrotests>]
[B<--except>]
[B<--given>]
[B<--last-run>]
[B<--by-src-name> name ...]
[dir|rpm file|repocop_id ...]

=head1	DESCRIPTION

B<repocop-report-stdout> processes results of repocop unit tests, created with
repocop-run command, stored in I<repocop workdir> and prints them to STDOUT.


=head1 ARGUMENTS

The arguments are the set of repocop ids. Repocop IDs can be extracted
from rpm files, so you can use an argument like /path/to/rpm/files/*.rpm
or just /path/to/rpm/files.

Note that repocop ids are used for internal representation of rpm packages.
They differ from pkgids used in repocop reports.

Repocop report package ids are used in repocop reports to
identify packages in a human readable way.
The repocop report package ID is a string
<NAME>-<VERSION>-<RELEASE>.ARCH for binary rpm packages and is a string
<NAME>-<VERSION>-<RELEASE>.src for source rpm packages.

Internal Repocop ids are subject to change; current repocop ID is a string
<NAME>-<VERSION>-<RELEASE>.ARCH@timestamp for binary rpm packages and is a string
<NAME>-<VERSION>-<RELEASE>.src for source rpm packages.


=head1	OPTIONS

=over


=item	B<-r, --report> I<skip|experimental|ok|warn|fail>

The level of test results reported. Test results below this level
are not reported. Default is warn.

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

Include all tests exept the given excluded set.

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

Include the given set of tests.

=item	B<--report-include-distrotests>

Include all distrotests.


=item	B<--workdir> I<dir>

Provides alternative location for the Repocop workdir. Repocop workdir
is a place where test results and packages metadata information are stored.
Default is I<~/.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<--except>, B<--given>

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

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

Use the repocop ids of packages processed at last run as an input.

=item	B<--by-src-name>

Input arguments are expected to be src.rpm names, not repocop ids.
Repocop ids are calculated from given src.rpm names.


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

