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

use Getopt::Long qw/GetOptions/;
use PAR::Dist::FromCPAN;

our $VERSION = '0.03';

=pod

=head1 NAME

cpan2par - Create PAR distributions from CPAN

=head1 SYNOPSIS

cpan2par --help

cpan2par [-v -f -t -o DIR] -p MODULEPATTERN

=head1 DESCRIPTION

This script creates PAR distributions from any number of modules
from CPAN. The only mandatory parameter is a pattern matching the
modules you wish to create PAR distributions from. This works the
same way as, for example C<cpan install MODULEPATTERN>.

=head2 Parameters

  -v --verbose
    Sets the verbose mode.
  -o --out
    Sets the output directory. (default: .)
  -f --follow
    Follow dependencies and recursively generate PAR distributions.
  -t --test
    Indicates whether the modules' tests will be run before the PAR
    distribution is created.
  -m --merge
    Merge all modules into the main PAR distribution instead
    of creating separate .par files for each dependency.
    (This step is carried out at the end. If something goes wrong,
     it will be skipped.)
  --no-docs
    Strip all documentation (man pages, html documentation) from the
    resulting PAR distribution.
    (This step is carried out at the end. If something goes wrong,
     it will be skipped.)
  -s --skip
    Skip modules that match this regex. (May be specified multiple
    times.)

=head1 SEE ALSO

This tool is implemented using the L<PAR::Dist::FromCPAN> module. Please
refer to that module's documentation for details on how this all works.

PAR has a mailing list, <par@perl.org>, that you can write to; send
an empty mail to <par-subscribe@perl.org> to join the list and
participate in the discussion.

Please send bug reports to <bug-par-dist-fromcpan@rt.cpan.org>.

The official PAR website may be of help, too: http://par.perl.org

=head1 AUTHOR

Steffen Mueller, E<lt>smueller at cpan dot orgE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2006 by Steffen Mueller

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.6 or,
at your option, any later version of Perl 5 you may have available.

=cut

my $usage = <<"HERE";
$0 --help          # for help

$0 [-v -f -t -o DIR -m --no-docs] -p MODULEPATTERN

This script creates PAR distributions from any number of modules
from CPAN. The only mandatory parameter is a pattern matching the
modules you wish to create PAR distributions from. This works the
same way as, for example "cpan install MODULEPATTERN".

-v --verbose
  Sets the verbose mode.
-o --out
  Sets the output directory. (default: .)
-f --follow
  Follow dependencies and recusrively generate PAR distributions.
-t --test
  Indicates whether the modules' tests will be run before the PAR
  distribution is created.
-p --pattern
  The module pattern to use.
-m --merge
  Merge all modules into the main PAR distribution instead
  of creating separate .par files for each dependency.
  (This step is carried out at the end. If something goes wrong,
   it will be skipped.)
--no-docs
  Strip all documentation (man pages, html documentation) from the
  resulting PAR distribution.
  (This step is carried out at the end. If something goes wrong,
   it will be skipped.)
-s --skip
  Skip modules that match this regex. (May be specified multiple
  times.)
HERE

my $pattern;
my $outdir = '.';
my $follow = 0;
my $run_tests = 0;
my $v = 0;
my $merge = 0;
my $nodocs = 0;
my @skip;
GetOptions(
	'h|help' => sub { print $usage; exit(1) },
	'o|out=s' => \$outdir,
	'p|pattern=s' => \$pattern,
	'f|follow' => \$follow,
	'v|verbose' => \$v,
	't|test' => \$run_tests,
	'm|merge' => \$merge,
	'no-docs' => \$nodocs,
	's|skip=s' => \@skip,
);

cpan_to_par(
	pattern => $pattern,
	($follow          ? (follow     => 1      ) : ()),
	($v               ? (verbose    => 1      ) : ()),
	($run_tests       ? (test       => 1      ) : ()),
	(defined($outdir) ? (out        => $outdir) : ()),
	($merge           ? (merge      => 1      ) : ()),
	($nodocs          ? (strip_docs => 1      ) : ()),
	(@skip            ? (skip       => \@skip ) : ()),
);

