ttyS0 tty1
  #   #ˆl–Ãa¾”T†Ù”â�Æœ¸&TÅ£Æ„á¿ÁÊ ?÷     ##!/usr/bin/perl

#                                      Copyright 2026 WebPros International, LLC
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited.

package scripts::fixcpanelperl;

use strict;
use warnings;

### Do not add any extra use/require statements here without talking to BC.
###
### This code is used by the installer for fresh install
###     its main goal is to bootstrap & repair cPanel Perl
###
### Thus no dependencies should be added to this special script.
### Because this script uses only system perl code,
###     the rest of the cPanel code base gets to have nice things!
###     and rely on cPanel Perl stack.

use IPC::Open3 ();
use POSIX      ();

use constant COLOR_RED    => 31;
use constant COLOR_GREEN  => 32;
use constant COLOR_YELLOW => 33;
use constant COLOR_GRAY   => 37;

use constant UPDATE_SOURCE_DEFAULT => 'httpupdate.cpanel.net';
use constant PKG_VERSION_SOURCE    => '11.130';
use constant PKG_DOWNLOADS_DIR     => '/usr/local/cpanel/tmp/rpm_downloads';

=pod

=head1 NAME

scripts::fixcpanelperl

=head1 SYNOPSIS

    # restore cpanel-perl package when missing
    /scripts/fix-cpanel-perl

=head1 DESCRIPTION

This script is used during fresh installation to bootstrap cPanel Perl and install a few
core dependencies packages to run the installation process using cPanel Perl version as early
as possible.

The script can also be used to recover from a catastrophic loss or coruption of the cpanel-perl
packages blocking check_cpanel_pkgs or upcp to recover the system.

This script is automatically invoked if needed when check_cpanel_pkgs cannot run.

=head1 NOTE

The list of Perl packages install by this script should be short.

This script should only use default perl packages install with the system,
no Cpanel packages can be use as part of this script.

=cut

my %shared_install_env = (
    LANG        => 'C',
    LANGUAGE    => 'C',
    LC_ALL      => 'C',
    LC_MESSAGES => 'C',
    LC_CTYPE    => 'C',
);

our %DISTRO_SETUP = (
    'centos' => {
        name        => 'centos',
        install_env => {
            %shared_install_env,
        },
        install_cmd => [ '/bin/rpm', '-Uvh', '--force', '--nodeps' ],
        template    => {
            base_url  => 'http://%httpupdate%/RPM/%cpanel_major%/%distro_name%/%distro_major%/x86_64',
            file_name => '%package%-%version%~el%distro_major%.%arch%.rpm'
        },
        package_sha512 => 'rpm.sha512',    # could use later once pushed to prod sha512
    },
    'ubuntu' => {
        name        => 'ubuntu',
        install_env => {
            %shared_install_env,
            DEBIAN_FRONTEND => q[noninteractive],
            DEBIAN_PRIORITY => q[critical],
        },
        install_cmd => [ '/bin/dpkg', '-i', '--force-confnew' ],
        template    => {
            base_url      => 'http://%httpupdate%/ubuntu/pool',
            base_file_uri => '%package%',
            file_name     => '%package%_%version%~u%distro_major%_%arch%.deb',
        },
        package_sha512 => 'sha512',
    },
);

our $CPANEL_CONFIG_FILE        = '/var/cpanel/cpanel.config';
our $SIG_VALIDATION_CPCONF_KEY = 'signature_validation';

sub colorize_bold {
    my ( $color, $msg ) = @_;

    return $msg
      if !defined $color || -e '/var/cpanel/disable_cpanel_terminal_colors';
    $msg ||= '';

    return chr(27) . '[1;' . $color . 'm' . $msg . chr(27) . '[0;m';
}

sub DEBUG($) { return _MSG( 'DEBUG', "  " . shift ) }

sub ERROR($) {
    return _MSG(
        colorize_bold( COLOR_RED,  'ERROR' ),
        colorize_bold( COLOR_GRAY, shift )
    );
}

sub WARN($) {
    return _MSG(
        colorize_bold( COLOR_YELLOW, 'WARN' ),
        colorize_bold( COLOR_YELLOW, shift )
    );
}

sub OK($) {
    return _MSG(
        colorize_bold( COLOR_GREEN, 'OK' ),
        colorize_bold( COLOR_GRAY,  shift )
    );
}
sub INFO(