#!/bin/sh -f

if [ `uname` != Darwin ] ; then
  exit
fi

#set -x

if test $# -lt 1 ; then
  echo "Give the name of an application."
  exit
fi

obuild_app=$1
shift

if [  "${OBUILD_PLATFORM}" = "" ] ; then
  obuild_platform=`uname`
else
  obuild_platform=${OBUILD_PLATFORM}
fi

if [ "${obuild_platform}" = "" ] ; then
  echo "obuild_platform variable not defined."
  exit
fi

if [  "${OBUILD_DIR_BIN}" = "" ] ; then
  obuild_dir_bin=bin_obuild
else
  obuild_dir_bin=${OBUILD_DIR_BIN}
fi

if [ "${obuild_dir_bin}" = "" ] ; then
  echo "obuild_dir_bin variable not defined."
  exit
fi

obuild_pwd=`pwd`
obuild_snovis_path=`dirname "${obuild_pwd}"`

obuild_path_app="${obuild_snovis_path}/${obuild_dir_bin}/${obuild_app}"

if [ ! -e "${obuild_path_app}" ] ; then
  echo "${obuild_path_app} does not exists."
  exit
fi

if [ ! -x "${obuild_path_app}" ] ; then
  echo "${obuild_path_app} is not an appliction."
  exit
fi

obuild_dir_app="${obuild_snovis_path}/${obuild_dir_bin}/${obuild_app}.app"

# Create the .app structure :
/bin/rm -R -f "${obuild_dir_app}"
/bin/mkdir -p "${obuild_dir_app}/Contents/MacOS"
obuild_dir_lib="${obuild_dir_app}/Contents/lib"
/bin/mkdir -p "${obuild_dir_lib}"
obuild_dir_dll="${obuild_dir_app}/Contents/bin"
/bin/mkdir -p "${obuild_dir_dll}"

# Copy the application :
/bin/cp "${obuild_path_app}" "${obuild_dir_app}/Contents/MacOS/${obuild_app}"

# Copy dlls (libs) in Contents/bin (/lib) of the .app :
while test $# -ge 1 ; do  
  if [ -e "$1" ] ; then
    name=`basename "$1"`
    if [ "`basename ${name} .dylib`" = "${name}" ] ; then
      # .bundle
      #echo "debug : bundle : $1"
      eval /bin/cp "\"$1\"" "\"${obuild_dir_dll}\""/.
    else
      # .dylib
      #echo "debug : dylib : $1"
      eval /bin/cp "\"$1\"" "\"${obuild_dir_lib}\""/.
    fi
#  else
#    echo "$1 not found."
  fi
  shift
done
cd "${obuild_pwd}"

s_awk='{i=index($0," (compatibility");if(i==0){print $0;} else { print substr($0,0,i-1);}}'

# Get application linked libraries :
/bin/rm -f obuild_tmp obuild_tmp1 obuild_tmp2
otool -X -L  "${obuild_dir_app}/Contents/MacOS/${obuild_app}" > obuild_tmp
/usr/bin/awk -s "${s_awk}" obuild_tmp > obuild_tmp1
grep -v '/usr/lib' obuild_tmp1 | grep -v '/System/Library' > obuild_tmp2
sed -e 's# #@@#g' obuild_tmp2 > obuild_tmp3
obuild_app_libs=`cat obuild_tmp3`
/bin/rm -f obuild_tmp obuild_tmp1 obuild_tmp2 obuild_tmp3
for libi in ${obuild_app_libs}
do 
  libi=`echo ${libi} | sed -e 's#@@# #g'`
  if [ -e "${libi}" ] ; then
    eval /bin/cp "\"${libi}\"" "\"${obuild_dir_lib}/.\""
  else
    echo "${libi} not found."
  fi
done
unset libi

# Get dlls linked libraries :
cd "${obuild_dir_dll}"
obuild_app_dlls=`find . -name "*" -type f -print`
for dlla in ${obuild_app_dlls}
do 
  /bin/rm -f obuild_tmp obuild_tmp1 obuild_tmp2
  otool -X -L "${dlla}" > obuild_tmp
  /usr/bin/awk -s "${s_awk}" obuild_tmp > obuild_tmp1
  grep -v '/usr/lib' obuild_tmp1 | grep -v '/System/Library' > obuild_tmp2
  sed -e 's# #@@#g' obuild_tmp2 > obuild_tmp3
  libs=`cat obuild_tmp3`
  /bin/rm -f obuild_tmp obuild_tmp1 obuild_tmp2 obuild_tmp3
  for libi in ${libs}
  do 
    libi=`echo ${libi} | sed -e 's#@@# #g'`
    if [ -e "${libi}" ] ; then
      eval /bin/cp "\"${libi}\"" "\"${obuild_dir_lib}/.\""
    else
      echo "${libi} not found."
    fi
  done
  unset libi
  unset libs
