#!/bin/sh
#
# Copyright (C) 2005-2020 Centre National d'Etudes Spatiales (CNES)
#
# This file is part of Orfeo Toolbox
#
#     https://www.orfeo-toolbox.org/
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
set -e

# verify basic tools are here (install_name_tool is specific macOS)
which sh >/dev/null 2>&1 || ret=$?
if [ ! -z "$ret" ] ; then echo "ERROR: Missing tool 'which'"; exit 1 ; fi
for tool in dirname find file grep cut sed chmod cat readlink install_name_tool ; do
  if [ -z "$(which $tool)" ] ; then
    echo "ERROR: Missing tool '${tool}'"
    echo "Please install it and make sure it can be found from PATH"
    exit 1;
  fi
done

DIRNAME_0=$(dirname "$0")
cd  "$DIRNAME_0" || exit 1

# NOTE: $OUT_DIR is also used in make_sylinks script (see below)
# Avoid any pre-mature optimization on variable names here.

OUT_DIR=$(pwd)

#do not move below 3 lines. it must be before "unset LD_LIBRARY_PATH" code
if [ -f "$OUT_DIR/lib/python/_otbApplication.so" ] ; then
  chmod +x "$OUT_DIR/setup_python.sh"
  ./setup_python.sh || setup_python_ret=$?
fi

# no interference with DYLD_LIBRARY_PATH and DYLD_FALLBACK_LIBRARY_PATH
unset DYLD_LIBRARY_PATH
unset DYLD_FALLBACK_LIBRARY_PATH

echo "Configuring..."

LIB_FILES=$(find "./lib" "./plugins" -type f -exec file {} \; | grep -i ": Mach-O "|cut -d ':' -f1)
BIN_FILES=$(find "./bin" -type f -exec file {} \; | grep -i ": Mach-O .*executable"|cut -d ':' -f1)
# run install_name_tool
for input_file in $LIB_FILES $BIN_FILES; do
    #echo "adding rpath to $OUT_DIR/$lib_file"
    input_file_old_rpaths=$(otool -l "$input_file" | grep -A 3 "LC_RPATH" | grep -oE 'path .* \(offset' | cut -d ' ' -f 2)
    for rpath_old in $input_file_old_rpaths; do
      install_name_tool "-delete_rpath" "$rpath_old" "$input_file"
    done
    install_name_tool "-add_rpath" "$OUT_DIR/lib" "$input_file"
done

#install uninstall_otb script
echo "Installing uninstall script for OTB 'tools/uninstall_otb.sh'"
sed -i "" "s,mapla.sh,Mapla.app -r,g" "$OUT_DIR/tools/uninstall_otb.sh"
sed -i "" "s,monteverdi.sh,Monteverdi.app -r,g" "$OUT_DIR/tools/uninstall_otb.sh"
sed -i "" "s,OUT_DIR,$OUT_DIR,g" "$OUT_DIR/tools/uninstall_otb.sh"
chmod +x "$OUT_DIR/tools/uninstall_otb.sh"

mkdir "$OUT_DIR/template.app/Contents/MacOS"
mkdir "$OUT_DIR/template.app/Contents/Resources"
cp -r "$OUT_DIR/template.app" "$OUT_DIR/Mapla.app"
cp -r "$OUT_DIR/template.app" "$OUT_DIR/Monteverdi.app"
mv "$OUT_DIR/mapla.sh" "Mapla.app/Contents/MacOS/Mapla"
mv "$OUT_DIR/monteverdi.sh" "Monteverdi.app/Contents/MacOS/Monteverdi"
rm -fr "template.app"
LONG_VERSION_STRING=@Monteverdi_VERSION_MAJOR@.@Monteverdi_VERSION_MINOR@.@Monteverdi_VERSION_PATCH@
SHORT_VERSION_STRING=@Monteverdi_VERSION_MAJOR@.@Monteverdi_VERSION_MINOR@
#sed qt.conf
sed -i "" "s,Prefix=..,Prefix=$OUT_DIR,g" "$OUT_DIR/bin/qt.conf"

cp "$OUT_DIR/bin/qt.conf" "$OUT_DIR/Mapla.app/Contents/Resources/qt.conf"
cp "$OUT_DIR/Monteverdi.icns" "Mapla.app/Contents/Resources/"
sed -i "" "s,STARTUP_FILE,Mapla,g" "Mapla.app/Contents/Info.plist"
sed -i "" "s,LONG_VERSION,$LONG_VERSION_STRING,g" "Mapla.app/Contents/Info.plist"
sed -i "" "s,SHORT_VERSION,$SHORT_VERSION_STRING,g" "Mapla.app/Contents/Info.plist"
sed -i "" "s,OUT_DIR,$OUT_DIR,g" "$OUT_DIR/Mapla.app/Contents/MacOS/Mapla"
chmod +x "$OUT_DIR/Mapla.app/Contents/MacOS/Mapla"

cp "$OUT_DIR/bin/qt.conf" "$OUT_DIR/Monteverdi.app/Contents/Resources/qt.conf"
cp "$OUT_DIR/Monteverdi.icns" "Monteverdi.app/Contents/Resources/"
sed -i "" "s,STARTUP_FILE,Monteverdi,g" "Monteverdi.app/Contents/Info.plist"
sed -i "" "s,LONG_VERSION,$LONG_VERSION_STRING,g" "Monteverdi.app/Contents/Info.plist"
sed -i "" "s,SHORT_VERSION,$SHORT_VERSION_STRING,g" "Monteverdi.app/Contents/Info.plist"
sed -i "" "s,OUT_DIR,$OUT_DIR,g" "$OUT_DIR/Monteverdi.app/Contents/MacOS/Monteverdi"
chmod +x "$OUT_DIR/Monteverdi.app/Contents/MacOS/Monteverdi"

sed -i "" "s,OUT_DIR,$OUT_DIR,g" "$OUT_DIR/otbenv.profile"
chmod +x "$OUT_DIR/otbenv.profile"

printf %s\\n ""
printf %s\\n "source './otbenv.profile' file to set required environment variables"
printf %s\\n "eg: '. $OUT_DIR/otbenv.profile'"
printf %s\\n "You can also copy above line to  ~/.profile to keep changes permanently!"
printf %s\\n "More documentation can be found in $OUT_DIR/README"


# echo "Creating symbolic links..."
. "./make_symlinks"
rm -f "$OUT_DIR/make_symlinks"
rm -f "$OUT_DIR/setup_python.sh"
rm -f "$OUT_DIR/Monteverdi.icns"
rm -f "$OUT_DIR/pkgsetup"

#open "$OUT_DIR"
