File: macbuild.sh

package info (click to toggle)
x2goclient 4.1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 9,224 kB
  • sloc: cpp: 26,696; perl: 1,280; sh: 525; makefile: 145
file content (612 lines) | stat: -rwxr-xr-x 20,039 bytes parent folder | download
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#!/bin/bash

make_boolean() {
  OPTION="${1}"

  case "${OPTION}" in
    ("0"|"no"|""|"No"|"nO"|"NO"|"false"|"FALSE") OPTION="0";;
    (*) OPTION="1";;
  esac

  printf "${OPTION}"
}

phase() {
  echo
  echo "***"
  echo "*** ${1}..."
  echo "***"
  echo
}

usage() {
  exec >&2

  NO_VAL="0, no, NO, No, nO, false or FALSE"
  printf "$(basename ${0}): usage\n\n"
  printf "Accepted environment variables:\n"
  printf "\tSDK:\t\t\t\tsets the target SDK [string]\n\t\t\t\t\tdefault: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk\n"
  printf "\tMACOSX_DEPLOYMENT_TARGET:\tsets the deployment target (specific OS X version to optimize/build for) [string]\n\t\t\t\t\tdefault: 10.7\n"
  printf "\tSTDLIB:\t\t\t\tsets a specific stdlib variant. Must be used with FORCE_STDLIB to have any effect. [string]\n\t\t\t\t\tdefault: autodetect\n"
  printf "\tFORCE_STDLIB:\t\t\tforces a specific C++ stdlib version. If you use this, also specify STDLIB. YOU SHOULD NEVER USE THIS, UNLESS YOU KNOW WHAT YOU ARE DOING! [boolean]\n\t\t\t\t\tdefault: disabled\n"
  printf "\tDEBUG\t\t\t\tenables or disables debug builds [boolean]\n\t\t\t\t\tdefault: disabled\n"
  printf "\tBUNDLE\t\t\t\tenables or disables library bundling and the creation of a .dmg installer [boolean]\n\t\t\t\t\tdefault: enabled\n"
  printf "\tUNIVERSAL\t\t\tenables or disables x86 support. x86_64 support is always enabled [boolean]\n\t\t\t\t\tdefault: enabled\n"
  printf "\tMACPORTS_PREFIX\t\t\tsets the (MacPorts) prefix used to detect PulseAudio, nxproxy and xauth binaries [string]\n\t\t\t\t\tdefault: /opt/local/\n"
  printf "\n"
  printf "Boolean values help:\n"
  printf "\ta value of ${NO_VAL} will be treated as false\n"
  printf "\tany other value will be treated as true\n"

  exit 2
}

dependency_error() {
  exec >&2

  typeset element="${1}"; shift
  typeset component="${1}"; shift
  typeset type="${1}"; shift

  echo "${element} ${type} not found."
  echo "Install ${component} -- e.g., via "port -vt install ${component}" if using MacPorts."
  echo "If ${component} is already installed, try passing MACPORTS_PREFIX if the autodetected or default value (${MACPORTS_PREFIX}) does not match your setup."

  exit 3
}

lazy_canonical_path() {
  typeset path="${1}"

  typeset old_path=""
  while [ "${old_path}" != "${path}" ]; do
    old_path="${path}"
    path="${path//\/\///}"
  done

  printf "${old_path}"
}

get_nesting_level() {
set -x
  typeset -i level=0
  typeset path="${1}"

  while [ -n "${path}" ] && [ "${path}" != "." ] && [ "${path}" != "/" ]; do
    i="$((${i} + 1))"
    path="$(dirname "${path}")"
  done

  printf "${level}"
set +x
}

repeat_str() { # INPUT COUNT
  typeset INPUT="${1:?"Error: no input string passed to ${FUNCNAME}()."}"
  typeset COUNT="${2:?"Error: no count passed to ${FUNCNAME}()."}"

  typeset ret=""
  typeset -i i=0
  while [ "${i}" -lt "${COUNT}" ]; do
    ret="${ret}$(printf "${INPUT}")"
    i=$((${i} + 1))
  done

  printf "${ret}"

  return 0
}

typeset -a otool_fail_str
otool_fail_str=( "is not an object file"
     "can't open file"
     "Archive : " )

