#!/usr/bin/perl -w

use strict;
use warnings;

use Carp;
use File::Basename;

use Test::Repocop::Metadata;
use Test::Repocop::CLI::Base;
our @ISA=qw/Test::Repocop::CLI/;

our @LONGOPT_POD2USAGE=qw/-verbose 1 -exitval 1/;
my $column=1;
my $out='name';
our @LONGOPT=(
    "column=i"  => \$column,
    'name' => sub {$out='name'},
    'sid|sourceid' => sub {$out='sid'},
    'sname|sourcename' => sub {$out='sname'},
);
__PACKAGE__->get_and_process_cli_options();
&Test::Repocop::Workdir::die_if_nothing_to_report();

my $metadata=Test::Repocop::Metadata->new();
# in perl, column counting is from 0.
$column--;
while (<>) {
    my @line=split(/\s+/,$_);
    my $id=$line[$column];
    next unless $id; # empty line ?
    my $name=$metadata->name($id);
    if (not $name) {
	warn "Oops! no name for $id";
	next;
    }
    my $sourceid=$metadata->sourceid($id);
    my $sourcename=$metadata->sourceid($sourceid);
    if ($out eq 'name') {
	print "$name\t$_";
    } elsif ($out eq 'sname') {
	print "$sourcename\t$_";
    } elsif ($out eq 'sid') {
	print "$sourceid\t$_";
    }
}



__END__

=head1	NAME

repocop-filter-list-id-prepend-name - prints rpm name, srpm name or srpm id of given id.

=head1	SYNOPSIS

B<repocop-filter-list-id-prepend-name>
[B<--name> ]
[B<--sourceid> ]
[B<--sourcename> ]

[B<-h|--help>]
[B<-v|--verbose>]
[B<-q|--quiet>]
[B<--workdir> I<workdir>]

=head1	DESCRIPTION

B<repocop-filter-list-id-prepend-name> prepends a column with rpm name, srpm name or srpm id
to a batch file with the list of repocop ids.

=head1	OPTIONS

=over

=item	B<--name>

print name

=item	B<--sourceid>

print source id

=item	B<--sourcename>

print source id

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