#!perl
use strict;
use warnings;
use FindBin;
# A local::lib installation must remain reachable after "pperlbrew use"
# changes PERL5LIB. System installations use the installing Perl's @INC.
use if -f "$FindBin::RealBin/../lib/perl5/App/pperlbrew.pm",
    'lib', "$FindBin::RealBin/../lib/perl5";
use App::pperlbrew;

App::pperlbrew->run_cli("$FindBin::RealBin/$FindBin::RealScript", @ARGV);

__END__

=head1 NAME

pperlbrew - Manage parallel installations of Perl 5, PetaPerl and PerlOnJava.

=head1 SYNOPSIS

pperlbrew command syntax:

    pperlbrew <command> [options] [arguments]

Commands:

    init           Initialize pperlbrew environment.
    info           Show useful information about the pperlbrew installation

    install        Install Perl 5, PetaPerl or PerlOnJava
    uninstall      Uninstall the given installation
    available      List perls available to install
    lib            Manage local::lib directories.
    alias          Give perl installations a new name
    upgrade-perl   Upgrade the current perl

    list           List perl installations
    use            Use the specified perl in current shell
    off            Turn off pperlbrew in current shell
    on             Turn on the installation in place when 'off' was run
    switch         Permanently use the specified perl as default
    switch-off     Permanently turn off pperlbrew (revert to system perl)
    exec           Execute programs with specified perl environments.

    list-modules   List installed CPAN modules for the current Perl version in use
    clone-modules  Re-installs all CPAN modules from one installation to another

    self-install       Install pperlbrew itself under PPERLBREW_ROOT/bin
    self-upgrade       Upgrade pperlbrew itself.

    install-patchperl  Install patchperl
    install-cpanm      Install cpanm, a friendly companion.
    install-cpm        Install cpm, a faster but still friendly companion.
    install-multiple   Install multiple versions and flavors of perl

    download       Download the specified perl distribution tarball.
    clean          Purge tarballs and build directories
    env            Print the environment for a selection
    symlink-executables  Link versioned Perl 5 executables to short names
    make-shim      Create a wrapper for a program in a managed installation
    make-pp        Package a program using PAR::Packer
    version        Display version
    help           Read more detailed instructions

Generic command options:

    -q --quiet     Be quiet on informative output message.
    -v --verbose   Tell me more about it.

Run C<pperlbrew help> for the full manual or C<pperlbrew help COMMAND> for one command.

=head1 INSTALLATION

With an existing Perl, install the PETAMEM distribution through a CPAN client,
then initialize the shell integration.

    cpanm App::pperlbrew
    pperlbrew init
    . "$HOME/perl5/pperlbrew/etc/bashrc"

To install from a downloaded distribution, give C<cpanm> the path to the tarball.
The manager continues to run with the Perl that installed it when you switch
interpreters. Keep that Perl and its installed modules available.

On Linux without Perl, the installer downloads the newest plain static-min
PetaPerl runtime for your CPU from the PetaPerl checksum index. It needs a shell, curl, base64, tar with gzip,
sha256sum and standard Linux utilities. Run it as your normal user.

    curl -fsSL https://perl.petamem.com/downloads/pperlbrew/install.sh | sh

For a locally staged release, run this from the checkout instead.

    sh dist/install.sh

Bootstrap installation requires a compatible static-min build for the detected
Linux architecture. Follow the activation line printed by the
installer. The private runtime stays available after C<pperlbrew off>.

=head2 Moving from perlbrew

The Linux bootstrap stops before fetching a runtime or writing files if
it detects perlbrew. Record your builds and uninstall perlbrew first, or
pass C<--force> to install alongside it in separate directories. The
installer leaves the existing perlbrew installation and its builds intact.

    curl -fsSL https://perl.petamem.com/downloads/pperlbrew/install.sh | sh -s -- --force
    # With a downloaded installer
    sh install.sh --force

Use pperlbrew as your sole Perl manager, including for Perl 5. Both managers
alter C<PATH> and can restore a saved Perl selection in new shells. Record
the builds you need with C<perlbrew list>, remove the old perlbrew activation
line from your shell startup file, and open a fresh login shell with only
pperlbrew activated. Install your required builds in pperlbrew's own root.

