#!perl

use 5.10.0;

use strict;
use warnings;

use autodie;

use Git::Repository;
use Pod::Usage;
use Getopt::Long;

my %opt;

my @orig_args = @ARGV;

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

my $module = $ARGV[0] || die("Usage: git cpan-init Foo::Bar\n");

Git::Repository->run("init");

my $repo = Git::Repository->new;

if ( not $opt{vcs} ) {
    say for $repo->run("cpan-import", @orig_args);

    say for $repo->run("checkout", "-f", "-t", "-b", "master", "cpan/master");
}
else {
    require Git::CPAN::Patch;
    my $gcp = Git::CPAN::Patch->new;
    $gcp->set_target( $module );

    my $repo = $gcp->repo;

    my $meta = $gcp->distribution_meta;
    my $external_repo = $meta->{resources}{repository}
        or die "distribution's META information doesn't contain a repository\n";

    die "type of repository not recognized\n" unless $external_repo->{type} eq 'git';

    say "repository found: $external_repo->{url}";

    local *STDERR;  # hush the warnings the easy way
    my $err;
    open STDERR, '>', \$err;

    say for $repo->run( 'remote', 'add', 'cpan', $external_repo->{url} );
    say for $repo->run( 'fetch', 'cpan' );
    say for $repo->run( 'checkout', '-f', '-t', '-b', 'master', 'cpan/master' );

}

__END__

=pod

=head1 NAME

git-cpan-init - Create a git repository for a CPAN module

=head1 SYNOPSIS

    git cpan-init [ --help | --man ] [ --backpan | --vcs ] [ Foo::Bar | Foo-Bar-0.03.tar.gz | http://... ]

=head1 DESCRIPTION

This command creates a new git repository, calls C<git-cpan-import> and then
checks out the code in the C<master> branch.

=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>, L<git-cpan-import>

=cut
