#!/usr/bin/env perl

use 5.010001;
use strict;
use warnings;

use Getopt::Long qw(GetOptions);
use Module::Path qw(module_path);
use Pod::Usage qw(pod2usage);

our $VERSION = '0.13.1'; # VERSION

my %opts;
GetOptions(\%opts,
           'help|h|?',
           'quiet|q',
           'full|f!',
           'abs',
           'all',
           'find-prefix|p!' => \$opts{find_prefix},
           'find-pm!'       => \$opts{find_pm},
           'find-pmc!'      => \$opts{find_pmc},
           'find-pod!'      => \$opts{find_pod},
           'pod' => sub {
               $opts{find_pod} = 1;
               $opts{find_pm}  = 0;
               $opts{find_pmc} = 0;
           },
       )
    or pod2usage 2;

if ($opts{help}) {
    pod2usage(
        -verbose => 2,
        -exitval => 0,
    )
}
elsif (!@ARGV) {
    warn "mpath: Please specify at least one module\n";
    exit 99;
}

$opts{full} //= @ARGV > 1 ? 1:0;

my $all_found = 1;

my %mpopts = (
    abs         => $opts{abs},
    all         => $opts{all},
    find_prefix => $opts{find_prefix},
    find_pm     => $opts{find_pm},
    find_pmc    => $opts{find_pmc},
    find_pod    => $opts{find_pod},
);

for my $module (@ARGV) {
    my @paths;
    if ($opts{all}) {
        @paths = module_path($module, \%mpopts);
    } else {
        my $path = module_path($module, \%mpopts);
        push @paths, $path if defined $path;
    }

    if (!@paths) {
        $all_found = 0;
        warn "mpath: $module not found\n" unless $opts{quiet};
    } else {
        for (@paths) {
            print "$module\t" if $opts{full};
            print "$_\n";
        }
    }
}

my $status = $all_found ? 0 : 1;
exit $status;

# ABSTRACT: Display the path to a perl module (installed locally)
# PODNAME: mpath

__END__

=pod

=encoding UTF-8

=head1 NAME

mpath - Display the path to a perl module (installed locally)

=head1 VERSION

This document describes version 0.13.1 of mpath (from Perl distribution Alt-Module-Path-SHARYANTO), released on 2014-06-22.

=head1 DESCRIPTION

mpath displays the full path to a perl module on the local system. It uses the
C<module_path()> function from L<Module::Path> to get the path.

If one of the module wasn't found, mpath will exit with the exit code 1 and
print the following message (you can silence it with the option B<--quiet>):

  % mpath Foo::Bar
  mpath: Foo::Bar not found

or:

  % mpath Moose Foo::Bar
  Moose    /usr/local/lib/perl5/site_perl/5.16.0/darwin-2level/Moose.pm
  mpath: Foo::Bar not found

=head1 USAGE

  mpath [OPTIONS] MODULE [MODULE ...]

=head1 EXAMPLE

  % mpath Module::Path
  /usr/local/lib/perl5/site_perl/5.16.0/Module/Path.pm

  % mpath --full Module::Path
  Module::Path /usr/local/lib/perl5/site_perl/5.16.0/Module/Path.pm

  % mpath Moose Moo
  /usr/local/lib/perl5/site_perl/5.16.0/darwin-2level/Moose.pm
  /usr/local/lib/perl5/site_perl/5.16.0/Moo.pm

=head1 OPTIONS

=over

=item B<-h>, B<-?>, B<--help>

Print this help message and exit.

=item B<-f>, B<--full>

Print module name before the path. For convenience this is turned on by default
if there are more than one argument (but you can still turn it off using
C<--nofull>).

=item B<-q>, B<--quiet>

Don't print any error when one of the module requested could not be found.

=item B<--abs>

Absolutize paths.

=item B<--all>

Find all found paths in C<@INC> instead of the first one.

=item B<--[no]find-prefix>, B<-p>

Find module prefixes (e.g. When there is a module Foo::Bar, searching Foo will
return the directory). Default is off.

=item B<--[no]find-pm>

Find C<.pm> files. Default is on.

=item B<--[no]find-pmc>

Find C<.pmc> files. Default is on.

=item B<--[no]find-pod>

Find C<.pod> files.

=item B<--pod>

Equivalent to C<--find-pod --nofind-pm --nofind-pmc>.

=back

=head1 SEE ALSO

L<Alt::Module::Path::SHARYANTO>

L<Module::Path>

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/Alt-Module-Path-SHARYANTO>.

=head1 SOURCE

Source repository is at L<https://github.com/sharyanto/perl-Alt-Module-Path-SHARYANTO>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Alt-Module-Path-SHARYANTO>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHOR

Steven Haryanto <stevenharyanto@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Steven Haryanto.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
