aboutsummaryrefslogtreecommitdiffstats
path: root/relink_symlinks
blob: 01508997b1122b3520cd171264dad8470894aac8 (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 =~ s{^/}{};
    my $dirname = $File::Find::dir;
    $dirname =~ s/^\Q$buildroot\E//;
    $dirname =~ s{/[^/]+}{../}g;

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

find(\&relativize, $buildroot);