#!/usr/bin/perl -w

use 5.006;
use strict;
use warnings;

our $VERSION = '0.01_04';

use Module::Which;
use Getopt::Long;
use Pod::Usage;

sub print_version {
	use File::Basename qw(basename);
	print "This is ", basename($0), ", $VERSION\n";
	exit(0);
}

our $VERBOSE = 0 || $ENV{WHICH_PM_VERBOSE};

my %which_options;
%which_options = (
	return => 'ARRAY',
	verbose => $VERBOSE,
	quiet => sub { $which_options{verbose} = 0 },	
);

GetOptions(
	\%which_options,
	"verbose",
	"quiet",
	#"include=s@"
	"help",
	"man",
	"version",
) or pod2usage(2);

#pod2usage(1) if $which_options{help};
pod2usage(-exitstatus => 1, -verbose => 1) if $which_options{help};
pod2usage(-exitstatus => 0, -verbose => 2) if $which_options{man};
print_version if $which_options{version};

#sub get_term_width {
#	return 200 unless -t STDOUT; 
#	eval { require Term::ReadKey };
#	return 80 if ($@); 
#	my @size = Term::ReadKey::GetTerminalSize(*STDOUT);
#	print "size: @size\n";
#	return shift @size;
#}

sub out_info {
	my $info = shift;
	my $pm_len = 0;
	for (@$info) {
		$pm_len = length($_->{pm}) if $pm_len < length($_->{pm});
	}
	return { pm_len => $pm_len }
}

sub print_pm {
	my $pm_info = shift;
	my $out_info = shift;
	$pm_info->{version} = "(not found)" unless defined $pm_info->{version};
	my $pm_len = $out_info->{pm_len};
	$pm_len = 20 if $pm_len < 20;
	$pm_info->{path} = '' unless $pm_info->{path}; # for (not found) entries
	my $w = 78 - $pm_len -10 -2;
	printf "%-${pm_len}s %-10s %-${w}s\n", 
		$pm_info->{pm}, $pm_info->{version}, substr($pm_info->{path}, 0, $w);
}

my $info = which(@ARGV, \%which_options);

my $out_info = out_info($info);
for (@$info) {
	print_pm($_, $out_info);
#	my $pm_info = $_;
#	$pm_info->{version} = "(not found)" unless defined $pm_info->{version};
#	printf "%-20s $pm_info->{version}\n", $pm_info->{pm};
}



__END__

=head1 NAME

which_pm - Perl script to find out which versions of certain Perl modules are installed

=head1 SYNOPSIS

  which_pm [--verbose] [--quiet] DBI DBD::
  which_pm --help
  which_pm --man
  which_pm --version

  which_pm Module::Find Module::Which File::

  Options:
    -verbose       shows error messages (due to "require $module")
    -quiet         hides error messages (due to "require $module")
    -help          brief help message
    -man           full documentation
    -version       prints the version number of this script

=head1 OPTIONS

=over 4

=item B<-verbose>

Version is determined by doing a C<require> on runtime.
In case, the statement fails, error messages are shown.

=item B<-quiet>

Version is determined by doing a C<require> on runtime.
In case, the statement fails, error messages are silently hidden.
This is the default. A failure during C<require> will
result 'unknown' as version.

=item B<-help>

Print a brief help message and exits.

=item B<-man>

Prints the manual page and exits.

=item B<-version>

Prints the version number and exits.

=back

=head1 DESCRIPTION

See the the documentation of L<Module::Which>.

=head1 SEE ALSO

Please report bugs via CPAN RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Module-Which>.

=head1 AUTHOR

Adriano R. Ferreira, E<lt>ferreira@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2005 by Adriano R. Ferreira

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


=cut
