#!/usr/bin/perl

# Created on: 2014-03-11 20:58:59
# Create by:  Ivan Wills
# $Id$
# $Revision$, $HeadURL$, $Date$
# $Revision$, $Source$, $Date$

use strict;
use warnings;
use Getopt::Long;
use Pod::Usage ();
use Data::Dumper qw/Dumper/;
use English qw/ -no_match_vars /;

our $VERSION = 0.6;
my ($name)   = $PROGRAM_NAME =~ m{^.*/(.*?)$}mxs;

my %option = (
    verbose => 0,
    man     => 0,
    help    => 0,
    VERSION => 0,
);

main();
exit 0;

sub main {

    Getopt::Long::Configure('bundling');
    GetOptions(
        \%option,
        'remote|r',
        'all|a',
        'sort|s',
        'insensitive|i',
        'context|C=i',
        'before|B=i',
        'after|A=i',
        'verbose|v+',
        'man',
        'help',
        'VERSION!',
    ) or Pod::Usage::pod2usage(2);

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

    # do stuff here
    my @options;
    push @options, '-r' if $option{remote};
    push @options, '-a' if $option{all};
    my $cmd = join ' ', qw/git branch/, @options;
    my @grep_options;
    push @grep_options, '-i'                  if $option{insensitive};
    push @grep_options, "-C $option{context}" if $option{context};
    push @grep_options, "-B $option{before}"  if $option{before};
    push @grep_options, "-A $option{after}"   if $option{after};
    my $grep = join ' ', @grep_options;

    print "$cmd | grep -P $grep '$ARGV[0]'\n" if $option{verbose} || $option{test};
    return exec "$cmd | grep -P $grep '$ARGV[0]'" if !$option{sort};

    print sort {sorter()} `$cmd | grep -P $grep '$ARGV[0]'`;
}

sub sorter {
    no warnings;
    my $A = $a;
    my $B = $b;
    $A =~ s/(\d+)/sprintf "%06d", $1/egxms;
    $B =~ s/(\d+)/sprintf "%06d", $1/egxms;
    $A cmp $B;
}

__DATA__

=head1 NAME

git-branch-grep - grep for branch names

=head1 VERSION

This documentation refers to git-branch-grep version 0.6

=head1 SYNOPSIS

   git-branch-grep [--remote|-r|--all|-a] regex

 OPTIONS:
  regex         grep's perl (-P) regular expression
  -r --remote   List all remote branches
  -a --all      List all branches

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

=head1 DESCRIPTION

Short hand for running

C<git branch (-r|-a)? | grep -P 'regex'>

=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) 2014 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
