#!/bin/sh

# InstallPrefix environment variable may be set by caller...
# If not, default to "$DESTDIR"
#
if [ "x$InstallPrefix" = "x" ]; then
  InstallPrefix="$DESTDIR"
  echo "info: Using default InstallPrefix value:"
  echo "      $InstallPrefix"
fi

ParaViewBinaryDir="@ParaView_BINARY_DIR@"
ParaViewExeOutputDir="@EXECUTABLE_OUTPUT_PATH@"
ParaViewExeName="@PV_EXE_NAME@"
ParaView_App="$ParaViewExeOutputDir/$ParaViewExeName.app"

# Remove the previous app package so we start with a clean bundle
#
echo "Remove previous bundle"
rm -rf "$InstallPrefix/ParaView @PARAVIEW_VERSION_FULL@.app"


# Copy paraview.app from the build tree to the install tree:
#
echo "Copy paraview.app from the build tree to $InstallPrefix/"
mkdir -p "$InstallPrefix"
cp -Rp "$ParaView_App" "$InstallPrefix/"
mv "$InstallPrefix/paraview.app" "$InstallPrefix/ParaView @PARAVIEW_VERSION_FULL@.app"

if [ ! -f @VISTRAILS_PLUGIN_APP@ ]; then
  echo "Copy VisTrailsPlug.app to $InstallPrefix/"
  VisTrails_App="@VISTRAILS_PLUGIN_APP@"
  cp -Rp "$VisTrails_App" "$InstallPrefix/"
fi


# Now operate on the bundle in the install tree:
#
ParaView_App="$InstallPrefix/ParaView @PARAVIEW_VERSION_FULL@.app"

BinDir="$ParaView_App/Contents/bin"
FrameworksDir="$ParaView_App/Contents/Frameworks"
LibrariesDir="$ParaView_App/Contents/Libraries"
PluginsDir="$ParaView_App/Contents/Plugins"
PythonDir="$ParaView_App/Contents/Python"
SupportDir="$ParaView_App/Contents/Support"


# Create directories in the install tree bundle
#
mkdir -p "$BinDir"
mkdir -p "$FrameworksDir"
mkdir -p "$LibrariesDir"
mkdir -p "$PluginsDir"
mkdir -p "$PluginsDir/sqldrivers"
mkdir -p "$PluginsDir/imageformats"
mkdir -p "$PythonDir"
mkdir -p "$SupportDir"

# Copy the command line tools into bin
echo "Copying command line tools into $BinDir"
cp -p "@EXECUTABLE_OUTPUT_PATH@/pvbatch" "$BinDir"
cp -p "@EXECUTABLE_OUTPUT_PATH@/pvblot" "$BinDir"
cp -p "@EXECUTABLE_OUTPUT_PATH@/pvdataserver" "$BinDir"
cp -p "@EXECUTABLE_OUTPUT_PATH@/pvpython" "$BinDir"
cp -p "@EXECUTABLE_OUTPUT_PATH@/pvrenderserver" "$BinDir"
cp -p "@EXECUTABLE_OUTPUT_PATH@/pvserver" "$BinDir"

# Copy the ".plugins" configuration file.
echo "Copying $ParaViewExeOutputDir/.plugins to $SupportDir"
cp -p "$ParaViewExeOutputDir/.plugins" "$SupportDir/"

# Copy the python scripts into the Python directory
#
echo "Copy python scripts"
cp -Rp "${ParaViewBinaryDir}/Utilities/VTKPythonWrapping/site-packages/" "$PythonDir/"

pushd "$ParaViewExeOutputDir"
cp -p *Python.so "$LibrariesDir/"
popd

# Create an empty qt.conf so that we don't try to load Qt plugins.
touch "$ParaView_App/Contents/Resources/qt.conf"

# Copy in the Plugins
packaged_plugin_names="@distributed_plugins@"
IFS="\;"
for plugin in $packaged_plugin_names; do
  echo "Copying lib${plugin}.dylib into $PluginsDir/"
  cp -p "@EXECUTABLE_OUTPUT_PATH@/lib${plugin}.dylib" $PluginsDir/
done

# Copy sqlite qt plugin
cp -p "@QT_PLUGINS_DIR@/sqldrivers/libqsqlite.dylib" $PluginsDir/sqldrivers/
cp -p "@QT_PLUGINS_DIR@/imageformats/libqjpeg.dylib" $PluginsDir/imageformats/

# Remove unneeded build-byproduct files that were copied by the
# above recursive directory copies:
#
rm -rf "$PythonDir/paraview/pv_compile_complete"