parse_otool_output() {
#set -x
  typeset raw_output="${@}"

  typeset fail_str=""
  for fail_str in "${otool_fail_str[@]}"; do
    if echo "${raw_output}" | grep -q "${fail_str}"; then
      return 1
    fi
  done

  typeset tmp_regex='^[[:space:]]+(.*)[[:space:]]\(compatibility version .*, current version .*\)'

  # In this special case, we do not want read to perform any word splitting.
  typeset oldifs="${IFS}"
  IFS=''

  # Used for skipping the ID entry.
  # Initialized to the empty string, but the first matching line will set it once.
  # The ID filename is required for subsequent dependency discovery.
  typeset id=""

  typeset line=""
  while read -r line; do
    if [[ "${line}" =~ ${tmp_regex} ]]; then
      typeset file="${BASH_REMATCH[1]}"

      if [ -z "${id}" ]; then
        echo "ID unset, something is wrong" >&2
        return 1
      elif [ "$(basename "${file}")" != "${id}" ]; then
        echo "${BASH_REMATCH[1]}"
      else
        first="0"
      fi
    elif [ -z "${id}" ]; then
      id="$(basename "${line%":"}")"
    fi
  done <<< "${raw_output}"

  IFS="${oldifs}"
#set +x
  return 0
}

MATCH_HELP='(^((-h)|(--help))([ 	]|$))|([ 	]+((-h)|(--help))([ 	]|$))'
[ -n "${*}" ] && [[ "${*}" =~ ${MATCH_HELP} ]] && usage

NAME="x2goclient"

