#!/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 autofixer2::centos7_base_repo_is_no_more;

use strict;
use warnings;

use File::Slurp::Tiny ();
use Cpanel::SafeFile  ();

BEGIN { unshift @INC, '/usr/local/cpanel'; }

exit run() unless caller;

sub run {
    return 0 unless supported_on_this_major( 94, 108 );

    # This is only needed on CentOS 7 servers back to cpanel version 94, so Cpanel::OS (v100) isn't an option
    return 0 unless _is_centos_7();

    my $base_repos_path = '/etc/yum.repos.d/CentOS-Base.repo';

    my $read_lock = Cpanel::SafeFile::safeopen( my $base_repo_fh, '<', $base_repos_path ) or do {
        die "Unable to obtain lock on $base_repos_path\n";
    };

    my @lines;
    while ( my $line = <$base_repo_fh> ) {
        if (   $line =~ m{^baseurl\s*=\s*http://mirror.centos.org/centos/\$releasever/([^/]+)/.*}
            || $line =~ m{^mirrorlist\s*=\s*http://mirrorlist.centos.org/.*&repo=([^&]+).*}
            || $line =~ m{^baseurl\s*=\s*http://centos2.cpanel.net/centos/\$releasever/([^/]+)/} ) {
            my $repoid = $1;
            chomp($repoid);
            $line = "baseurl=https://vault.centos.org/7.9.2009/$repoid/\$basearch\n";
        }

        push @lines, $line;
    }

    Cpanel::SafeFile::safeclose( $base_repo_fh, $read_lock );

    my $update_lock = Cpanel::SafeFile::safeopen( $base_repo_fh, '>', $base_repos_path ) or do {
        die "Unable to obtain lock on $base_repos_path\n";
    };

    print {$base_repo_fh} join( "", @lines );

    Cpanel::SafeFile::safeclose( $base_repo_fh, $update_lock );

    return 0;
}

# We need this to operate on 94 for HB-8214, therefore Cpanel::OS isn't an option
sub _is_centos_7 {
    my ( $name, $major_version );
    local $@;
    my @lines = eval { File::Slurp::Tiny::read_lines('/etc/os-release') };
    return 0 unless @lines;
    for my $line (@lines) {
        next unless length $line;
        if ( !length $name && $line =~ m{^NAME="([^.\s"]+)} ) {
            $name = lc $1;
            return 0 unless 'centos' eq $name;
        }
        if ( !length $major_version && $line =~ m{^VERSION="([^.\s"]+)} ) {
            $major_version = $1;
            return 0 unless '7' eq $major_version;
        }
        last if length $name && length $major_version;
    }
    return length $name && length $major_version;
}

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;
