#!/usr/bin/perl

# Created on: 2013-05-04 16:16:56
# Create by:  Ivan Wills
# $Id$
# $Revision$, $HeadURL$, $Date$
# $Revision$, $Source$, $Date$

use strict;
use warnings;
use version;
use Getopt::Alt qw/get_options/;
use Pod::Usage;
use Data::Dumper qw/Dumper/;
use English qw/ -no_match_vars /;
use FindBin qw/$Bin/;
use Path::Class;
use Group::Git;
use Config::Any;

our $VERSION = version->new('0.0.4');
my ($name)   = $PROGRAM_NAME =~ m{^.*/(.*?)$}mxs;


main();
exit 0;

sub main {

    my $opt = get_options(
        { sub_command => 1, },
        [
            'local|l!',
            'test|t!',
            'verbose|v+',
        ]
    );

    if ( $opt->opt->VERSION ) {
        print "$name Version = $VERSION\n";
        exit 1;
    }
    elsif ( $opt->opt->man ) {
        pod2usage( -verbose => 2 );
    }
    elsif ( $opt->opt->help ) {
        pod2usage( -verbose => 1 );
    }

    # do stuff here
    my $conf = Config::Any->load_stems({
        stems   => [ qw/group group-git/ ],
        use_ext => 1,
    });
    $conf = {
        map { %$_        }
        map { values %$_ }
        @{$conf}
    };

    my $module = $conf->{type} && !$opt->opt->local ? 'Group::Git::' . ucfirst $conf->{type} : 'Group::Git';
    my $file   = "$module.pm";
    $file =~ s{::}{/}g;
    require $file;
    my $group = $module->new(
        %{ $opt->opt },
        conf => $conf,
    );

    my $action = $opt->cmd;

    for my $project ( sort {lc $a cmp lc $b} keys %{ $group->repos } ) {
        print "\n$project\n" if $opt->opt->verbose;
        $group->$action($project) if $action;
    }

    return;
}

__DATA__

=head1 NAME

group-git - Perform operations over many git repositories at once

=head1 VERSION

This documentation refers to group-git version 0.0.4.

=head1 SYNOPSIS

   group-git [option] (git-command) -- [args]

 OPTIONS:
  -l --local    Ignore any management type and just look at what is checked out
                Only useful if you have a group-git.yml file defining Github,
                Bitbucked or Gitosis types.

  -v --verbose  Show more detailed option
     --VERSION  Prints the version information
     --help     Prints this help information
     --man      Prints the full documentation for group-git

  eg
   group-git log -- -n 1
   group-git --local status
   group-git --verbose grep 'where is it'

=head1 DESCRIPTION

Run any git command over all repositories in the current directory. If you
have a config file set up in the current directory (group-git.yml or some
other suffix group-git file supported by L<Config::Any>) some extra functionality
becomes available in particular both pull and update will clone any missing
repositories.

 eg group-git.yml
 ---
 # Type may be any of Github, Bitbucket or Gitosis
 type: Github
 # if username or password are missing they will be prompted for
 username: joeblogs@gmail.com
 password: securepass
 # Gitosis uses it's git url which needs to be defined or will be prompted for
 #gitosis: gituser@gitosis.server.com:gitosis-admin.git

=head1 SUBROUTINES/METHODS

=head1 DIAGNOSTICS

=head1 CONFIGURATION AND ENVIRONMENT

=head1 DEPENDENCIES

=head1 INCOMPATIBILITIES

=head1 BUGS AND LIMITATIONS

There are no known bugs in this module.

Please report problems to Ivan Wills (ivan.wills@gmail.com).

Patches are welcome.

=head1 AUTHOR

Ivan Wills - (ivan.wills@gmail.com)

=head1 LICENSE AND COPYRIGHT

Copyright (c) 2013 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
All rights reserved.

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