#!/usr/local/cpanel/3rdparty/bin/perl
#                                      Copyright 2025 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 recoverymgmt;

use strict;
use warnings;

use Cpanel::HttpRequest;

my @always_repair = (
    'check_tar_and_xz_binaries',
    'insecure_cpanel_version_icontact_notification',
    'security_upgrade_banner',
    'run_security_update',    # CPANEL-52965 -- bump cPanel & WHM to the corrected build (if available) for their major release
);

my @repair_once = (
    'fix_iptables_legacy_rhel9',
    'run_maintenance_script',

    # These base repo and autossl autofixers are for CPANEL-46620. They can be removed
    # from recoverymgmt after this AutoSSL problem is reduced or eliminated.
    'centos6_base_repo_is_no_more',
    'centos7_base_repo_is_no_more',
    'centos7_disable_broken_repos',
    'set_autossl_to_lets_encrypt_2',
    'remove_carddav_sample',
    'force_release_pin',          # CPANEL-52947 -- bump partner onto release tier so they can reach 11.136.1.7
    'unblock_c7_v110_upcp_v2',    # CPANEL-52975 -- install epel-release + boost169-program-options on C7 hosts stuck at <=110.0.52 (renamed to force rerun)
    'unblock_cl6_v110_upcp',      # CPANEL-52976 -- pin CL6 hosts on cPanel 110 to the cl6110 tier and kick upcp

    # CPANEL-52811: mysql84-community-release el8-3 ships mysql-9.7-lts-community enabled=1.
    # Disable it before sysup upgrades MySQL. Remove after CPANEL-52811 ULC fix is deployed.
    'mysql97_lts_repo_disable',
    'cpanel_feature',              # CPANEL-53208
    'update_cpanel_plugins',       # DUCKS-6049
    'lsws_uninstall_cpanelplugin', # CPANEL-53481
);
our $srcdir = '/usr/local/cpanel/src';

exit run() unless caller;

sub run {

    # For mocking, the repair sub is trivial.
    repair($_)      for @always_repair;
    repair_once($_) for @repair_once;

    return 0;
}

sub repair_once {
    my ($module2run) = @_;
    return if -e "$srcdir/$module2run";    # Same check autorepair uses for run_once -- "is it there"
    return repair($module2run);
}

sub repair {
    my ($module2run) = @_;
    chdir "/usr/local/cpanel/src" or die "Can't chdir to /usr/local/cpanel/src: $!";
    print "Running autorepair on $module2run\n";

    my $url        = 'http://' . get_update_source() . '/autofixer2/' . $module2run;
    my $httpClient = Cpanel::HttpRequest->new( 'hideOutput' => 1, 'signed' => 1 );
    $httpClient->download( $url, $module2run );
    if ( !-e $module2run ) {
        die "Unable to fetch repair $module2run: $!";
    }
    my $perl_bin = -x "/usr/local/cpanel/3rdparty/bin/perl" ? "/usr/local/cpanel/3rdparty/bin/perl" : "perl";
    return system $perl_bin, $module2run;
}

sub get_update_source {
    my $update_source = 'httpupdate.cpanel.net';
    my $source_file   = '/etc/cpsources.conf';
    if ( -r $source_file && -s $source_file ) {    # pull in from cpsources.conf if it's set.
        open( my $fh, "<", $source_file ) or return;
        while (<$fh>) {
            next if ( $_ !~ m/^\s*HTTPUPDATE\s*=\s*(\S+)/ );
            $update_source = "$1";
            die("HTTPUPDATE is set to '$update_source' in $source_file") if ( !$update_source );
            last;
        }
    }

    return $update_source;
}

1;
