summaryrefslogtreecommitdiff
path: root/debian/fixlinks
blob: 110a5e3134520b314166b3cfce31ef7765f90028 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/perl -w
# vi: set ts=4:


@LINKS=`find . -type l|LC_ALL=C xargs ls -l`;

#print @LINKS;

#$debug = 1;

while($_ = shift @LINKS){
	chomp;
	my ($perm,$nlinks,$owner,$group,$size,$month,$day,$year,$file) =
		split(' ', $_, 9);
	my $link;

	if($perm =~ m/^l/){
		($relfile, $link) = split(' -> ', $file);
	}

	# chop off leading . in $file
	$file = $relfile;
	$file =~ s/^\.//;

	if($perm =~ m/^l/){
		my @pathcomponents = split('/', $file);
		my @linkcomponents = split('/', $link);

		if($link =~ m/^\//){
			@newcomponents = @linkcomponents;
		}else{
			@newcomponents = @pathcomponents;

			# chop off filename
			pop(@newcomponents);

			while($comp = shift @linkcomponents){
				$debug && print "path: ",join(':',@newcomponents)," -- $comp -- ", join(':',@linkcomponents),"\n";

				if($comp eq ""){
					# ignore
				}elsif($comp eq ".."){
					pop(@newcomponents);
				}else{
					push @newcomponents,$comp;
				}
			}
		}

		if($newcomponents[0] eq ""){
			shift(@newcomponents);
		}
		if($pathcomponents[0] eq ""){
			shift(@pathcomponents);
		}

		#print "from ",join('/',@pathcomponents),"\n";
		#print "to ",join('/',@newcomponents),"\n";

		if($newcomponents[0] eq $pathcomponents[0]){
			$debug && print $newcomponents[0],", ",$pathcomponents[0];
			$debug && print "should be relative\n";
			while($newcomponents[0] eq $pathcomponents[0]){
				shift(@newcomponents);
				shift(@pathcomponents);
			}
			while(@pathcomponents > 1){
				shift(@pathcomponents);
				unshift(@newcomponents,"..");
			}
		}else{
			$debug && print "should be absolute\n";
			unshift(@newcomponents,"");
		}
		$newlink=join('/',@newcomponents);
		print "ln -sf $newlink $relfile\n";
		unlink($relfile);
		symlink($newlink,$relfile);
	}
}