#!/usr/bin/perl -w

use strict;
use warnings;

use File::Find;
use RPM::Header;
use Getopt::Long;
use Pod::Usage;

my ($help,@srpmdir);
my $verbose=0;
my $allow_binary_rpm=0;

GetOptions (
    "h|help"  => \$help,
    "binary-rpm" => \$allow_binary_rpm,
    "srpmdir=s" => \@srpmdir,
    "rpmdir=s" => \@srpmdir,
    "verbose+"  => \$verbose,
    'quiet'   => sub { $verbose = 0 },
    );

if ($help or not @ARGV) {
    #exec "pod2usage --exit=0 $0";
    pod2usage({ #-message => "the options below are package-specific:" ,
	-exitval => 0  ,
	-verbose => $verbose,
	      } );
}

push @srpmdir, shift @ARGV unless @srpmdir;

my %name2path;
map {&read_srpmdir($_)} @srpmdir;

foreach my $name (@ARGV) {
    my $path=$name2path{$name};
    if (defined $path) {
	print $path,"\n";
    } else {
	warn "no path for $name\n";
    }
}

sub read_srpmdir {
    my $srpmdir=shift;
    unless (-d $srpmdir) {
	die "not a directory: $srpmdir\n";
    } else {
	find (\&wanted, $srpmdir);
    }
}

sub wanted {
    return if ! /\.rpm$/;
    return if ! $allow_binary_rpm && ! /\.src\.rpm$/;
    my $path=$File::Find::name;
    return unless -e $path;
    my $rhref = new RPM::Header $path;
    $name2path{$rhref->{NAME}}=$path;
}

=head1	NAME

girar-nmu-helper-name2path - converts the list of names for src.rpms into paths

=head1	SYNOPSIS

B<girar-nmu-helper-name2path> [--srpmdir] /path/to/dir/SRPMS [list of src.rpm names]

=head1	DESCRIPTION

B<girar-nmu-helper-name2path> converts the list of src.rpm names into
the list of src.rpm paths. 
src.rpms are searched in the directory specified as --srpmdir I<dir> option 
(or specified as the first argument).

Note: this utility is deprecated. Use girar-nmu-filter-name --name2path.

=head1	OPTIONS

=over

=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<--srpmdir> I<path>

Path to dir with src.rpms.

=back

=head1	AUTHOR

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

=head1	COPYING

Copyright (c) 2010-2021 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

