#!/usr/local/bin/perl

use 5.008;

use strict;
use warnings;

use CPAN::Access::AdHoc;
use CPAN::DistnameInfo;
use Getopt::Long 2.33;
use Pod::Usage;

our $VERSION = '0.000_04';

my %opt;

GetOptions( \%opt,
    qw{ cpan=s quiet! },
    help => sub { pod2usage( { -verbose => 2 } ) },
) or pod2usage( { -verbose => 0 } );

my $cad = CPAN::Access::AdHoc->new( cpan => $opt{cpan} );

my %ext;
foreach my $pkg ( $cad->indexed_distributions() ) {
    my $dist = CPAN::DistnameInfo->new( $pkg );
    my $xt = $dist->extension();
    if ( not defined $xt ) {
	if ( $pkg =~ m/ [.] ( pm [.] gz ) \z /smx ) {
	    $ext{$1}++;
	} else {
	    $opt{quiet}
		or warn "$pkg has an unknown extension\n";
	}
    } elsif ( '' eq $xt ) {
	$opt{quiet}
	    or warn "$pkg has an empty extension\n";
    } else {
	$ext{$xt}++;
    }
}

foreach my $xt ( sort keys %ext ) {
    print "$xt\n";
}

__END__

=head1 TITLE

dist-extensions - List file name extensions found in a CPAN repository.

=head1 SYNOPSIS

 dist-extensions
 dist-extensions -help
 dist-extensions -version

=head1 OPTIONS

=head2 -cpan

This option specifies the URL of the desired CPAN repository.

=head2 -help

This option displays the documentation for this script. The script then
exits.

=head2 -quiet

This option silences the informational messages about distributions
whose file extension can not be deciphered.

=head2 -version

This option displays the version of this script. The script then exits.

=head1 DETAILS

This Perl script feeds all the indexed distributions in a CPAN
repository to L<CPAN::DistnameInfo|CPAN::DistnameInfo> and lists the
unique file name extensions found in the repository. Informational
messages about distributions that do not have recognized extensions are
printed to standard error, and can be supressed with the C<-quiet>
option.

=head1 AUTHOR

Thomas R. Wyant, III F<wyant at cpan dot org>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2012 by Thomas R. Wyant, III

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl 5.10.0. For more details, see the full text
of the licenses in the directory LICENSES.

This program is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose.

=cut

# ex: set textwidth=72 :
