#!perl

our $DATE = '2015-01-13'; # DATE
our $VERSION = '0.01'; # VERSION

use 5.010;
use strict;
use warnings;

use Perinci::CmdLine::Any -prefer_lite=>1;

my $prefix = '/CPAN/Local/';

Perinci::CmdLine::Any->new(
    url => $prefix,
    log => 1,
    subcommands => {
        update          => {url=>"${prefix}update_local_cpan"},
        'update-index'  => {url=>"${prefix}update_local_cpan_index"},
        'update-files'  => {url=>"${prefix}update_local_cpan_files"},

        stats      => {url=>"${prefix}stat_local_cpan"},

        authors    => {url=>"${prefix}list_local_cpan_authors"},
        modules    => {url=>"${prefix}list_local_cpan_modules"},
        dists      => {url=>"${prefix}list_local_cpan_dists"},
        deps       => {url=>"${prefix}list_local_cpan_deps"},
        'rev-deps' => {url=>"${prefix}list_local_cpan_rev_deps"},
        #search => {url=>""},
    },
)->run;

# ABSTRACT: Manage your local CPAN mirror
# PODNAME: lcpan

__END__

=pod

=encoding UTF-8

=head1 NAME

lcpan - Manage your local CPAN mirror

=head1 VERSION

This document describes version 0.01 of lcpan (from Perl distribution CPAN-Local), released on 2015-01-13.

=head1 SYNOPSIS

=head2 Update (download and index) your local CPAN mirror

 % lcpan update

A mini CPAN mirror will be downloaded in your C<~/cpan> directory. This will
take a while (it downloads +- 4GB of files, as of this writing; subsequent
update will of course take shorter time.)

A SQLite database is also created/updated in C<~/cpan/index.db> which contains
information about authors, modules/packages, distributions. This database is
used for quickly answer various queries.

You can run the above command e.g. daily to keep your mirror up-to-date.

=head2 Using your local CPAN mirror to install modules

 % cpanm --mirror ~/cpan --mirror-only -n Some::Module

You can also set up a shell alias to save typing the options all the time, e.g.:

 alias lcpanm="cpanm --mirror ~/cpan --mirror-only -n"

Then you can install modules simply using:

 % lcpanm Some::Module

=head2 Querying your local CPAN mirror

Info about your local CPAN mirror:

 % lcpan stats
 +------------------+----------------------+
 | name             | value                |
 +------------------+----------------------+
 | authors          | 11967                |
 | distributions    | 29907                |
 | last-updated     | 2015-01-09T10:40:55Z |
 | modules          | 13365                |
 | packages         | 133650               |
 +------------------+----------------------+

Add C<--verbose> if you want more stats which normally are skipped because they
can take a while to get (e.g. disk-space).

B<List modules:>

 % lcpan modules

List modules matching some query (name):

 % lcpan modules smtp

List modules by certain author, show detail instead of just module names:

 % lcpan modules --author PERLANCAR --detail

List modules from a certain dist:

 % lcpan modules --dist libwww-perl --detail

B<List distributions:>

 % lcpan dists

List distributions matching some query (name, filename, abstract), show detail
instead of just distribution name:

 % lcpan dists critic --detail

List distributions released by a certain author:

 % lcpan dists --author PERLANCAR

B<List authors:>

 % lcpan authors

List authors matching some query (PAUSE ID, name, email), show detail instead of
just ID:

 % lcpan authors tatsuhiko --detail

B<Show dependencies and reverse dependencies>:

 # which modules does Text::ANSITable depend on?
 % lcpan deps Text::ANSITable

 # do it recursively
 % lcpan deps Text::ANSITable -R

 # which distributions depend on Text::ANSITable?
 % lcpan rev-deps Text::ANSITable

=head1 DESCRIPTION

This application is a convenient bundling of C<CPAN::Mini> and an indexer so in
addition to creating your local CPAN mirror and installing modules from it, you
can also query various things about your local CPAN mirror quickly from the
command-line (as well as programmatically).

=head1 SUBCOMMANDS

=head2 B<authors>

List authors in local CPAN.

=head2 B<deps>

List dependencies of a module, data from local CPAN.

=head2 B<dists>

List distributions in local CPAN.

=head2 B<modules>

List packages in local CPAN.

=head2 B<rev-deps>

List reverse dependencies of a module, data from local CPAN.

=head2 B<stats>

=head2 B<update>

=head2 B<update-files>

=head2 B<update-index>

=head1 OPTIONS

C<*> marks required options.

=head2 Common options

=over

=item B<--config-path>=I<s>

Set path to configuration file.

Can be specified multiple times.

=item B<--config-profile>=I<s>

Set configuration profile to use.

=item B<--debug>

Set log level to debug.

=item B<--format>=I<s>

Choose output format, e.g. json, text.

=item B<--help>, B<-h>, B<-?>

Display this help message.

=item B<--json>

Set output format to json.

=item B<--log-level>=I<s>

Set log level.

