#!/bin/bash

# //TODO: Add Icon File and copy it to the bundle
# 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 the JPEG, TIFF, and PNG, and Boost libraries. Each of these
# libraries is assumed to have been built with some sort of rpath already. We are
# dependant on that fact.

BASE_DIR="@CMAKE_CURRENT_BINARY_DIR@"
cd ${BASE_DIR}

dylib=".dylib"
PLUGINS_DIR_NAME="Lib"
APPLICATION_APP_BUNDLE="@PROGNAME@.app"
APPLICATION_BINDIR="${APPLICATION_APP_BUNDLE}/Contents/MacOS"
APPLICATION_APP_NAME="${APPLICATION_BINDIR}/@PROGNAME@"
PLUGINS_PATH="${APPLICATION_APP_BUNDLE}/Contents/${PLUGINS_DIR_NAME}"
FRAMEWORKS_PATH="${APPLICATION_APP_BUNDLE}/Contents/Frameworks"
RPATH_PLUGIN_PATH="@executable_path/../${PLUGINS_DIR_NAME}"
RPATH_FRAMEWORK_PATH="@executable_path/../Frameworks"

DEBUG=0

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

# Create the PlugIns and Frameworks Directories
mkdir -p "${PLUGINS_PATH}"
mkdir -p "${FRAMEWORKS_PATH}"

get_libraries() {
  #echo "-----------getting libraries for $1"
  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
  #echo "----finished getting libraries for $1"
}

update_library() {
   lib="$1"
   path=$(dirname ${lib})
   file=$(basename ${lib})
   if [ ! -f "${BASE_DIR}/${PLUGINS_PATH}/${file}" ] 
   then 
     echo "* Installing Library -->$1<-- into ${APPLICATION_APP_BUNDLE} " 
     cp "${lib}" "${BASE_DIR}/${PLUGINS_PATH}" || exit 1
     #chmod 755 "${BASE_DIR}/${PLUGINS_PATH}/${file}"
     install_name_tool -id "${RPATH_PLUGIN_PATH}/${file}" "${BASE_DIR}/${PLUGINS_PATH}/${file}"
     install_name_tool -change "${lib}" "${RPATH_PLUGIN_PATH}/${file}" "$2"
     get_libraries "${BASE_DIR}/${PLUGINS_PATH}/${file}"
   else
     # Change internal id of the Library
     install_name_tool -id "${RPATH_PLUGIN_PATH}/${file}" "${BASE_DIR}/${PLUGINS_PATH}/${file}"
     # Change the hugin executable to have the proper rpath for the Library
     #echo "updating $2 to use ${RPATH_PLUGIN_PATH}/${file} for ${lib}"
     install_name_tool -change "${lib}" "${RPATH_PLUGIN_PATH}/${file}" "$2"
   fi
}

add_executable() {
  # -----------------------------------------------------------------------------
  # Copy the executable into the App package 
  # -----------------------------------------------------------------------------
  echo "* Installing Binary -->$1<-- into ${APPLICATION_APP_BUNDLE} " 
  cp "$1" "${BASE_DIR}/${APPLICATION_BINDIR}" || exit 1
  get_libraries "${BASE_DIR}/${APPLICATION_BINDIR}/$(basename $1)"
}

get_libraries "${BASE_DIR}/${APPLICATION_BINDIR}/@PROGNAME@"
add_executable "${BASE_DIR}/../../tools/nona"
add_executable "${BASE_DIR}/../../deghosting/hugin_hdrmerge"
add_executable "${BASE_DIR}/../stitch_project/hugin_stitch_project"
add_executable "$(which msgfmt)"
add_executable "$(which PTroller)"
add_executable "$(which PTmasker)"
add_executable "$(which PTtiff2psd)"
add_executable "$(which enblend)"
add_executable "$(which enfuse)"

echo "*-----------------------------------------------------------*"
echo "* Creating DMG file: @CMAKE_INSTALL_PREFIX@/@PROGNAME@.dmg "
mkdmg -zlib -s "${BASE_DIR}/${APPLICATION_APP_BUNDLE}" -i "@CMAKE_INSTALL_PREFIX@/@PROGNAME@.dmg"
echo ""
