#!perl

our $DATE = '2015-09-18'; # DATE
our $VERSION = '0.14'; # VERSION

# NO_PERINCI_CMDLINE_SCRIPT
# FRAGMENT id=shcompgen-hint completer=1 for=cpanm

use 5.010001;
use strict;
use warnings;
use Log::Any::IfLOG '$log';

use Complete::File qw(complete_file);
use Complete::Util qw(complete_array_elem combine_answers);
use Getopt::Long::Complete qw(GetOptionsWithCompletion);

die "This script is for shell completion only\n"
    unless $ENV{COMP_LINE} || $ENV{COMMAND_LINE};

my $noop = sub {};

# complete with list of installed modules
my $comp_installed_mods = sub {
    require Complete::Module;

    my %args = @_;

    $log->tracef("Adding completion: installed modules");
    Complete::Module::complete_module(
        word => $args{word},
        path_sep => '::',
    );
};

# complete with installable stuff
my $comp_installable = sub {
    require Complete::Module;

    my %args = @_;
    my $word   = $args{word} // '';
    my $mirror = $args{mirror}; # XXX support multiple mirrors

    # if user already types something that looks like a path instead of module
    # name, like '../' or perhaps 'C:\' (windows) then don't bother to complete
    # with module name because it will just delay things without getting any
    # result.
    my $looks_like_completing_module =
        $word eq '' || $word =~ /\A(\w+)(::\w+)*(::)?\z/;

    my @answers;

    {
        $log->tracef("Adding completion: tarballs & dirs");
        my $answer = complete_file(
            filter => sub { /\.(zip|tar\.gz|tar\.bz2)$/i || (-d $_) },
            word   => $word,
        );
        $log->tracef("  answer: %s", {words=>$answer, path_sep=>'/'});
        push @answers, $answer;
    }

    if ($looks_like_completing_module) {
        $log->tracef("Adding completion: installed modules ".
                         "(e.g. when upgrading)");
        my $answer = Complete::Module::complete_module(
            word     => $word,
            path_sep => '::',
        );
        $log->tracef("  answer: %s", $answer);
        push @answers, $answer;
    }

    # currently we only complete from local CPAN (App::lcpan) if it's available.
    # for remote service, ideally we will need a remote service that quickly
    # returns list of matching PAUSE ids, package/module names, and dist names
    # (CPANDB, XPAN::Query, and MetaCPAN::Client are not ideal because the
    # response time is not conveniently quick enough). i probably will need to
    # setup such completion-oriented web service myself. stay tuned.
    {
        no warnings 'once';
        use experimental 'smartmatch';

        # completing an empty word still takes at least 3-4s here, due to almost
        # 10k results. so we skip it for now.
        last unless length($word);

        last unless $looks_like_completing_module;
        eval "use App::lcpan 0.32"; last if $@;
        $log->tracef("Adding completion: modules from local CPAN mirror");

        require Perinci::CmdLine::Util::Config;

        my %lcpanargs;
        my $res = Perinci::CmdLine::Util::Config::read_config(
            program_name => "lcpan",
        );
        unless ($res->[0] == 200) {
            $log->tracef("Can't get config for lcpan: %s", $res);
            last;
        }
        my $config = $res->[2];

        $res = Perinci::CmdLine::Util::Config::get_args_from_config(
            config => $config,
            args   => \%lcpanargs,
            #subcommand_name => 'update',
            meta   => $App::lcpan::SPEC{update},
        );
        unless ($res->[0] == 200) {
            $log->tracef("Can't get args from config: %s", $res);
            last;
        }
        App::lcpan::_set_args_default(\%lcpanargs);
        my $dbh = App::lcpan::_connect_db('ro', $lcpanargs{cpan}, $lcpanargs{index_name});
        my $sth;
        my $num_sep = 0; while ($word =~ /::/g) { $num_sep++ }
        if ($word eq '') {
            $sth = $dbh->prepare("SELECT name,has_child FROM namespace WHERE name='$word' OR (name LIKE '$word%' AND num_sep=$num_sep) ORDER BY name");
        } else {
            $sth = $dbh->prepare("SELECT name,has_child FROM namespace WHERE name LIKE '$word%' AND num_sep=$num_sep ORDER BY name");
        }
        $sth->execute;
        my @mods;
        while (my @row = $sth->fetchrow_array) {
            my $mod = $row[0];
            push @mods, $mod unless @mods ~~ $mod;
            if ($row[1]) {
                $mod .= '::';
                push @mods, $mod unless @mods ~~ $mod;
            }
        };
        #$log->tracef("  answer: %s", \@mods);
        push @answers, \@mods;
    }

    # TODO module name can be suffixed with '@<version>'

    combine_answers(@answers);
};

