#!/bin/sh

DEBUG=0

TAR_FLAG=
if [ "x$DEBUG" = "x1" ]; then
  TAR_FLAG=v
fi

mkdir -p "@ParaView_BINARY_DIR@/paraview_bundle/paraview.app/Contents/Frameworks"
mkdir -p "@ParaView_BINARY_DIR@/paraview_bundle/paraview.app/Contents/MacOS"
mkdir -p "@ParaView_BINARY_DIR@/paraview_bundle/paraview.app/Contents/Resources"
cd "@ParaView_BINARY_DIR@/paraview_bundle"

echo "Copy ParaView bundle"
(cd "@ParaView_BINARY_DIR@/bin" && tar c${TAR_FLAG}f - paraview.app) | tar xf -
(cd "@ParaView_BINARY_DIR@/bin" && tar c${TAR_FLAG}f - pvserver pvdataserver pvrenderserver pvpython) | (cd paraview.app/Contents/MacOS && tar xf -)

echo "Copy ParaView Documentation"
(cd "@ParaView_BINARY_DIR@/" && tar c${TAR_FLAG}f - Documentation pqClient.adp) | (cd paraview.app/Contents/MacOS && tar xf -)

echo "Copy ParaView libraries"
(cd "@ParaView_BINARY_DIR@/bin" && tar c${TAR_FLAG}f - *.dylib *.so) | (cd paraview.app/Contents/MacOS && tar xf - )

echo "Copy ParaView Python scripts"
(cd "@ParaView_BINARY_DIR@/Utilities/VTKPythonWrapping" && tar c${TAR_FLAG}f - paraview) | (cd paraview.app/Contents/Resources && tar xf - )

#exit

QT_LIB_DIR="@QT_LIBRARY_DIR@"
QT_BIN_DIR="$(dirname @QT_MOC_EXECUTABLE@)"

echo "Copy Qt libraries"
(cd "${QT_LIB_DIR}" && tar cf - *.dylib) | (cd paraview.app/Contents/MacOS && tar xvf - )

echo "Copy QAssistant"
(cd "${QT_BIN_DIR}" && tar cf - assistant.app) | (cd paraview.app/Contents/MacOS && tar xvf - )

echo "Copy Qt frameworks"
for a in QtCore QtGui QtNetwork QtXml QtAssistant; do
  (cd "${QT_LIB_DIR}" &&  tar cf - $a.framework) | (cd paraview.app/Contents/Frameworks && tar xvf -) 
done

EXECUTABLES="paraview.app/Contents/MacOS/paraview"
EXECUTABLES="${EXECUTABLES} paraview.app/Contents/MacOS/pvserver"
EXECUTABLES="${EXECUTABLES} paraview.app/Contents/MacOS/pvrenderserver"
EXECUTABLES="${EXECUTABLES} paraview.app/Contents/MacOS/pvdataserver"
if [ -e paraview.app/Contents/MacOS/pvpython ]; then
    EXECUTABLES="${EXECUTABLES} paraview.app/Contents/MacOS/pvpython"
fi
SUB_EXECUTABLES="${SUB_EXECUTABLES} paraview.app/Contents/MacOS/assistant.app/Contents/MacOS/assistant"

