#!perl
use warnings;
use strict;
use YAML::XS 0.63 qw(DumpFile);
use File::Spec;
use Getopt::Long;
use Pod::Usage 1.69;
use CPAN::Smoker::Utils qw(is_distro_ok block_distro);
use CPAN::Smoker::Utils::PerlConfig;

our $VERSION = 'v1.0.1'; # VERSION
my ( $distro, $help, $version_check, $comment );

GetOptions(
    'help'      => \$help,
    'distro=s'  => \$distro,
    'comment=s' => \$comment,
    'version'   => \$version_check
) or pod2usage(1);

pod2usage( -exitval => 0, -verbose => 2 ) if $help;

if ($version_check) {
    print "dblock - version $VERSION\n\n";
    exit(0);
}

pod2usage( -exitval => 1, -verbose => 1 ) unless ( is_distro_ok($distro) );

my $perl_info = CPAN::Smoker::Utils::PerlConfig->new;

unless ( my $data_ref = block_distro( $distro, $perl_info->dump, $comment ) )
{
    exit(1);
}
else {
    my $path = delete( $data_ref->{full_path} );

    # to make sure group can write to distroprefs
    umask 002;
    DumpFile( $path, $data_ref );
}

__END__

=head1 dblock

dblock - program to create a distribution preferences file to prevent the smoker
from testing it

=head1 SYNOPSIS

    dblock --distro='ARFREITAS/Siebel-Srvrmgr'

=head1 OPTIONS

=over

=item B<--distro>

This parameter expects a string describing the Perl distribution to be disabled
regarding testing on the smoker. The string must have the format
C<AUTHOR/DISTRIBUTION>.

This is a required parameter: not providing it will cause the program to abort
with an error.

=item B<--help>

This help message.

=item B<--comment>

Adds a customized comment to the distroprefs file. It's entirely optional, and
if not given, the default text will be the string C<Tests hang smoker>.

Sometimes you want to add an distroprefs when, for example, the distribution
uses too much disk space, and an different message will help to identify that.

=item B<--version>

Prints the version number and exits.

=back

=head1 DESCRIPTION

This program creates a YAML file that describes a Perl distribution preferences
specifically to disable L<CPAN::Reporter> to skip trying to test the
distribution itself.

Sometimes this is required to be included for CPAN Smokers
(see L<CPAN::Reporter::Smoker>) because some distributions just cannot be
tested automatically and makes a smoker to hang during the tests, one will need
to manually terminate the related processes.

The YAML file will be created on the respective home directory of the user,
followed by C<.cpan> and C<prefs>.

See L<CPAN::Reporter> for details. You can check examples of those files on this
project webpage at Github. Those preferences files were created during several
tests executed.

=head1 AUTHOR

Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2017 of Alceu Rodrigues de Freitas Junior,
arfreitas@cpan.org

This file is part of CPAN Smoker Utils.

CPAN Smoker Utils is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

CPAN Smoker Utils 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.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with CPAN Smoker Utils.  If not, see L<http://www.gnu.org/licenses/>.

=cut
