#!/usr/local/cpanel/3rdparty/bin/perl
# cpanel - autofixer2/centos6_base_repo_is_no_more Copyright 2020 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
#

use strict;
use warnings;

use Cpanel::Sys::OS  ();
use Cpanel::SafeFile ();

my $os_major = int( Cpanel::Sys::OS::getreleaseversion() );

exit unless $os_major && $os_major == 6 && Cpanel::Sys::OS::getos() eq 'centos';    # on C6 the base repo has ceased to be

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> ) {

    # baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/ to baseurl=https://vault.centos.org/6.10/os/x86_64/
    # mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra to https://vault.centos.org/6.10/os/x86_64/
    if (   $line =~ m{^baseurl\s*=\s*http://mirror.centos.org/centos/\$releasever/([^/]+)/.*}
        || $line =~ m{^mirrorlist\s*=\s*http://mirrorlist.centos.org/.*&repo=([^&]+).*} ) {
        my $repoid = $1;
        chomp($repoid);
        $line = "baseurl=http://vault.centos.org/6.10/$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 );