Before each command, pperlbrew checks for an active perlbrew shell environment,
an executable on C<PATH>, or an installation in C<PERLBREW_ROOT> or
C<~/perl5/perlbrew>. It warns on standard error even when perlbrew is installed
but inactive. The command still runs.

=head1 CONFIGURATION

=over 4

=item PPERLBREW_ROOT

The installation root defaults to C<~/perl5/pperlbrew>. To use a different
root, set this to an absolute path before running the installer or
C<pperlbrew init>. The root holds managed interpreters in C<perls/>, cached
downloads in C<dists/>, shell files in C<etc/> and bootstrap payloads in
C<libexec/>. If users share a root, anyone with write access can change the
executables it provides.

=item PPERLBREW_HOME

The per-user settings directory defaults to C<~/.pperlbrew>. It contains the
saved shell selection and named local module libraries. Set this before
initialization and activation to use another directory.

=item PPERLBREW_NO_PROFILE

Set to C<1> when running the bootstrap to manage your own shell startup file.
Source the activation file in each shell that should support switching.

=item PPERLBREW_RELEASE_URL

Base URL for downloading C<install.sh> during self-upgrade. Defaults to
C<https://perl.petamem.com/downloads/pperlbrew>.

=item PPERLBREW_RUNTIME_URL

Directory containing PetaPerl's C<SHA256SUMS> and runtime binaries. Defaults
to C<https://perl.petamem.com/downloads>. A local mirror can use a C<file://> URL.

=item PPERLBREW_CONFIGURE_FLAGS

Flags passed to C<sh Configure> for a Perl 5 source build. The default is C<-de>.

=item PPERLBREW_CPAN_MIRROR

The CPAN mirror for Perl 5 source downloads defaults to C<https://cpan.metacpan.org>.

=back

=head1 COMMAND: HELP

Usage: pperlbrew help [command]

Read the full manual or the section for a single command.

=head1 COMMAND: INIT

Usage: pperlbrew init

Initialize the manager root and generate shell integration files. Run this
after a CPAN install or upgrade. The Linux bootstrap runs it automatically.
Source the corresponding file in your current shell and add the same line
to its startup file for future sessions. With the default root, use one of:

    # sh, bash or zsh
    . "$HOME/perl5/pperlbrew/etc/bashrc"
    # fish
    source "$HOME/perl5/pperlbrew/etc/pperlbrew.fish"
    # csh or tcsh
    source "$HOME/perl5/pperlbrew/etc/cshrc"

C<pperlbrew init -> prints the shell integration for Bash or zsh.

=head1 COMMAND: INFO

=over 4

=item B<info> [module]

Usage: pperlbrew info [ <module> ]

Display useful information about the pperlbrew installation.

If a module is given the version and location of the module is displayed.

=back

=head1 COMMAND: INSTALL

Usage:

    pperlbrew install pperl-0.6.25
    pperlbrew install pperl-0.6.25 --as my-build --from /path/to/pperl
    pperlbrew install jperl-5.44.1
    pperlbrew install [options] <perl-release>
    pperlbrew install [options] /path/to/perl-5.14.0.tar.gz
    pperlbrew install [options] /path/to/perl/git/checkout/dir
    pperlbrew install [options] https://example.com/mirror/perl-5.12.3.tar.gz

C<pperl-VERSION> installs a prebuilt PetaPerl for this machine.
C<--from FILE> installs a local pperl executable under the requested name.
C<jperl-VERSION> builds the tagged PerlOnJava release with Gradle and a JDK (24 or later for 5.44).
5.44.1 supplies a Gradle wrapper. 5.44.0 needs C<gradle> on PATH. Use C<-n> to skip its test suite.
C<--as NAME> keeps different versions or named copies side by side.
Perl 5 configure flags such as C<--thread> and C<--64int> apply to Perl 5.
In jperl 5.44.1, ithreads and 64-bit integers are built in. Set
C<JPERL_THREAD_MODE=platform> to use platform threads in place of virtual
threads, or pass C<--interpreter> to jperl to select its interpreter backend.
Both PetaPerl and PerlOnJava installations provide C<perl> and their native command in the selected bin directory.

