#!/bin/bash

# shellcheck shell=bash

readonly orig_exe="$1"
orig_dir=$(dirname "${orig_exe}")
readonly dest_dir="$2"
new_exe="${dest_dir}/$(basename "${orig_exe}")"

echo "cp \"${orig_exe}\" \"${dest_dir}\""
cp "${orig_exe}" "${dest_dir}"

extract_rpath() {
  awk '/cmd LC_RPATH/,/path/ { if ($1 == "path") print $2 }'
}
# shellcheck disable=SC2312
orig_rpath=$(otool -l "${orig_exe}" | extract_rpath)

# shellcheck disable=SC2250

if expr "X$orig_rpath" : 'X.@executable_path' >/dev/null; then
  readonly new_rpath="${orig_rpath/@executable_path/$orig_dir}"
  echo "install_name_tool -rpath \"${orig_rpath}\" \"${new_rpath}\" \"${new_exe}\""
  install_name_tool -rpath "${orig_rpath}" "${new_rpath}" "${new_exe}"
fi

# resign executable with "ad-hoc signing"
set -x
codesign --force --sign - "${new_exe}"
