#!/usr/bin/perl
use warnings;
use strict;

use Getopt::Long;
use Test::BrewBuild::Dispatch;

our $VERSION = '1.05_01';

my %args;
my $help;

GetOptions(
    "cmd=s"      => \$args{cmd},
    "testers=s@" => \$args{testers},
    "repo=s"     => \$args{repo},
    "debug=s"    => \$args{debug},
    "forks=i"    => \$args{forks},
    "help"       => \$help,
);

if ($help){
    print <<'EOF';
Usage: 

bbdispatch -t 127.0.0.1:7800 [-r https://github.com/user/repo] [-d 0-7]

EOF
exit;
}

$args{cmd} = 'brewbuild' if ! $args{cmd};

my $dispatcher = Test::BrewBuild::Dispatch->new(
    debug => $args{debug},
    forks => $args{forks},
);

my $return = $dispatcher->dispatch(%args);

print $return;

=pod

=head1 NAME

bbdispatch - Dispatch C<Test::BrewBuild> builds to remote test servers.

=head1 SYNOPSIS

Dispatch a default C<brewbuild> test run on the two listed remote servers,
using the repository you're currently working in:

    bbdispatch -t 10.0.0.1:7800 -t 10.0.0.2:9999

Dispatch a custom command to a single remote tester:

    bbdispatch -c "brewbuild -R" -t 10.0.0.1:7800

Have the remote testers test a specified repository:

    bbdispatch -r https://github.com/user/repo-name -t 10.0.0.1:7800

Add debugging to any other command string:

    bbdispatch [...] -d 0-7

Set maximum number of concurrent testers to communicate with (default is 4):

    bbdispatch [...] -f 8

=head1 DESCRIPTION

This script dispatches C<brewbuild> build sets to pre-configured
L<Test::BrewBuild::Tester> test servers.

=head1 COMMAND LINE OPTIONS

=head2 -t, --testers

A list of testers to dispatch to. Can be sent in multiple times. Format of the
testers is C<IP:Port>. If C<Port> is ommitted, we'll default to C<7800>.

Testers can also be configured within the config file. Testers set on the CLI
will override those.

=head2 -c, --cmd

By default, the testers will simply run C<brewbuild>. You can send in any
C<brewbuild> commands here, so long as the first argument is C<brewbuild>
liters.

This option will override anything in the configuration file.

=head2 -r, --repo

By default, if a repository isn't specified in the config file, we'll attempt
to locate the repository information from the current working directory.

Set this to the repo link to override both.

=head2 -d, --debug

Set to 0-7 to enable debug logging.

=head2 -f, --forks

By default, we maintain four (4) testers concurrently. You can change this by
sending in an inteter with this option.

=head1 AUTHOR

Steve Bertrand, C<< <steveb at cpan.org> >>

=head1 CONTRIBUTING

Any and all feedback and help is appreciated. A Pull Request is the preferred
method of receiving changes (L<https://github.com/stevieb9/p5-test-brewbuild>),
but regular patches through the bug tracker, or even just email discussions are
welcomed.

=head1 BUGS

L<https://github.com/stevieb9/p5-test-brewbuild/issues>

=head1 SUPPORT

You can find documentation for this script and its associated module with the
perldoc command.

    perldoc bbdispatch
    perldoc Test::BrewBuild::Dispatch

=head1 SEE ALSO

    perldoc brewbuild
    perldoc Test::BrewBuild

    perldoc bbtester
    perldoc Test::BrewBuild::Tester

=head1 LICENSE AND COPYRIGHT

Copyright 2016 Steve Bertrand.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See L<http://dev.perl.org/licenses/> for more information.

=cut