Build and install the wanted perl. The last argument can be a short string designating a specific version which can be known from the output of C<pperlbrew available>, a path to a pre-downloaded tarball, a path to a git-checkout of perl5 repo, or a URL to a tarball.

Supported <perl-release> format include:

=over 4

=item perl-<version>

=item perl-stable

=item perl-blead

=item <version>

=item stable

=item blead

=back

Version numbers typically follows the "5.xx.xx" format, or "perl-5.xx.xx-RCxx" for
release candidates.

Version "stable" is a special token that means whatever the latest
stable version is at the moment.

Version "blead" is also a special token that means whatever the latest
version in the repository, which is downloaded from this specific URL
regardless of mirror settings:

    https://github.com/Perl/perl5/archive/blead.tar.gz

The specified perl is downloaded from the official CPAN website or from the
mirror site configured before.

Specifying a version number without the 'perl-' prefix refers to the
official release provided by perl5 porters.

Options for C<install> command:

    -f --force     Force installation
    -j $n          Parallel building and testing. ex. C<pperlbrew install -j 5 perl-5.14.2>
    -n --notest    Skip testing

       --switch    Automatically switch to this Perl once successfully
                   installed, as if with `pperlbrew switch <version>`

       --as        Install the given version of perl by a name.
                   ex. C<pperlbrew install perl-5.6.2 --as legacy-perl>

       --noman     Skip installation of manpages

       --thread    Build perl with usethreads enabled
       --multi     Build perl with usemultiplicity enabled
       --64int     Build perl with use64bitint enabled
       --64all     Build perl with use64bitall enabled
       --ld        Build perl with uselongdouble enabled
       --debug     Build perl with DEBUGGING enabled
       --clang     Build perl using the clang compiler
       --no-patchperl
                   Skip calling patchperl

    -D,-U,-A       Switches passed to perl Configure script.
                   ex. C<pperlbrew install perl-5.10.1 -D usemymalloc -U versiononly>

    --destdir $path
                   Install perl as per 'make install DESTDIR=$path'

    --sitecustomize $filename
                   Specify a file to be installed as sitecustomize.pl

By default, Perl 5 source installations are configured after their name like this:

    sh Configure -de -Dprefix=$PPERLBREW_ROOT/perls/<name>

=head1 COMMAND: INSTALL-MULTIPLE

Usage: pperlbrew install-multiple [options] <perl-version-1> <perl-version-2> ...

Build and install the given versions of perl.

C<install-multiple> accepts the same set of options as the command
C<install> plus the following ones:

    --both $flavor       Where $flavor is one of C<thread>, C<multi>, C<ld>,
                         C<64int>, C<64all>, C<debug> and C<clang>.

                         For every given perl version, install two
                         flavors, one with the flag C<--$flavor> set
                         and the other with out. C<--both> can be
                         passed multiple times with different values
                         and in that case, all the possible
                         combinations are generated.

    --common-variations  equivalent to C<--both thread --both ld --both 64int>

    --all-variations     generates all the possible flavor combinations

    --append $string     Appends the given string to the generated names

For instance:

    pperlbrew install-multiple 5.18.0 blead --both thread --both debug

Installs the following perls:

    perl-blead
    perl-blead-debug
    perl-blead-thread-multi
    perl-blead-thread-multi-debug
    perl-5.18.0
    perl-5.18.0-debug
    perl-5.18.0-thread-multi
    perl-5.18.0-thread-multi-debug

(note that the C<multi> flavor is selected automatically because
C<thread> requires it)

Another example using custom compilation flags:

    pperlbrew install-multiple 5.18.0 --both thread -Doptimize='-O3' --append='-O3'


=head1 COMMAND: UNINSTALL

Usage:

    pperlbrew uninstall <name>

Alternatively:

    pperlbrew delete <name>
    pperlbrew rm <name>

Uninstalls the given perl installation. The name is the installation name as in
the output of `pperlbrew list`. This effectively deletes the specified perl installation,
and all libs associated with it.

=head1 COMMAND: USE

Usage: pperlbrew B<use> [perl-<version> | <version> | <name>]

Select the named installation in the current shell. Future shells use the
saved selection from C<switch>. Source the activation file first.

