#!/usr/bin/perl -w

use strict;
use warnings;

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

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

GetOptions (
    "help"  => \$help,
    "srpmdir=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,
	      } );
}

$srpmdir||=shift @ARGV;

my %name2path;
unless (-d $srpmdir) {
    die "not a directory: $srpmdir\n";
} else {
    my $PWD=`pwd`;
    chomp $PWD;
    $srpmdir=~s!^\./!!;
    $srpmdir="$PWD/$srpmdir" if $srpmdir!~m!^/!;
    open (RPMARGS_FILES, "ls -1 $srpmdir |egrep -x '[^.].*[.]src[.]rpm' |") || die $!;
    my $f;
    while ($f=<RPMARGS_FILES>) {
	chomp $f;
	my $path="$srpmdir/$f";
	next unless -e $path;
	my $rhref = new RPM::Header $path;
	$name2path{$rhref->{NAME}}=$path;
    }
    close RPMARGS_FILES;
}


foreach my $name (@ARGV) {
    print $name2path{$name},"\n";
}

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