my $comp_file = sub {
    my %args = @_;

    complete_file(
        word => $args{word},
    );
};

# this is taken from App::cpanminus::script and should be updated from time to
# time.
GetOptionsWithCompletion(
    sub {
        my %args  = @_;
        my $type      = $args{type};
        my $word      = $args{word};
        if ($type eq 'arg') {
            $log->tracef("Completing arg");
            my $seen_opts = $args{seen_opts};
            if ($seen_opts->{'--uninstall'} || $seen_opts->{'--reinstall'}) {
                return $comp_installed_mods->(word=>$word);
            } else {
                return $comp_installable->(
                    word=>$word, mirror=>$seen_opts->{'--mirror'});
            }
        } elsif ($type eq 'optval') {
            my $ospec = $args{ospec};
            my $opt   = $args{opt};
            $log->tracef("Completing optval (opt=$opt)");
            if ($ospec eq 'l|local-lib=s' ||
                    $ospec eq 'L|local-lib-contained=s') {
                return complete_file(filter=>'d', word=>$word);
            } elsif ($ospec eq 'format=s') {
                return complete_array_elem(
                    array=>[qw/tree json yaml dists/], word=>$word);
            } elsif ($ospec eq 'cpanfile=s') {
                return complete_file(word=>$word);
            }
        }
        return [];
    },
    'f|force'   => $noop,
    'n|notest!' => $noop,
    'test-only' => $noop,
    'S|sudo!'   => $noop,
    'v|verbose' => $noop,
    'verify!'   => $noop,
    'q|quiet!'  => $noop,
    'h|help'    => $noop,
    'V|version' => $noop,
    'perl=s'          => $noop,
    'l|local-lib=s'   => $noop,
    'L|local-lib-contained=s' => $noop,
    'self-contained!' => $noop,
    'mirror=s@'       => $noop,
    'mirror-only!'    => $noop,
    'mirror-index=s'  => $noop,
    'cpanmetadb=s'    => $noop,
    'cascade-search!' => $noop,
    'prompt!'         => $noop,
    'installdeps'     => $noop,
    'skip-installed!' => $noop,
    'skip-satisfied!' => $noop,
    'reinstall'       => $noop,
    'interactive!'    => $noop,
    'i|install'       => $noop,
    'info'            => $noop,
    'look'            => $noop,
    'U|uninstall'     => $noop,
    'self-upgrade'    => $noop,
    'uninst-shadows!' => $noop,
    'lwp!'    => $noop,
    'wget!'   => $noop,
    'curl!'   => $noop,
    'auto-cleanup=s' => $noop,
    'man-pages!' => $noop,
    'scandeps'   => $noop,
    'showdeps'   => $noop,
    'format=s'   => $noop,
    'save-dists=s' => $noop,
    'skip-configure!' => $noop,
    'dev!'       => $noop,
    'metacpan!'  => $noop,
    'report-perl-version!' => $noop,
    'configure-timeout=i' => $noop,
    'build-timeout=i' => $noop,
    'test-timeout=i' => $noop,
    'with-develop' => $noop,
    'without-develop' => $noop,
    'with-feature=s' => $noop,
    'without-feature=s' => $noop,
    'with-all-features' => $noop,
    'pp|pureperl!' => $noop,
    "cpanfile=s" => $noop,
    #$self->install_type_handlers,
    #$self->build_args_handlers,
);

# ABSTRACT: Shell completer for cpanm
# PODNAME: _cpanm

__END__

=pod

=encoding UTF-8

=head1 NAME

_cpanm - Shell completer for cpanm

=head1 VERSION

This document describes version 0.14 of _cpanm (from Perl distribution App-ShellCompleter-cpanm), released on 2015-09-18.

=head1 SYNOPSIS

To install, install this module and then in your bash (and/or bash startup
file):

 complete -C _cpanm cpanm

or, you can use L<shcompgen> to do that for you automatically.

Now L<cpanm> has bash completion:

 % cpanm --s<tab>
 % cpanm --uninstall "Text::A<tab>

=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 _cpanm _cpanm

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 _cpanm 'p/*/`_cpanm`/'

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/App-ShellCompleter-cpanm>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-BashCompleter-cpanm>.

=head1 BUGS

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

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