TOP_DIR="$(dirname "$0")"
[[ "${TOP_DIR}" == /* ]] || TOP_DIR="${PWD}/${TOP_DIR#./}"
BUILD_DIR="${TOP_DIR}/client_build"
APPBUNDLE="${BUILD_DIR}/${NAME}.app"
EXE_DIR="${APPBUNDLE}/Contents/exe/"
FRAMEWORKS_DIR="${APPBUNDLE}/Contents/Frameworks/"
RESOURCES_DIR="${APPBUNDLE}/Contents/Resources/"
DMGFILE="${BUILD_DIR}/${NAME}.dmg"
PROJECT="${TOP_DIR}/${NAME}.pro"
PKG_DMG="${TOP_DIR}/pkg-dmg"

# Try to find the MacPorts prefix.
typeset MACPORTS_PREFIX_SEARCH=""
if type -P port >/dev/null 2>&1; then
  MACPORTS_PREFIX_SEARCH="$(type -P port)"
  MACPORTS_PREFIX_SEARCH="${MACPORTS_PREFIX_SEARCH%%bin/port}"
else
  # Port not being found in ${PATH} doesn't necessarily mean it isn't available.
  # Try to guess.
  MACPORTS_PREFIX_SEARCH="/opt/local/"
fi

NXPROXY="nxproxy"
NXAUTH="xauth"
PULSEAUDIO_BINARIES=( "pulseaudio" "esdcompat" "pacat" "pacmd"      "pactl"
                      "pamon"      "paplay"    "parec" "parecord"   "pasuspender" )
PULSEAUDIO_LIBRARIES=( "libpulse-simple.0.dylib"
                       "pulse-11.1"
                       "pulseaudio" )
RESOURCE_FILES=( "audio/startup.wav" )

typeset -a special_files_regex
special_files_regex+=( "pulseaudio/libpulsecommon-[0-9]+\.[0-9]+\.dylib"
                       "pulseaudio/libpulsecore-[0-9]+\.[0-9]+\.dylib"   )

typeset -r dependency_base_format='@executable_path/../Frameworks/'

: ${SDK:="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk"}
: ${MACOSX_DEPLOYMENT_TARGET:="10.7"}
: ${FORCE_STDLIB:="0"}
: ${DEBUG:="0"}
: ${BUNDLE:="1"}
: ${UNIVERSAL:="1"}
: ${MACPORTS_PREFIX:="${MACPORTS_PREFIX_SEARCH}"}

unset MACPORTS_PREFIX_SEARCH

FORCE_STDLIB="$(make_boolean "${FORCE_STDLIB}")"
DEBUG="$(make_boolean "${DEBUG}")"
BUNDLE="$(make_boolean "${BUNDLE}")"
UNIVERSAL="$(make_boolean "${UNIVERSAL}")"

[ "${DEBUG}" -eq "0" ] && BUILD_MODE="release" || BUILD_MODE="debug"

BUILD_ARCH="x86_64"
[ "${UNIVERSAL}" -eq "1" ] && BUILD_ARCH="${BUILD_ARCH} x86"

if [ "${FORCE_STDLIB}" -eq "1" ]; then
  if [[ -z "${STDLIB}" ]]; then
    echo "stdlib forcing enabled, but STDLIB not passed." >&2
    exit 1
  fi

  if [[ "${STDLIB}" != "libc++" && "${STDLIB}" != "libstdc++" ]]; then
    echo "stdlib forcing enabled, but STDLIB contains illegal value. Legal values: libc++, libstdc++" >&2
    exit 1
  fi
else
  SDK_MINOR_VERSION="$(/usr/bin/perl -pe 's#.*?10\.(\d+).*?\.sdk$#\1#' <<< "${SDK}")"
  MATCH_NUMBERS='^[0-9]+$'
  if [[ "${SDK_MINOR_VERSION}" =~ ${MATCH_NUMBERS} ]]; then
    [ "${SDK_MINOR_VERSION}" -gt "6" ] && STDLIB="libstdc++"
    [ "${SDK_MINOR_VERSION}" -gt "8" ] && STDLIB="libc++"
  else
    echo "Unable to determine OS X version. Unknown value '${SDK_MINOR_VERSION}'." >&2
    exit 1
  fi
fi

# Gather files.
NXPROXY="$(lazy_canonical_path "${MACPORTS_PREFIX}/bin/${NXPROXY}")"
NXAUTH="$(lazy_canonical_path "${MACPORTS_PREFIX}/bin/${NXAUTH}")"

[ -x "${NXPROXY}" ] || dependency_error "nxproxy" "nxproxy" "binary"
[ -x "${NXAUTH}" ] || dependency_error "xauth" "xauth" "binary"

typeset -i i
typeset -i fail
typeset -a PULSEAUDIO_BINARIES_FULL
typeset cur_binary
fail="0"
for cur_binary in ${PULSEAUDIO_BINARIES[@]}; do
  cur_binary="$(lazy_canonical_path "${MACPORTS_PREFIX}/bin/${cur_binary}")"

  if [ -x "${cur_binary}" ]; then
    PULSEAUDIO_BINARIES_FULL+=( "${cur_binary}" )
  else
    fail="1"
    break
  fi
done

[ "${fail}" -eq "1" ] && dependency_error "${cur_binary##"$(lazy_canonical_path "${MACPORTS_PREFIX}/bin/")"}" "pulseaudio" "binary"

typeset cur_lib_or_libdir
typeset -a PULSEAUDIO_LIBRARIES_FULL
fail="0"
for cur_lib_or_libdir in ${PULSEAUDIO_LIBRARIES[@]}; do
  cur_lib_or_libdir="$(lazy_canonical_path "${MACPORTS_PREFIX}/lib/${cur_lib_or_libdir}")"

  if [ ! -d "${cur_lib_or_libdir}" ] && [ -x "${cur_lib_or_libdir}" ]; then
    echo "Adding ${cur_lib_or_libdir} to \${PULSEAUDIO_LIBRARIES_FULL}"
    PULSEAUDIO_LIBRARIES_FULL+=( "${cur_lib_or_libdir}" )
  elif [ -d "${cur_lib_or_libdir}" ]; then
    # That's a directory... more work needed here.
    echo "Scrubbing directory ${cur_lib_or_libdir}"
    typeset entry=""

    # -r parameter to read: Backslashes may NOT escape any characters!
    # -d '': specifies the delimiter to be used - as '' resolves to an empty string followed
    #        by a NUL character, the delimiter is set to this very NUL (\000) character.
    while read -r -d '' entry; do
      typeset cur_file="$(basename "${entry}")"
      typeset TMP_REGEX='^.*\.(\.[0-9]+){0,2}(so|dylib|bundle)$'

      # This is only here should the PA build system ever break and create
      # "linux-style" library file names. Let's hope it never actually comes to that.
      typeset TMP_REGEX_LINUX_COMPAT='^.*\.(so|dylib|bundle)(\.[0-9]+){0,2}$'

      if [[ "${cur_file}" =~ ${TMP_REGEX} ]] || [[ "${cur_file}" =~ ${TMP_REGEX_LINUX_COMPAT} ]]; then
        # Filename matched the expected template.
        echo "Adding $(lazy_canonical_path "${entry}") to \${PULSEAUDIO_LIBRARIES_FULL}"
        PULSEAUDIO_LIBRARIES_FULL+=( "$(lazy_canonical_path "${entry}")" )
      fi
    done < <(gfind "${cur_lib_or_libdir}" -type 'f' -print0)
  else
    fail="1"
    break
  fi
done

[ "${fail}" -eq "1" ] && dependency_error "${cur_lib_or_libdir}" "pulseaudio" "library or library directory"

set -e

phase "Cleaning"
make clean

# Create gitlog.
git --no-pager log --since "2 years ago" --format="%ai %aN (%h) %n%n%x09*%w(68,0,10) %s%d%n" > "ChangeLog.gitlog"
mv "ChangeLog.gitlog" "res/txt/git-info"

# Copy debian changelog as the general changelog.
cp -a "debian/changelog" "res/txt/"

[ -e "${BUILD_DIR}" ] && rm -rf "${BUILD_DIR}"

mkdir "${BUILD_DIR}"
pushd "${BUILD_DIR}"

phase "Running lrelease"
lrelease "${PROJECT}"

phase "Running qmake"
qmake -config "${BUILD_MODE}" -spec macx-g++ "${PROJECT}" \
      CONFIG+="${BUILD_ARCH}" \
      QMAKE_MAC_SDK="${SDK}" \
      QMAKE_MACOSX_DEPLOYMENT_TARGET="${MACOSX_DEPLOYMENT_TARGET}" \
      OSX_STDLIB="${STDLIB}" \
      MACPORTS_INCLUDE_PATH="${MACPORTS_PREFIX}/include" \
      MACPORTS_LIBRARY_PATH="${MACPORTS_PREFIX}/lib" \
      MACPORTS_PREFIX="${MACPORTS_PREFIX}"

phase "Running make"
make -j2

mkdir -p "${EXE_DIR}/"
mkdir -p "${FRAMEWORKS_DIR}/"

phase "Copying nxproxy"
cp -av "${NXPROXY}" "${EXE_DIR}/"

phase "Copying (n)xauth"
cp -av "${NXAUTH}" "${EXE_DIR}/nxauth"

phase "Copying misc resources"
typeset cur_res_file
for cur_res_file in ${RESOURCE_FILES[@]}; do
  cp -av "${TOP_DIR}/res/${cur_res_file}" "${RESOURCES_DIR}/"
done

phase "Copying PulseAudio"
for cur_binary in ${PULSEAUDIO_BINARIES_FULL[@]}; do
  cp -av "${cur_binary}" "${EXE_DIR}/"
done

typeset intermediate_lib_dir=""
for cur_binary in ${PULSEAUDIO_LIBRARIES_FULL[@]}; do
set -x
  intermediate_lib_dir="$(lazy_canonical_path "$(dirname "${cur_binary}")/")"
  intermediate_lib_dir="${intermediate_lib_dir##"$(lazy_canonical_path "${MACPORTS_PREFIX}/lib/")"}"

  mkdir -p "${FRAMEWORKS_DIR}/${intermediate_lib_dir}/"

  cp -av "${cur_binary}" "${FRAMEWORKS_DIR}/${intermediate_lib_dir}/"
set +x
done

if [ "${BUNDLE}" = "1" ]; then
  phase "Bundling nxproxy"
  dylibbundler \
    --fix-file "${EXE_DIR}/nxproxy" \
    --bundle-deps \
    --dest-dir "${FRAMEWORKS_DIR}/" \
    --install-path "@executable_path/../Frameworks/" \
    --create-dir

  phase "Bundling (n)xauth"
  dylibbundler \
    --fix-file "${EXE_DIR}/nxauth" \
    --bundle-deps \
    --dest-dir "${FRAMEWORKS_DIR}/" \
    --install-path "@executable_path/../Frameworks/" \
    --create-dir

  phase "Bundling PulseAudio"
  typeset cur_binary_name=""
  for cur_binary in ${PULSEAUDIO_BINARIES_FULL[@]}; do
    if [ ! -L "${cur_binary}" ]; then
      cur_binary_name="$(basename "${cur_binary}")"
      dylibbundler \
        --fix-file "${EXE_DIR}/${cur_binary_name}" \
        --bundle-deps \
        --dest-dir "${FRAMEWORKS_DIR}/" \
        --install-path "@executable_path/../Frameworks/" \
        --create-dir \
        --overwrite-files
    fi
  done

  typeset intermediate_lib_dir=""
  for cur_binary in ${PULSEAUDIO_LIBRARIES_FULL[@]}; do
    intermediate_lib_dir="$(lazy_canonical_path "$(dirname "${cur_binary}")/")"
    intermediate_lib_dir="${intermediate_lib_dir##"$(lazy_canonical_path "${MACPORTS_PREFIX}/lib/")"}"

    if [ ! -L "${cur_binary}" ]; then
      cur_binary_name="$(basename "${cur_binary}")"
      echo "Handling ${cur_binary} => ${intermediate_lib_dir}/${cur_binary_name}."

      typeset nesting_level="$(get_nesting_level "${intermediate_lib_dir}")"

      dylibbundler \
        --fix-file "${FRAMEWORKS_DIR}/${intermediate_lib_dir}/${cur_binary_name}" \
        --bundle-deps \
        --dest-dir "${FRAMEWORKS_DIR}/${intermediate_lib_dir}" \
        --install-path "@executable_path/$(repeat_str "../" "${nesting_level}")../Frameworks/${intermediate_lib_dir}" \
        --create-dir \
        --overwrite-files
    fi
  done

  phase "Deduplicating PulseAudio libraries and dependencies"
  typeset -r base_dir="$(lazy_canonical_path "${FRAMEWORKS_DIR}")"

  typeset -a all_files
  typeset entry=""
  while read -r -d '' entry; do
    typeset sanitized_entry="$(lazy_canonical_path "${entry}")"
    echo "Adding ${sanitized_entry} to all files"
    all_files+=( "${sanitized_entry}" )
  done < <(gfind "${base_dir}" -type 'f' -print0)

  typeset -a top_files
  for entry in "${all_files[@]}"; do
    typeset relative_path="${entry##"$(lazy_canonical_path "${base_dir}/")"}"
    typeset tmp_regex='^[^/]+$'
    echo "Checking ${relative_path} against regex '${tmp_regex}'"
    if [[ "${relative_path}" =~ ${tmp_regex} ]]; then
      echo "${relative_path} is top file, adding to array."
      top_files+=( "${relative_path}" )
    fi
  done

  typeset -a duplicates
  for entry in "${all_files[@]}"; do
    typeset relative_path="${entry##"$(lazy_canonical_path "${base_dir}/")"}"
    typeset file_name="$(basename "${entry}")"
    typeset top_entry=""
    for top_entry in "${top_files[@]}"; do
      if [ "${top_entry}" != "${relative_path}" ]; then
        if [ "${file_name}" = "${top_entry}" ]; then
          echo "Adding duplicate: ${relative_path}"
          duplicates+=( "${relative_path}" )
        fi
      fi
    done
  done

  echo "duplicates array before:"
  for entry in "${duplicates[@]}"; do
    echo "${entry}"
  done

  typeset -i i="0"
  for i in "${!duplicates[@]}"; do
    entry="${duplicates[${i}]}"
    typeset special_file_regex=""
    for special_file_regex in "${special_files_regex[@]}"; do
      typeset tmp_regex='^'"${special_file_regex}"'$'
      if [[ "${entry}" =~ ${tmp_regex} ]]; then
        cp -v "${base_dir}/$(basename "${entry}")" "${base_dir}/$(dirname "${special_file_regex}")/"
        duplicates[${i}]="$(basename "${entry}")"
        echo "Renamed ${entry} in duplicates array to ${duplicates[${i}]}"
      fi
    done
  done

  echo "duplicates array after:"
  for entry in "${duplicates[@]}"; do
    echo "${entry}"
  done

  for entry in "${duplicates[@]}"; do
    rm -v "${base_dir}/${entry}"
    typeset -i i="0"
    for i in "${!all_files[@]}"; do
      typeset all_entry="${all_files[${i}]}"
      typeset relative_path="${all_entry##"$(lazy_canonical_path "${base_dir}/")"}"
      if [ "${relative_path}" = "${entry}" ]; then
        unset all_files[${i}]
      fi
    done
  done

  echo "New value for all_files:"
  for entry in "${all_files[@]}"; do
    echo "${entry}"
  done

  echo "Duplicates-to-real map:"
  # Build complementary array to duplicates.
  typeset -a to_files
  for entry in "${duplicates[@]}"; do
    typeset filename="$(basename "${entry}")"

    typeset all_entry=""
    for all_entry in "${all_files[@]}"; do
      typeset all_entry_filename="$(basename "${all_entry}")"

      if [ -n "${filename}" ] && [ -n "${all_entry_filename}" ]; then
        if [ "${filename}" = "${all_entry_filename}" ]; then
          typeset dependency_format="$(lazy_canonical_path "${dependency_base_format}/${all_entry##${base_dir}}")"
          to_files+=( "${dependency_format}" )

          echo "${entry} => ${dependency_format}"

          # There should be only one entry matching, so we can save a bit of time and break out of the loop.
          # Even more importantly, we only want one entry for each duplicates entry anyway...
          break
        fi
      else
        echo "ERROR: empty file name while matching duplicates with non-duplicates." >&2
        echo "ERROR: duplicate entry: \"${entry}\"" >&2
        echo "ERROR: real entry: \"${all_entry}\"" >&2
        exit 1
      fi
    done
  done

  # Add binaries to all_files as well.
  typeset entry=""
  while read -r -d '' entry; do
    echo "Adding ${entry} to all files"
    all_files+=( "${entry}" )
  done < <(gfind "${EXE_DIR}" -type 'f' -executable -print0)

  # Try to fixup files broken by duplicates removal.
  for all_entry in "${all_files[@]}"; do
    typeset otool_out=""
    typeset -i tmp_ret="0"

    # Newer otool versions terminate with a non-zero return code on errors,
    # while the classic/legacy versions do not. We need to make sure our
    # script doesn't terminate just because otool returns a non-zero exit
    # status.
    set +e
    otool_out="$(otool -L "${all_entry}")"
    tmp_ret="${?}"
    set -e

    # If the return code was non-zero, skip this file.
    # A return code of zero does not automatically mean that otool finished
    # successfully, so in that case throw otool's stdout into parse_otool_output().
    if [ "${tmp_ret}" -eq "0" ]; then
      # Don't merge the declaration and initialization with the real value assignment.
      # We need the return value of parse_otool_output(), but running
      # typeset foo="$(bar)" will give us the return value of typeset, not bar().
      typeset dependencies=""
      set +e
      dependencies="$(parse_otool_output "${otool_out}")"
      tmp_ret="${?}"
      set -e
    fi

    if [ "${tmp_ret}" -ne "0" ]; then
      echo "WARNING: otool returned error for file: ${all_entry}" >&2
      echo "WARNING: skipping." >&2
      continue
    fi

    typeset line=""
    while read -r line; do
      #echo "dependency of ${all_entry}: ${line}"

      typeset duplicate_entry=""
      typeset -i i="0"
      for i in "${!duplicates[@]}"; do
        typeset duplicate_entry="${duplicates[${i}]}"
        #echo "checking for duplicate ${duplicate_entry}"
        typeset duplicate_format="$(lazy_canonical_path "${dependency_base_format}/${duplicate_entry}")"

        if [ -n "${line}" ] && [ -n "${duplicate_format}" ]; then
          if [ "${line}" = "${duplicate_format}" ]; then
            install_name_tool -change "${line}" "${to_files[${i}]}" "${all_entry}"
          fi
        else
          echo "ERROR: empty file name while replacing duplicate dependencies." >&2
          echo "ERROR: for file ${all_entry}" >&2
          echo "ERROR: at dependency ${line}" >&2
          echo "ERROR: duplicate entry: \"${duplicate_entry}\"" >&2
          echo "ERROR: dependency: \"${line}\"" >&2
          exit 1
        fi
      done
    done <<< "${dependencies}"
  done

  phase "Bundling up using macdeployqt"
  macdeployqt "${APPBUNDLE}" -verbose=2

  phase "Creating DMG"
  ${PKG_DMG} \
    --source "${APPBUNDLE}" \
    --sourcefile \
    --target "${DMGFILE}" \
    --volname "x2goclient" \
    --verbosity 2 \
    --mkdir "/.background" \
    --copy "${TOP_DIR}/res/img/png/macinstaller_background.png:/.background" \
    --copy "${TOP_DIR}/res/osxbundle/macdmg.DS_Store:/.DS_Store" \
    --copy "${TOP_DIR}/LICENSE" \
    --copy "${TOP_DIR}/COPYING" \
    --symlink "/Applications" \
    --icon "${TOP_DIR}/res/img/icons/dmg/x2go-mac-dmg.icns" \
    --format "UDBZ"
fi

popd