done
unset dlla
cd "${obuild_pwd}"

# Change the install names of the application :
# Get linked libraries :
/bin/rm -f obuild_tmp obuild_tmp1 obuild_tmp2
otool -X -L "${obuild_dir_app}/Contents/MacOS/${obuild_app}" > obuild_tmp
/usr/bin/awk -s "${s_awk}" obuild_tmp > obuild_tmp1
grep -v '/usr/lib' obuild_tmp1 | grep -v '/System/Library' > obuild_tmp2
sed -e 's# #@@#g' obuild_tmp2 > obuild_tmp3
libs=`cat obuild_tmp3`
/bin/rm -f obuild_tmp obuild_tmp1 obuild_tmp2 obuild_tmp3
for libi in ${libs}
do 
  libi=`echo ${libi} | sed -e 's#@@# #g'`
  new_install_name=`basename "${libi}"`
  if [ -f "${obuild_dir_lib}/${new_install_name}" ] ; then
    new_install_name=@executable_path/../lib/${new_install_name}
    install_name_tool -change "${libi}" ${new_install_name} "${obuild_dir_app}/Contents/MacOS/${obuild_app}"
  fi
  unset new_install_name
done
unset libi
unset libs

# Change the install names of the libraries :
cd "${obuild_dir_lib}"
obuild_app_libs=`find . -name "*" -type f -print`
for liba in ${obuild_app_libs}
do 
  #echo "----> ${liba}"

  new_install_name=`basename "${liba}"`
  new_install_name=@executable_path/../lib/${new_install_name}
  install_name_tool -id ${new_install_name} "${liba}"
  unset new_install_name
  
  # Get linked libraries :
  /bin/rm -f obuild_tmp obuild_tmp1 obuild_tmp2
  otool -X -L "${liba}" > obuild_tmp
  /usr/bin/awk -s "${s_awk}" obuild_tmp > obuild_tmp1
  grep -v '/usr/lib' obuild_tmp1 | grep -v '/System/Library' > obuild_tmp2
  sed -e 's# #@@#g' obuild_tmp2 > obuild_tmp3
  libs=`cat obuild_tmp3`
  /bin/rm -f obuild_tmp obuild_tmp1 obuild_tmp2 obuild_tmp3
  for libi in ${libs}
  do 
    libi=`echo ${libi} | sed -e 's#@@# #g'`
    #echo "${libi}"
    new_install_name=`basename "${libi}"`
    if [ -f "${obuild_dir_lib}/${new_install_name}" ] ; then
      new_install_name=@executable_path/../lib/${new_install_name}
      #echo "${new_install_name}"
      install_name_tool -change "${libi}" ${new_install_name} "${liba}"
    fi
    unset new_install_name
  done
  unset libi
  unset libs
done
unset liba
cd "${obuild_pwd}"

# Change the install names of the dlls :
cd "${obuild_dir_dll}"
obuild_app_dlls=`find . -name "*" -type f -print`
for dlla in ${obuild_app_dlls}
do 
  #echo "----> ${dlla}"

  # Get linked libraries :
  /bin/rm -f obuild_tmp obuild_tmp1 obuild_tmp2
  otool -X -L "${dlla}" > obuild_tmp
  /usr/bin/awk -s "${s_awk}" obuild_tmp > obuild_tmp1
  grep -v '/usr/lib' obuild_tmp1 | grep -v '/System/Library' > obuild_tmp2
  sed -e 's# #@@#g' obuild_tmp2 > obuild_tmp3
  libs=`cat obuild_tmp3`
  /bin/rm -f obuild_tmp obuild_tmp1 obuild_tmp2 obuild_tmp3
  for libi in ${libs}
  do 
    libi=`echo ${libi} | sed -e 's#@@# #g'`
    #echo "${libi}"
    new_install_name=`basename "${libi}"`
    if [ -f "${obuild_dir_lib}/${new_install_name}" ] ; then
      new_install_name=@executable_path/../lib/${new_install_name}
      #echo "${new_install_name}"
      install_name_tool -change "${libi}" ${new_install_name} "${dlla}"
    fi
    unset new_install_name
  done
  unset libi
  unset libs
done
unset dlla
cd "${obuild_pwd}"

unset obuild_app_dlls
unset obuild_dir_dll

unset obuild_app_libs
unset obuild_dir_lib

unset obuild_path_app
unset obuild_dir_app

unset obuild_pwd
