summaryrefslogtreecommitdiff
path: root/package/update_mirror.pl
blob: a692542ace553d956c6b8eee4941f7a032e4fbc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/perl

use strict;
use warnings;
use autodie;

# my $mirrors = 'https://mirrors.tuna.tsinghua.edu.cn/centos-stream';
my $mirrors = 'https://mirrors.aliyun.com/centos-stream';
# my $mirrors = 'https://mirrors.aliyun.com/rockylinux';

if (@ARGV < 1) {
    die "Usage: $0 <filename1> <filename2> ...\n";
}

my $filename = "/etc/yum.repos.d/centos.repo";
my $keyword = "baseurl";
open(my $fh, '<', $filename) or die "Can not open '$filename' $!";
my $found = 0;
while (my $row = <$fh>) {
    chomp $row;
    if (index($row, $keyword) != -1) {
        $found = 1;
        last;
    }
}
close($fh);

if ($found) {  
        print "Find the keyword '$keyword' in '$filename'.\n";  
} else {
    while (my $filename = shift @ARGV) {
        my $backup_filename = $filename . '.bak';
        rename $filename, $backup_filename;
        open my $input, "<", $backup_filename;
        open my $output, ">", $filename;
        while (<$input>) {
            s/^metalink/# metalink/;
            if (m/^name/) {
                my (undef, $repo, $arch) = split /-/;
                $repo =~ s/^\s+|\s+$//g;
                ($arch = defined $arch ? lc($arch) : '') =~ s/^\s+|\s+$//g;

                if ($repo =~ /^Extras/) {
                    $_ .= "baseurl=${mirrors}/SIGs/\$releasever-stream/extras" . ($arch eq 'source' ? "/${arch}/" : "/\$basearch/") . "extras-common\n";
                } else {
                    $_ .= "baseurl=${mirrors}/\$releasever-stream/$repo" . ($arch eq 'source' ? "/" : "/\$basearch/") . ($arch ne '' ? "${arch}/tree/" : "os") . "\n";
                }
            }
            print $output $_;
        }
    }
}