1#*************************************************************************
2#
3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4#
5# Copyright 2000, 2010 Oracle and/or its affiliates.
6#
7# OpenOffice.org - a multi-platform office productivity suite
8#
9# This file is part of OpenOffice.org.
10#
11# OpenOffice.org is free software: you can redistribute it and/or modify
12# it under the terms of the GNU Lesser General Public License version 3
13# only, as published by the Free Software Foundation.
14#
15# OpenOffice.org is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU Lesser General Public License version 3 for more details
19# (a copy is included in the LICENSE file that accompanied this code).
20#
21# You should have received a copy of the GNU Lesser General Public License
22# version 3 along with OpenOffice.org.  If not, see
23# <http://www.openoffice.org/license.html>
24# for a copy of the LGPLv3 License.
25#
26#*************************************************************************
27
28use lib ("$ENV{SOLARENV}/bin/modules");
29use macosxotoolhelper;
30
31sub action($$$)
32{
33    my %action =
34        ('app/UREBIN/URELIB' => '@executable_path/../lib',
35         'app/OOO/URELIB' => '@executable_path/../ure-link/lib',
36         'app/OOO/OOO' => '@executable_path',
37         'app/SDK/URELIB' => '@executable_path/../../ure-link/lib',
38         'app/BRAND/URELIB' => '@executable_path/../basis-link/ure-link/lib',
39         'app/BRAND/OOO' => '@executable_path/../basis-link/program',
40         'app/NONE/URELIB' => '@__VIA_LIBRARY_PATH__',
41         'app/NONE/OOO' => '@__VIA_LIBRARY_PATH__',
42         'app/NONE/NONE' => '@__VIA_LIBRARY_PATH__',
43         'shl/URELIB/URELIB' => '@loader_path',
44         'shl/OOO/URELIB' => '@loader_path/../ure-link/lib',
45         'shl/OOO/OOO' => '@loader_path',
46         'shl/LOADER/LOADER' => '@loader_path',
47         'shl/OXT/URELIB' => '@executable_path/urelibs',
48         'shl/BOXT/URELIB' => '@executable_path/urelibs',
49         'shl/BOXT/OOO' => '@loader_path/../../../basis-link/program',
50         'shl/NONE/URELIB' => '@__VIA_LIBRARY_PATH__',
51         'shl/NONE/OOO' => '@__VIA_LIBRARY_PATH__',
52         'shl/NONE/NONE' => '@__VIA_LIBRARY_PATH__');
53    my ($type, $loc1, $loc2) = @_;
54    my $act = $action{"$type/$loc1/$loc2"};
55    die "illegal combination $type/$loc1/$loc2" unless defined $act;
56    return $act;
57}
58
59@ARGV == 3 || @ARGV >= 2 && $ARGV[0] eq "extshl" or die
60  'Usage: app|shl|extshl UREBIN|URELIB|OOO|SDK|BRAND|OXT|BOXT|NONE|LOADER <filepath>*';
61$type = shift @ARGV;
62$loc = shift @ARGV;
63if ($type eq "SharedLibrary")
64{
65    $type = "shl";
66}
67if ($type eq "Executable")
68{
69    $type = "app"
70}
71if ($type eq "Library")
72{
73    $type = "shl"
74}
75if ($type eq "extshl")
76{
77    $type = "shl";
78    my $change = "";
79    my %inames;
80    foreach $file (@ARGV)
81    {
82        my $iname = otoolD($file);
83        (defined $iname ? $iname : $file . "\n") =~ m'^(.*?([^/]+))\n$' or
84            die "unexpected otool -D output";
85        $change .= " -change $1 " . action($type, $loc, $loc) . "/$2";
86        $inames{$file} = $2;
87    }
88    if( $loc eq "LOADER" )
89    {
90        foreach $file (@ARGV)
91        {
92            my $call = "install_name_tool$change -id \@loader_path/$inames{$file} $file";
93            system($call) == 0 or die "cannot $call";
94        }
95    }
96    else
97    {
98        foreach $file (@ARGV)
99        {
100            my $call = "install_name_tool$change -id \@__________________________________________________$loc/$inames{$file} $file";
101            system($call) == 0 or die "cannot $call";
102        }
103    }
104}
105foreach $file (@ARGV)
106{
107    my $call = "otool -L $file";
108    open(IN, "-|", $call) or die "cannot $call";
109    my $change = "";
110    while (<IN>)
111    {
112        $change .= " -change $1 " . action($type, $loc, $2) . "$3"
113            if m'^\s*(@_{50}([^/]+)(/.+)) \(compatibility version \d+\.\d+\.\d+, current version \d+\.\d+\.\d+\)\n$';
114    }
115    close(IN);
116    if ($change ne "")
117    {
118        $call = "install_name_tool$change $file";
119        system($call) == 0 or die "cannot $call";
120    }
121}
122