=item B<--naked-res>

When outputing as JSON, strip result envelope.

By default, when outputing as JSON, the full enveloped result is returned, e.g.:

    [200,"OK",[1,2,3],{"func.extra"=>4}]

The reason is so you can get the status (1st element), status message (2nd
element) as well as result metadata/extra result (4th element) instead of just
the result (3rd element). However, sometimes you want just the result, e.g. when
you want to pipe the result for more post-processing. In this case you can use
`--naked-res` so you just get:

    [1,2,3]


=item B<--no-config>

Do not use any configuration file.

=item B<--quiet>

Set log level to quiet.

=item B<--subcommands>

List available subcommands.

=item B<--trace>

Set log level to trace.

=item B<--verbose>

Set log level to info.

=item B<--version>, B<-v>

=back

=head2 Options for subcommand authors

=over

=item B<--cpan>=I<s>

Location of your local CPAN mirror, e.g. /path/to/cpan.

Defaults to C<~/cpan>.


=item B<--detail>

=item B<--query>=I<s>, B<-q>

Search query.

=back

=head2 Options for subcommand deps

=over

=item B<--cpan>=I<s>

Location of your local CPAN mirror, e.g. /path/to/cpan.

Defaults to C<~/cpan>.


=item B<--include-core>

Include Perl core modules.

=item B<--level>=I<i>, B<-l>

Recurse for a number of levels (-1 means unlimited).

Default value:

 1

=item B<--module>=I<s>*

=item B<--perl-version>=I<s>, B<-V>

Set base Perl version for determining core modules.

Default value:

 "v5.18.4"

=item B<--phase>=I<s>

Default value:

 "runtime"

Valid values:

 ["develop", "configure", "build", "runtime", "test", "ALL"]

=item B<--rel>=I<s>

Default value:

 "requires"

Valid values:

 ["requires", "recommends", "suggests", "conflicts", "ALL"]

=item B<-R>

Recurse (alias for `--level -1`).

See C<--level>.

=back

=head2 Options for subcommand dists

=over

=item B<--author>=I<s>, B<-a>

Filter by author.

=item B<--cpan>=I<s>

Location of your local CPAN mirror, e.g. /path/to/cpan.

Defaults to C<~/cpan>.


=item B<--detail>

=item B<--query>=I<s>, B<-q>

Search query.

=back

=head2 Options for subcommand modules

=over

=item B<--author>=I<s>, B<-a>

Filter by author.

=item B<--cpan>=I<s>

Location of your local CPAN mirror, e.g. /path/to/cpan.

Defaults to C<~/cpan>.


=item B<--detail>

=item B<--dist>=I<s>, B<-d>

Filter by distribution.

=item B<--query>=I<s>, B<-q>

Search query.

=back

=head2 Options for subcommand rev-deps

=over

=item B<--cpan>=I<s>

Location of your local CPAN mirror, e.g. /path/to/cpan.

Defaults to C<~/cpan>.


=item B<--module>=I<s>*

=back

=head2 Options for subcommand stats

=over

=item B<--cpan>=I<s>

Location of your local CPAN mirror, e.g. /path/to/cpan.

Defaults to C<~/cpan>.


=back

=head2 Options for subcommand update

=over

=item B<--cpan>=I<s>

Location of your local CPAN mirror, e.g. /path/to/cpan.

Defaults to C<~/cpan>.


=back

=head2 Options for subcommand update-files

=over

=item B<--cpan>=I<s>

Location of your local CPAN mirror, e.g. /path/to/cpan.

Defaults to C<~/cpan>.


=item B<--max-file-size>=I<i>

=item B<--remote-url>=I<s>

=back

=head2 Options for subcommand update-index

=over

=item B<--cpan>=I<s>

Location of your local CPAN mirror, e.g. /path/to/cpan.

Defaults to C<~/cpan>.


=back

=head1 ENVIRONMENT

LCPAN_OPT

=head1 FILES

~/lcpan.conf

/etc/lcpan.conf

=head1 FAQ

=head1 SEE ALSO

=head1 COMPLETION

This script has shell tab completion capability with support for several shells.

=head2 bash

To activate bash completion for this script, put:

 complete -C lcpan lcpan

in your bash startup (e.g. C<~/.bashrc>). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.

It is recommended, however, that you install L<shcompgen> which allows you to activate completion scripts for several kinds of scripts on multiple shells. Some CPAN distributions (those that are built with L<Dist::Zilla::Plugin::GenShellCompletion>) will even automatically enable shell completion for their included scripts (using C<shcompgen>) at installation time, so you can immadiately have tab completion.

=head2 tcsh

To activate tcsh completion for this script, put:

 complete lcpan 'p/*/`lcpan`/'

in your tcsh startup (e.g. C<~/.tcshrc>). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.

It is also recommended to install C<shcompgen> (see above).

=head2 other shells

For fish and zsh, install C<shcompgen> as described above.

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/CPAN-Local>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-CPAN-Local>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=CPAN-Local>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
