summaryrefslogtreecommitdiff
path: root/bin/merge-app-bundles
blob: 2a2200f1c44d49f135d3390b458fa3d6ac10a479 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env bash

# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

# Exit on errors
set -e

# Use of unset variable is an error
set -u

# If any part of a pipeline of commands fails, the whole pipeline fails
set -o pipefail

if [ `uname` != Darwin ]; then
    echo This is for macOS only >&2
    exit 1
fi

if [ $# != 3 ]; then
    echo Usage: $0 app-bundle-1 app-bundle-2 output-app-bundle
    exit 1
fi

if [ -d "$3" ]; then
    echo The directory $3 exists already
    exit 1
fi

if [ -f "$3" ]; then
    echo $3 exists and is a file
    exit 1
fi

if [ ! -d "$1" ]; then
    echo No such directory: $1
    exit 1
fi

if [ ! -d "$2" ]; then
    echo No such directory: $2
    exit 1
fi

ONE=$(cd "$1" && /bin/pwd)
TWO=$(cd "$2" && /bin/pwd)
mkdir "$3"
OUT=$(cd "$3" && /bin/pwd)

# Create all directories
(
    cd "$ONE"
    find . -type d -print
) |
(
    cd "$OUT"
    while read dirname; do
        mkdir -p "$dirname"
    done
)

# Check which files in 1 exist in 2, and if they are executable, merge them into a fat copy. For
# other files, just use one copy, assuming they are equivalent in most cases.
(
    cd "$ONE"
    find . -type l -or -type f
) |
(
    cd "$TWO"
    while read fname; do
        if test -L "$fname"; then
            ln -s $(readlink "$fname") "$OUT/$fname"
        elif test -f "$fname"; then
            case "$fname" in
                *.so | \
                *.dylib | \
                *.dylib.* | \
                */Frameworks/LibreOfficePython.framework/Versions/*/LibreOfficePython | \
                */Frameworks/LibreOfficePython.framework/Versions/*/Resources/Python.app/Contents/MacOS/LibreOfficePython | \
                */Library/Spotlight/OOoSpotlightImporter.mdimporter/Contents/MacOS/OOoSpotlightImporter)
                    lipo -create -output "$OUT/$fname" "$fname" "$ONE/$fname"
                    ;;
                # Ignore differences in these files. Let's hope it's just the timestamps.
                *.ot[tp] | \
                *.bau | \
                *.pyc | \
                */_sysconfigdata_m_darwin_darwin.py | \
                */Contents/Resources/firebird/security3.fdb | \
                */Contents/Resources/autocorr/acor_*.dat | \
                */Contents/Resources/resource/*/LC_MESSAGES/*.mo | \
                */Contents/Resources/config/images_*.zip)
                    cp "$fname" "$OUT/$fname"
                    ;;
                *)
                    case $(file --brief "$fname") in
                        Mach-O\ 64-bit\ executable\ *)
                            lipo -create -output "$OUT/$fname" "$fname" "$ONE/$fname"
                            ;;
                        *)
                            cmp -s "$fname" "$ONE/$fname" ||
                                echo "$fname differs and is not an executable!?" >&2
                            cp "$fname" "$OUT/$fname"
                    esac
            esac
        else
            # We ignore some files that can't be built for macOS on arm64 for now
            case "$fname" in
                ./Contents/Frameworks/LibreOfficePython.framework/Versions/3.7/lib/python*/lib-dynload/_ctypes.cpython-*m.so)
                    ;;
                *)
                    echo "$fname does not exist in $TWO" >&2
                    ;;
            esac
            cp "$ONE/$fname" "$OUT/$fname"
        fi
    done
)

# Look for files in 2 that don't exist in 1
(
    cd "$TWO"
    find . -type f -print
) |
(
    cd "$ONE"
    while read fname; do
        if test -f "$fname"; then
            :
        else
            echo "$fname does not exist in $ONE" >&2
            cp "$TWO/$fname" "$OUT/$fname"
        fi
    done
)

# Local Variables:
# tab-width: 4
# indent-tabs-mode: nil
# fill-column: 100
# End: