#!/usr/local/cpanel/3rdparty/bin/perl
# cpanel - autofixer2/fix_cpanel_plugins_mirrors_for_ubuntu
#                                                  Copyright 2023 cPanel, L.L.C.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

package autofixer2::fix_cpanel_plugins_mirrors_for_ubuntu;

use strict;
use warnings;

use Cpanel::OS              ();
use Cpanel::SafeRun::Object ();

exit run() unless caller;

sub run {
    return 0 unless supported_on_this_major( 102, 114 );    # Set this to the Min/Max we support this autofixer on.

    return 0 unless Cpanel::OS::is_apt_based();

    my $run = Cpanel::SafeRun::Object->new(
        program => '/usr/bin/apt-get',
        args    => [ '--allow-releaseinfo-change', 'update' ],
    );

    if ( $run->CHILD_ERROR() ) {
        warn 'Unable to update apt-get with --allow-releaseinfo-change flag.  This issue will need to be resolved manually.';
    }

    return 0;    # exit(0);
}

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>;
        chomp $full_version;
        close($fh);
        ($major_version) = $full_version =~ /^[0-9]+\.([0-9]+)/;
    }

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

1;