Without a parameter, shows the version of perl currently in use.

=head1 COMMAND: SWITCH

Usage: pperlbrew switch [ <name> ]

Switch to the given version, and makes it the default for this and all
future terminal sessions.

Without a parameter, shows the version of perl currently selected.

=head1 COMMAND: LIST

Usage: pperlbrew list [--verbose | --no-decoration]

An asterisk marks the active selection. Use C<--no-decoration> for one
installation or library name per line.

List all perl installations inside pperlbrew root specified by C<$PPERLBREW_ROOT>
environment variable. By default, the value is C<~/perl5/pperlbrew>.

If there are libs associated to some perl installations, they will be included
as part of the name. The output items in this list can be the argument in
various other commands.

=head1 COMMAND: AVAILABLE

Usage: pperlbrew available [perl|pperl|jperl] [--all]

List PetaPerl builds for the current machine, PerlOnJava releases and Perl 5
releases on CPAN. Supply a family name to list only that family.

The PetaPerl and PerlOnJava catalogs ship with the manager. For Perl 5,
the default list shows the latest stable patch releases in each series.
C<pperlbrew available perl --all> includes older and development releases.

=head1 COMMAND: OFF

Usage: pperlbrew off [-s [-f]]

Clear the active interpreter for the current shell. The shell searches the
rest of PATH for C<perl>. On a system without another Perl, select a managed
interpreter again before invoking C<perl>.

If you use the '-s' option, then the current installation that you are turning
off is saved in the environment variable PPERLBREW_PERL_LAST, making it
available for the C<on> command. If PPERLBREW_PERL_LAST is already set, it is
not overridden unless you also use the '-f' option.

Source your shell activation file to use this command in the current shell.

=head1 COMMAND: ON

Usage: pperlbrew on

Runs C<pperlbrew use $PPERLBREW_PERL_LAST>. You must have run C<pperlbrew off -s>
to set PPERLBREW_PERL_LAST.

Source your shell activation file to use this command in the current shell.

=head1 COMMAND: SWITCH-OFF

Usage: pperlbrew switch-off

Clear the saved default selection for future shells. With shell integration
loaded, this also clears the current selection. The manager remains available.

=head1 COMMAND: ALIAS

Usage: pperlbrew alias [-f] create <name> <alias>

    Create an alias for the installation named <name>.

Usage: pperlbrew alias [-f] rename <old_alias> <new_alias>

    Rename the alias to a new name.

Usage: pperlbrew alias delete <alias>

    Delete the given alias.

=head1 COMMAND: EXEC

Usage: pperlbrew exec [options] <command> <args...>

Options for C<exec> command:

    --with perl-version,... - only use these versions
    --min n.nnnnn           - minimum perl version
                              (format is the same as in 'use 5.012')
    --max n.nnnnn           - maximum perl version
    --halt-on-error         - stop on first nonzero exit status

Run a command once per selected interpreter, in sequence. Use installation
names from C<pperlbrew list>. Separate names with commas, or quote a
space-separated list. A selection can include a named local library.

    pperlbrew exec --with java-old,java-new -- perl -e 'print "$]\n"'

The double dash separates manager options from the command. By default,
C<exec> continues after a failure and exits with status 1 if any invocation
failed. C<--halt-on-error> stops at the first failure.

=head1 COMMAND: ENV

Usage: pperlbrew env [name]

Print the shell assignments for the named installation. Shell integration
uses this output to set C<PPERLBREW_PERL>, the paths to executables and
manual pages, and any active local module library. To change the current
shell interactively, use C<pperlbrew use NAME>.

=head1 COMMAND: SYMLINK-EXECUTABLES

Usage: pperlbrew symlink-executables [name]

Create short names for version-suffixed executables in a managed Perl 5
installation. For example, C<perl5.13.6> can become available as C<perl>.
PetaPerl and PerlOnJava receive their C<perl> links during installation.

=head1 COMMAND: INSTALL-CPANM

Usage: pperlbrew install-cpanm

Install the C<cpanm> standalone executable in C<$PPERLBREW_ROOT/bin>.

=head1 COMMAND: INSTALL-CPM

Usage: pperlbrew install-cpm

