#!/usr/local/cpanel/3rdparty/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 autofixer2::disable_cpaddons_repo;

use strict;
use warnings;

use Cpanel::FindBin         ();
use Cpanel::SafeRun::Errors ();

exit run() unless caller;

sub run {

    return 0 unless supported_on_this_major( 32, 136 );    # Set this to the Min/Max we support this autofixer on.

    # Use Cpanel::FindBin over Cpanel::Binaries because this will work across the version range
    my $ycm_bin = Cpanel::FindBin::findbin('yum-config-manager');

    # cPAddons were never supported on Ubuntu, where yum-config-manager isn't present, so there's nothing to disable.
    return 0 unless $ycm_bin;

    # Disable the cPAddons production feed repo so nothing pings it any longer.
    # Done via the yum command in case the repo is not in the known repo file.
    Cpanel::SafeRun::Errors::saferunnoerror( $ycm_bin, '--disable', 'cpanel-addons-production-feed' );

    return 0;    # exit(0);
}

# Do we run this code?
sub supported_on_this_major {
    my ( $min_ver, $max_ver ) = @_;

    my $major = get_major_version();

    return 0 if $major < $min_ver;
    return 0 if $major > $max_ver;

    return 1;
}

sub get_major_version {
    my $major_version;

    if ( open( my $fh, '<', '/usr/local/cpanel/version' ) ) {
        my $full_version = <$fh>;
        close($fh);
        if ( length $full_version ) {
            chomp $full_version;
            ($major_version) = $full_version =~ /^[0-9]+\.([0-9]+)/;
        }
    }

    # Safe default.
    return $major_version || 30;
}

1;
