#!/bin/bash

# This script attempts to copy needed 3rd party libraries and frameworks into 
# the application bundle. It will then attempt to set the 'install_name' for 
# each library so that it references either the PlugIns or Frameworks directory.
# The Script will change every library it can find. Each of these libraries 
# needs to have an absolute install path so we can copy it.

BASE_DIR="$2"
cd ${BASE_DIR}

APPLICATION_NAME="$1"
APPLICATION_APP_BUNDLE="${APPLICATION_NAME}.app"
  APPLICATION_BINDIR="${APPLICATION_APP_BUNDLE}/Contents/MacOS"
APPLICATION_APP_NAME="${APPLICATION_BINDIR}/${APPLICATION_NAME}"
   PLUGINS_PATH="${APPLICATION_APP_BUNDLE}/Contents/Libraries"
FRAMEWORKS_PATH="${APPLICATION_APP_BUNDLE}/Contents/Frameworks"
RPATH_PLUGIN_PATH="@executable_path/../Libraries"
RPATH_FRAMEWORK_PATH="@executable_path/../Frameworks"


echo "*-----------------------------------------------------------*"
echo "* Copying Support Libraries for ${APPLICATION_APP_BUNDLE}"
echo "* Located in ${BASE_DIR}"

mkdir -p "${PLUGINS_PATH}"
mkdir -p "${FRAMEWORKS_PATH}"

get_libraries() {
  local LIBRARIES=$(echo $(otool -X -L $1 | grep -v ${RPATH_PLUGIN_PATH} | grep -v \/System\/Library | grep -v \/usr\/lib | sed -e 's/(.*)//' | sort -u))
  if [ -n "$LIBRARIES" ]; then
    for library in $LIBRARIES
    do
      update_library $library $1
    done
  fi
  install_name_tool -delete_rpath "@CMAKE_SOURCE_DIR@/mac/ExternalPrograms/repository/lib" $1 2&>/dev/null
}

update_library() {
   local lib="$1"
   local lib_file=$(basename ${lib})
   local bin="$2"
   if [ ! -f "${BASE_DIR}/${PLUGINS_PATH}/${lib_file}" ] 
   then 
     echo "* Installing Library -->$1<-- into ${APPLICATION_APP_BUNDLE} " 
     cp "${lib}" "${BASE_DIR}/${PLUGINS_PATH}" || exit 1
     chmod 755 "${BASE_DIR}/${PLUGINS_PATH}/${lib_file}"
     get_libraries "${BASE_DIR}/${PLUGINS_PATH}/${lib_file}"
     install_name_tool -id "${RPATH_PLUGIN_PATH}/${lib_file}" "${BASE_DIR}/${PLUGINS_PATH}/${lib_file}"
   fi
   install_name_tool -change "${lib}" "${RPATH_PLUGIN_PATH}/${lib_file}" "${bin}"
}


# -----------------------------------------------------------------------------
# Copy libraries for all exetuables in APPLICATION_BINDIR
# -----------------------------------------------------------------------------

for _exe in ${BASE_DIR}/${APPLICATION_BINDIR}/*; do
  get_libraries $_exe
done


  # -----------------------------------------------------------------------------
  # Copy ExifTool into the stitchers
  # -----------------------------------------------------------------------------
if [ "${APPLICATION_APP_BUNDLE}" == "PTBatcherGUI.app" ] || [ "${APPLICATION_APP_BUNDLE}" == "HuginStitchProject.app" ]; then

  if [ ! -x "${APPLICATION_APP_BUNDLE}/Contents/Resources/ExifTool/exiftool" ]; then
    echo "* Installing ExifTool into ${APPLICATION_APP_BUNDLE} " 
    mkdir -p "${APPLICATION_APP_BUNDLE}/Contents/Resources/ExifTool"
    cp -v "@CMAKE_SOURCE_DIR@/mac/ExternalPrograms/repository/"Image-ExifTool-*/exiftool  "${APPLICATION_APP_BUNDLE}/Contents/Resources/ExifTool"
    cp -r "@CMAKE_SOURCE_DIR@/mac/ExternalPrograms/repository/"Image-ExifTool-*/lib       "${APPLICATION_APP_BUNDLE}/Contents/Resources/ExifTool"
  fi

fi