Install the C<cpm> standalone executable in C<$PPERLBREW_ROOT/bin>.

=head1 COMMAND: INSTALL-PATCHPERL

Usage: pperlbrew install-patchperl

Install the optional C<patchperl> helper for older Perl 5 releases.

=head1 COMMAND: SELF-UPGRADE

Usage: pperlbrew self-upgrade [--force]

For a Linux bootstrap installation, run the installer against the current
release server. If perlbrew is present, pass C<--force> to upgrade alongside it.
For a CPAN installation, upgrade with the CPAN client that
installed App::pperlbrew.
Existing managed interpreters and the saved shell selection are preserved.

=head1 COMMAND: SELF-INSTALL

Usage: pperlbrew self-install

For a CPAN installation, initialize the manager root and link the installed
command. For a bootstrap installation, run the Linux installer.

=head1 COMMAND: CLEAN

Usage: pperlbrew clean

Empty the download cache and build directory under C<$PPERLBREW_ROOT>.
Use C<uninstall> to remove a managed interpreter.

=head1 COMMAND: VERSION

Usage: pperlbrew version

Show the version of pperlbrew.

=head1 COMMAND: LIB

Usage: pperlbrew lib <action> <lib-name>

    pperlbrew lib list
    pperlbrew lib create <lib-name>
    pperlbrew lib delete <lib-name>

The `lib` command is used to manipulate local::lib roots inside perl
installations. Effectively it is similar to `perl
-Mlocal::lib=/path/to/lib-name`, but a little bit more than just that.

A lib name can be a short name, containing alphanumeric, like 'awesome', or a
full name, prefixed by a perl installation name and a '@' sign, for example,
'perl-5.14.2@awesome'.

Here are some a brief examples to invoke the `lib` command:

    # Create lib perl-5.12.3@shizuka
    pperlbrew lib create perl-5.12.3@shizuka

    # Create lib perl-5.14.2@nobita and perl-5.14.2@shizuka
    pperlbrew use perl-5.14.2
    pperlbrew lib create nobita
    pperlbrew lib create shizuka

    # See the list of use/switch targets
    pperlbrew list

    # Activate a lib in current shell
    pperlbrew use perl-5.12.3@shizuka
    pperlbrew use perl-5.14.2@nobita
    pperlbrew use perl-5.14.2@shizuka

    # Activate a lib as default
    pperlbrew switch perl-5.12.3@shizuka
    pperlbrew switch perl-5.14.2@nobita
    pperlbrew switch perl-5.14.2@shizuka

    # Delete lib perl-5.14.2@nobita and perl-5.14.2@shizuka
    pperlbrew use perl-5.14.2
    pperlbrew lib delete nobita
    pperlbrew lib delete shizuka

    # Delete lib perl-5.12.3@shizuka
    pperlbrew lib delete perl-5.12.3@shizuka

Short lib names are local to current perl. A lib name 'nobita' can
refer to 'perl-5.12.3@nobita' or 'perl-5.14.2@nobita', whichever is
activated in the current shell.

When C<use>ing or C<switch>ing to a lib, always provide the long name. A simple
rule: the argument to C<use> or C<switch> command should appear in the output of
C<pperlbrew list>.

=head1 COMMAND: UPGRADE-PERL

Usage: pperlbrew upgrade-perl

Upgrade the active Perl 5 installation to the latest patch release in its
series. For PetaPerl or PerlOnJava, install the new version alongside the
old one and select it with C<use> or C<switch>. If you have a shell with 5.32.0 activated,
it upgrades it to 5.32.1.

Minor Perl releases (ex. 5.x.*) are binary compatible with one another, so this
command offers you the ability to upgrade older pperlbrew environments in place.

=head1 COMMAND: DOWNLOAD

Usage: pperlbrew download [--force] <release>

    pperlbrew download perl-5.42.0
    pperlbrew download jperl-5.44.1

Cache a Perl 5 source archive, a PetaPerl executable or a PerlOnJava source
archive under C<$PPERLBREW_ROOT/dists/>. For PetaPerl and PerlOnJava, the
manager verifies the SHA-256 digest before using a download or a cached file.
Use C<--force> to fetch a fresh copy. See C<pperlbrew available> for names.

