#!/usr/bin/perl use warnings; use strict; use Cwd qw(cwd); use File::Basename qw(dirname basename); use File::Spec; my $DEBUG = 0; my $prefix = '/sw'; my $parent = "Resources"; sub do_cmd { my $cmd = shift; print "$cmd\n" if $DEBUG; system($cmd) == 0 or die "Command failed: $cmd\nError: $!\n"; } sub resolve_link { my $source = shift; if (-l $source) { my $target = readlink($source); if ($target =~ m,^/,) { return resolve_link($target); } else { return resolve_link(File::Spec->catfile(dirname($source), $target)); } } else { return File::Spec->rel2abs($source); } } my @pkgs = @ARGV; my $pwd = cwd; for my $pkg (@pkgs) { print " reparenting $pkg\n"; # open FILE, "dpkg -L \Q$pkg\E | xargs file |" # or die "Can't read package files: $!\n"; open FILE, "dpkg -L $pkg |" or die "Can't read package files: $!\n"; for my $file () { next unless $file =~ m,^$prefix/,; # my ($fname, $ftype) = ($file =~ /^([^:]+):(.*)/) # or die "Can't understand 'file' output: $file\n"; my $fname = $file; chomp $fname; next unless -f $fname; (my $dir = dirname($fname)) =~ s,^,$parent,; do_cmd("mkdir -p \Q$dir\E"); do_cmd("/bin/cp -RP \Q$fname\E \Q$dir\E"); # if ($ftype =~ /Mach-O/) { # my $newfile = File::Spec->catfile($dir, basename($fname)); # open OTOOL, "otool -L \Q$newfile\E |" # or die "Can't run otool: $!\n"; # my @libs = ; # close OTOOL; # # for my $libline (@libs) { # $libline =~ m,^\s+$prefix/(.*)\s\(comp, or next; # my $lib = $1; # (my $libdir = $lib) =~ s,^/,$parent,; # my $plib = File::Spec->catfile($prefix, $lib); # my $ilib = File::Spec->catfile($libdir, $lib); # my $inst = File::Spec->catfile('@executable_path', # File::Spec->abs2rel($ilib, "$pwd/$parent$prefix/bin")); # if (resolve_link($plib) eq $fname) { # do_cmd("install_name_tool -id \Q$inst\E \Q$newfile\E"); # } else { # do_cmd("install_name_tool -change \Q$plib\E \Q$inst\E " . # "\Q$newfile\E"); # } # } # } } close FILE; }