aboutsummaryrefslogtreecommitdiffstats
path: root/relink_symlinks
blob: f9642df78563791e8a43d5625f5978b441835402 (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
#!/usr/bin/perl
# relativize absolute symlinks

use strict;
use warnings;
use File::Find;

my $buildroot = $ENV{RPM_BUILD_ROOT};
die "No build root defined" unless $buildroot;
die "Invalid build root" unless -d $buildroot;
# normalize build root
$buildroot =~ s|/$||;

sub relativize {
    return unless -l $_;

    my $link = readlink($_);
    return unless $link =~ m{^/} && $link !~ m{^/(dev|proc|sys)/};

    $link =~ s{^/}{};
    my $dirname = $File::Find::dir;
    $dirname =~ s/^\Q$buildroot\E//;
    $dirname =~ s{/[^/]+}{../}g;

    unlink $_;
    symlink $dirname . $link, $_;
}

find(\&relativize, $buildroot);