=head1 COMMAND: LIST-MODULES

Usage:
    pperlbrew list-modules

List all installed cpan modules for the current perl.

This command can be used in conjunction with `pperlbrew exec` to migrate
your module installation to different perl. The following command
re-installs all modules under perl-5.16.0:

    pperlbrew list-modules | pperlbrew exec --with perl-5.16.0 cpanm

Note that this installs the I<latest> versions of the Perl modules on the new perl,
which are not necessarily the I<same> module versions you had installed previously.

=head1 COMMAND: CLONE-MODULES

Usage:

    pperlbrew clone-modules [options] <destination>
    pperlbrew clone-modules [options] <source> <destination>

Options:

    --notest    Skip all module tests

This command re-installs all CPAN modules found from one installation to another. For example, this lists all modules under '5.26.1' and re-installs them under '5.27.7':

    pperlbrew clone-modules 5.26.1 5.27.7

The argument "source" is optional and defaults to the currently activated one. However if none is activated (pperlbrew is switched off), it is an error.

Note that this does not guarantee that the versions of modules stay the same in the destination.

=head1 COMMAND: MAKE-SHIM

Usage:

    pperlbrew make-shim <program>
    pperlbrew make-shim -o <shim-name> <program>
    pperlbrew make-shim --output <shim-name> <program>

This commands produce an executable file under current directory named C<program>, or C<shim-name> if given after C<--output> (or C<-o> for short). The output is a shell-wrapper, a shim, of the named program inside current pperlbrew environment.

When the shim is executed, the original C<program> is then executed with all relevant environment variable set to the pperlbrew environment it is installed in, regardless which pperlbrew environment is currently activated. The shim can also be moved to different directories and, such as the conventional C<~/.local/bin>, so it is always available.

For example, you may find C<tldr> from L<App::tldr> a handy tool and decide to install it inside your daily working environment:

    pperlbrew use perl-5.36.1
    cpm install -g App::tldr

But when you occasionally have to switch to a different environment, C<PATH> would be tweaked and the command C<tldr> would went missing, and that is the expected outcome:

    pperlbrew use perl-5.18.4
    tldr perl  #=> error: command not found

It would be nice if C<tldr> can be made universally available. One way to mitigate such needs is to prepare install the C<tldr> program outside of C<PPERLBREW_ROOT>, while still utilize pperlbrew environment to run it.

For example, prepare a conventional directory C<~/.local/bin> and put that in C<PATH>, then:

    pperlbrew use perl-5.36.1

    cd /tmp
    pperlbrew make-shim tldr
    mv /tmp/tldr ~/.local/bin/tldr

This C<~/.local/bin/tldr> is a shell-wrapper of the actual C<tldr> program, and it internally activates the pperlbrew environment C<perl-5.36.1>. Running the tldr shim will then always run the actual C<tldr>, no matter which pperlbrew environment is activated, or even if pperlbrew is turned off. The only requirements is that the pperlbrew environment C<perl-5.36.1> and the installation of C<App::tldr> has to remain.

=head1 COMMAND: MAKE-PP

Usage:

    pperlbrew make-pp -i <path> -o <path>
    pperlbrew make-pp --input <path> --output <path>

This command takes a path of a perl program (the input), and produce a PAR-packed version of that program to the specified path (the output). Essentially this is a wrapper of C<pp> from L<PAR::Packer>, hence the name.

This requires the current pperlbrew environment to have L<PAR> and L<PAR::Packer> installed first. Otherwise C<make-pp> bails out. In addition, if the current perl is not a pperlbrew-managed perl, or if the given output path is already occupied, <make-pp> also bails out.

The produced file is a standalone binary executable containing these content:

    1. The input perl program
    2. perl runtime
    3. all core perl libs of current perl
    4. the entire site lib
    5. the entire local lib (managed by `pperlbrew lib` command), if active.

It is expected that the executable can then be running on a different machine of the same OS and arch.

Packing the full library trees can produce a large executable. It also includes modules that the program loads dynamically.

=head1 SEE ALSO

L<App::pperlbrew>, L<App::cpanminus>, L<Devel::PatchPerl>

=cut
