#!/usr/bin/env perl

use strict;
use warnings;

use autodie;

use Git;
use Pod::Usage;
use Parse::BACKPAN::Packages;
use Getopt::Long;

sub say (@) { print @_, "\n" }

my %opt;
GetOptions( \%opt, 
    'help' => sub { pod2usage(1) },
    'man'  => sub { pod2usage( verbose => 2 ) },
    'mkdir:s', 
    'force!'
) or pod2usage( "for a list of all valid options, do 'git backpan-init --help'" );

my $module = shift or pod2usage( 'module name required' );

# Backpan takes its modules with a '-', not '::'s
$module =~ s/::/-/g;

if ( defined $opt{mkdir} ) {
    ( my $dirname = $opt{mkdir} || lc $module ) =~ s/::/-/g;
    say "creating directory $dirname";
    mkdir $dirname;
    chdir $dirname;
}

if ( -d '.git' ) {
    unless ( $opt{force} ) {
        die "Aborting: git repository already present.\n",
            "use '-force' if it's really what you want to do\n";
    }
}
else {
    Git::command_noisy('init');
}

my $repo = Git->repository;

say "opening backpan index";
my $backpan = Parse::BACKPAN::Packages->new;

my @dists = $backpan->distributions($module)
    or die "Error: no distributions found. ",
            "Are you sure you spelled the module name correctly?\n";

foreach my $dist ( @dists ) {
     say "importing " . $dist->distvname;
     my $url  = 'http://backpan.perl.org/' . $dist->prefix;
     $repo->command_noisy('cpan-import' => $url);
}

$repo->command_noisy('checkout', '-t', '-b', 'master', 'cpan/master');

__END__

=pod

=head1 NAME

git-backpan-init - Initialize a repository for a CPAN module with full history
from the backpan.

=head1 SYNOPSIS

    git backpan-init [ --help | --man ] [ --mkdir <directory> ] [ --force ] Foo::Bar

=head1 DESCRIPTION

This command is like C<cpan-init> except it imports all the historical versions
of a module as well, allowing you to run C<git bisect> or to browse history.

The name of the module can be written with double-colons or dashes (i.e.,
I<Foo::Bar> or I<Foo-Bar>).

Unless the I<-force> option is used, the command will abort if a git repository
is found to be already present in the directory.

=head1 OPTIONS

=over

=item --mkdir I<directory>

Creates a sub-directory and create the git repository there instead 
than in the current directory.  If no directory name is given,
figures one out based on the name of the module.

=item --force, -f

Forces the imports, even if a git repository was
already present in the directory.

=item --help, -h

Prints the synopsis and valid options and exits.

=item --man

Prints the man page and exits.

=back

=head1 EXAMPLES

Most of the time, you want to use I<git backpan-init> like this:

    % cd ~/work/
    % git backpan-init -mkdir Foo::Bar

=head1 VERSION

This document describes git-backpan-init version 0.1.3

=head1 BUGS AND LIMITATIONS

Please report any bugs or feature requests to
C<bug-git-cpan-patch@rt.cpan.org>, or through the web 
interface at L<http://rt.cpan.org>.
  
=head1 AUTHORS

Yanick Champoux C<< <yanick@cpan.org> >>

Yuval Kogman C<< <nothingmuch@woobling.org> >>

=head1 LICENCE

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.

=head1 SEE ALSO

L<Git::CPAN::Patch>

=cut
