#!/usr/bin/perl -w

use strict;
use warnings;

use File::Path qw(make_path remove_tree);
use Test::Repocop::Log;
use Test::Repocop::Workdir;
use Test::Repocop::TestDB;
use Test::Repocop::TestDB::Status qw/testdb_status_num2str testdb_status_str2num testdb_status_num_max/;
use Test::Repocop::Metadata;
use Test::Repocop::CLI::Base;
use Test::Repocop::CLI::Report;
our @ISA=qw/Test::Repocop::CLI/;
our @LONGOPT_POD2USAGE=qw/-verbose 1 -exitval 1/;

my $reportdir="$repocop_workdir/reports/status";

my ($by_srpm,$by_test,$total)=(1,0,1);
our @LONGOPT=(
    "outdir=s"  => \$reportdir,
    "by-srpm!"  => \$by_srpm,
    # TODO: not implemented
    "by-test!"  => \$by_test,
);
__PACKAGE__->get_and_process_cli_options();
&Test::Repocop::Workdir::die_if_nothing_to_report();
&Test::Repocop::CLI::Report::process_arguments();

&repocop_note("creating reports in $reportdir...");

my $TOTAL_STATUS_NUM;
my %SRPM2STATUS;

my $metadata=Test::Repocop::Metadata->new();
my $testdb=Test::Repocop::TestDB->new();
my $cache=$testdb->get_pkg_test_status_iterator();
while (my ($rpm,$test,$status)=$cache->iterate3_filtered()) {
    warn "got undef: rpm=[$rpm] test=[$test] status=[$status]\n" unless defined($rpm) and defined($test) and defined($status);
    my $status_num=&testdb_status_str2num($status);
    $TOTAL_STATUS_NUM=&testdb_status_num_max($TOTAL_STATUS_NUM,$status_num);
    my $srpm=$metadata->sourceid($rpm);
    warn "got undef:[$srpm] for $rpm" unless defined($srpm);
    $SRPM2STATUS{$srpm}=&testdb_status_num_max($SRPM2STATUS{$srpm},$status_num);
}

if ($total) {
    my $dir_total=&prepare_report_subdir('');
    my $str_status=&testdb_status_num2str($TOTAL_STATUS_NUM);
    $str_status='ok' if $str_status eq 'skip';
    open (CURFILE, ">", "$dir_total/total");
    print CURFILE $str_status, "\n";
    close (CURFILE);
}

if ($by_srpm) {
    my $dir_by_srpm=&prepare_report_subdir('by-srpm');
    foreach my $srpm (sort keys(%SRPM2STATUS)) {
	my $str_status=&testdb_status_num2str($SRPM2STATUS{$srpm});
	$str_status='ok' if $str_status eq 'skip';
	open (CURFILE, ">", "$dir_by_srpm/$srpm");
	print CURFILE $str_status, "\n";
	close (CURFILE);
    }
}

sub prepare_report_subdir {
    my ($name)=@_;
    &repocop_note("by $name...");
    my $dir_by_name="$reportdir/$name";
    remove_tree($dir_by_name);
    make_path($dir_by_name);
    return $dir_by_name;
}

&repocop_note("done.");

=head1	NAME

repocop-report-status - a tool that creates short txt reports on repocop unit tests results.

=head1	SYNOPSIS

B<repocop-report-status>
[B<-h|--help>]
[B<-v|--verbose>]
[B<-q|--quiet>]
[B<--workdir> I<workdir>]
[B<--acl-file> I<file>]
[B<--acl-altlinux> [B<--acl-branch I<sisyphus|t7|...>] [B<--acl-expire> I<time>] ]
[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 ...]
[repocop_id ...]

=head1	DESCRIPTION

B<repocop-report-status> processes results of repocop unit tests, created with 
repocop-run command, stored in I<repocop workdir> and creates short results in txt form.


=head1 ARGUMENTS

The arguments are the set of repocop ids. The repocop ID is a string
<NAME>-<VERSION>-<RELEASE>.ARCH for binary rpm packages and is a string
<NAME>-<VERSION>-<RELEASE>.src for source rpm packages.

Also repocop IDs can be extracted from file names,
so you can give an arument like /path/to/rpm/files/*.rpm.


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



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

The file content is ACL db,
which is used to sort result by ACL.
The argument is generic acl file compatible with Sisyphus' list.src.classic.

Alternatively, you can provide B<--acl-altlinux>.

=item	B<--acl-altlinux>

This option is ALTLinux-specific. if provided, repocop use ALTLinux::ACL
(perl-ALTLinux-ACL is required). Also, the following options of ALTLinux::ACL
become available:

B<--acl-branch> I<sisyphus|t7|...> Name of branch. Default is sisyphus.

B<--acl-expire> I<days> Cache expiration time, float, in days. Default is 1/5 day.


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


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