echo "Stage 1 - fix all the Qt dylib's"
for a in ${QT_LIB_DIR}/*.dylib; do
  if [ -e $a  ]; then
      echo -n .
      file=$(basename $a)
      for executable in ${EXECUTABLES}; do
          install_name_tool "${executable}" -change $a @executable_path/${file}
      done
      for executable in ${SUB_EXECUTABLES}; do
          install_name_tool "${executable}" -change $a @executable_path/../../../${file}
      done
      install_name_tool paraview.app/Contents/MacOS/${file} -id @executable_path/${file}
      
      if [ "x$DEBUG" = "x1" ]; then
          echo "Stage 1.a.$a"
      fi
      for lib in @ParaView_BINARY_DIR@/bin/*.so @ParaView_BINARY_DIR@/bin/*.dylib; do
          libfile=$(basename ${lib})
          install_name_tool paraview.app/Contents/MacOS/${file} -change ${lib} @executable_path/${libfile}
      done
      if [ "x$DEBUG" = "x1" ]; then
          echo "Stage 1.b.$a"
      fi
      for lib in ${QT_LIB_DIR}/*.dylib; do
          libfile=$(basename ${lib})
          install_name_tool paraview.app/Contents/MacOS/${file} -change ${lib} @executable_path/${libfile}
      done
  fi
done

VTK_USE_FFMPEG=@VTK_USE_FFMPEG_ENCODER@
if [ "x$VTK_USE_FFMPEG" = "xON" ]; then
    FFMPEG_LIB_DIR=@FFMPEG_avcodec_LIBRARY@
    FFMPEG_LIB_DIR=`dirname $FFMPEG_LIB_DIR`

    echo ""
    echo "Stage 1.a - Install FFMPEG libraries"

    echo "FFMPEG is ON, libdir: $FFMPEG_LIB_DIR"
    for a in ${FFMPEG_LIB_DIR}/libavcodec*dylib* ${FFMPEG_LIB_DIR}/libavformat*dylib* ${FFMPEG_LIB_DIR}/libavutil*dylib*; do
	echo -n .
	cp $a  paraview.app/Contents/MacOS/
    done
    echo "."
fi

echo ""
echo "Stage 2 - Fix all the remaining dylib's"
for a in @ParaView_BINARY_DIR@/bin/*.so @ParaView_BINARY_DIR@/bin/*.dylib ${FFMPEG_LIB_DIR}/libavcodec*dylib* ${FFMPEG_LIB_DIR}/libavformat*dylib* ${FFMPEG_LIB_DIR}/libavutil*dylib*; do
    if [ -e $a ]; then
        echo -n .
        file=$(basename $a)
        for executable in ${EXECUTABLES}; do
            install_name_tool "${executable}" -change $a @executable_path/${file}
        done
        for executable in ${SUB_EXECUTABLES}; do
            install_name_tool "${executable}" -change $a @executable_path/../../../${file}
        done
        install_name_tool paraview.app/Contents/MacOS/${file} -id @executable_path/${file}
        
        if [ "x$DEBUG" = "x1" ]; then
            echo "Stage 2.a.$a"
        fi
        for lib in @ParaView_BINARY_DIR@/bin/*.dylib ${FFMPEG_LIB_DIR}/libavcodec*dylib* ${FFMPEG_LIB_DIR}/libavformat*dylib* ${FFMPEG_LIB_DIR}/libavutil*dylib*; do
            libfile=$(basename ${lib})
            install_name_tool paraview.app/Contents/MacOS/${file} -change ${lib} @executable_path/${libfile}
        done
        if [ "x$DEBUG" = "x1" ]; then
            echo "Stage 2.b.$a"
        fi
        for lib in ${QT_LIB_DIR}/*.dylib; do
            libfile=$(basename ${lib})
            install_name_tool paraview.app/Contents/MacOS/${file} -change ${lib} @executable_path/${libfile}
        done
    fi
done

echo ""
echo "Stage 3 - Fix all the Qt Frameworks"
for a in QtCore QtGui QtNetwork QtXml QtAssistant; do
  install_name_tool -id @executable_path/../Frameworks/$a.framework/Versions/4/$a paraview.app/Contents/Frameworks/$a.framework/Versions/4/$a
  for executable in ${EXECUTABLES}; do
    install_name_tool "${executable}" -change ${QT_LIB_DIR}/$a.framework/Versions/4/$a @executable_path/../Frameworks/$a.framework/Versions/4/$a
  done
  for executable in ${SUB_EXECUTABLES}; do
    install_name_tool "${executable}" -change ${QT_LIB_DIR}/$a.framework/Versions/4/$a @executable_path/../../../../Frameworks/$a.framework/Versions/4/$a
  done

  if [ "x$DEBUG" = "x1" ]; then
    echo "Stage 3.a.$a"
  fi
  for b in QtCore QtGui QtNetwork QtXml QtAssistant; do
    install_name_tool -change ${QT_LIB_DIR}/$b.framework/Versions/4/$b @loader_path/../../../$b.framework/Versions/4/$b paraview.app/Contents/Frameworks/$a.framework/Versions/4/$a
    install_name_tool -change ${QT_LIB_DIR}/$b.framework/Versions/4/$b @loader_path/../../../$b.framework/Versions/4/$b paraview.app/Contents/Frameworks/$a.framework/Versions/Current/$a
    install_name_tool -change ${QT_LIB_DIR}/$b.framework/Versions/4/$b @loader_path/../$b.framework/Versions/4/$b paraview.app/Contents/Frameworks/$a.framework/$a
  done
  if [ "x$DEBUG" = "x1" ]; then
    echo "Stage 3.b.$a"
  fi
  for b in $(ls paraview.app/Contents/MacOS/*.dylib); do
    install_name_tool -change ${QT_LIB_DIR}/$a.framework/Versions/4/$a @executable_path/../Frameworks/$a.framework/Versions/4/$a $b
  done
done

echo "Installing python libraries"
mkdir paraview.app/Contents/Frameworks/Python.Framework
# We are assuming that python is installed as a framework. This will fail otherwise.
PYTHON_FRAMEWORK_ROOT=@PYTHON_INCLUDE_PATH@/..
if [ -e ${PYTHON_FRAMEWORK_ROOT}/Python ];  then
    python_root=${PYTHON_FRAMEWORK_ROOT}
elif [ -e ${PYTHON_FRAMEWORK_ROOT}/../../../Python ]; then
    python_root=${PYTHON_FRAMEWORK_ROOT}/../../..
fi
cp ${python_root}/Python paraview.app/Contents/Frameworks/Python.Framework
chmod u+w paraview.app/Contents/Frameworks/Python.Framework/Python

echo ""
echo "Stage 4 - Fix all the Python Frameworks"
for a in Python; do
  lib_name=paraview.app/Contents/Frameworks/$a.framework/$a
  if [ -e ${lib_name} ]; then
      # Try to find the line that shows the library name. This is what we will change.
      old_path=`otool -L ${lib_name} | grep $a | cut -f1 -d\( | grep -v :`
      install_name_tool -id @executable_path/../Frameworks/$a.framework/$a ${lib_name}
      for executable in ${EXECUTABLES}; do
	  install_name_tool "${executable}" -change ${old_path}  @executable_path/../Frameworks/$a.framework/$a
      done
      for executable in ${SUB_EXECUTABLES}; do
	  install_name_tool "${executable}" -change ${old_path}  @executable_path/../Frameworks/$a.framework/$a
      done
      
      if [ "x$DEBUG" = "x1" ]; then
	  echo "Stage 3.a.$a"
      fi
      for b in $(ls paraview.app/Contents/MacOS/*.dylib); do
	  install_name_tool -change ${old_path}  @executable_path/../Frameworks/$a.framework/$a $b
      done
      for b in $(ls paraview.app/Contents/MacOS/*.so); do
	  install_name_tool -change ${old_path}  @executable_path/../Frameworks/$a.framework/$a $b
      done
  fi
done

echo "Remove unnecessary stuff"
rm -rf paraview.app/Contents/Frameworks/*.framework/Versions/Current/Headers
rm -rf paraview.app/Contents/Frameworks/*.framework/*debug*
rm -rf paraview.app/Contents/Frameworks/*.framework/*.prl
rm -rf paraview.app/Contents/Frameworks/*.framework/Versions/Current/*debug*
rm -rf paraview.app/Contents/MacOS/doc/CVS
