#!/bin/bash
# Creates Apple ".app" bundle for @PROJECT_NAME_UCASE@
# Note:
#   Examine linkings using `otool -L somelib.so`
#   Debug the loading of dynamic libraries using `export DYLD_PRINT_LIBRARIES=1`

# STK rawwaves directory
STK_RAWWAVE=$HOME/stk-*/rawwaves

if [ ! -d "$STK_RAWWAVE" ]; then
   STK_RAWWAVE=$(brew --prefix stk)/share/stk/rawwaves
fi
 
# Place to create ".app" bundle
APP="@CMAKE_BINARY_DIR@/@PROJECT_NAME_UCASE@.app"

MSG_COLOR='\x1B[1;36m'
COLOR_RESET='\x1B[0m'
echo -e "$MSG_COLOR\n\nCreating App Bundle \"$APP\"...$COLOR_RESET"

# Locate macdeployqt, assume homebrew & Qt5
which macdeployqt > /dev/null 2>&1
if [ $? -ne 0 ]; then
  export PATH=$PATH:$(brew --prefix qt)/bin
fi
 
# Remove any old .app bundles
rm -Rf "$APP"

# Copy/overwrite Info.plist
\cp  "@CMAKE_BINARY_DIR@/Info.plist" "@CMAKE_INSTALL_PREFIX@/"
 
# Create .app bundle containing contents from CMAKE_INSTALL_PREFIX
mkdir -p "$APP/Contents/MacOS"
mkdir -p "$APP/Contents/Frameworks"
mkdir -p "$APP/Contents/Resources"
mkdir -p "$APP/Contents/share/stk/rawwaves"
cd "@CMAKE_INSTALL_PREFIX@"
cp -R * "$APP/Contents"
cp "@CMAKE_SOURCE_DIR@/cmake/apple/"*.icns "$APP/Contents/Resources/"
cp $STK_RAWWAVE/*.raw "$APP/Contents/share/stk/rawwaves" > /dev/null 2>&1
 
# Make all libraries writable for macdeployqt
cd "$APP"
find . -type f -print0 | xargs -0 chmod u+w

lmmsbin="MacOS/@CMAKE_PROJECT_NAME@"
zynlib="lib/lmms/libzynaddsubfx.so"
zynfmk="Frameworks/libZynAddSubFxCore.dylib"
zynbin="MacOS/RemoteZynAddSubFx"
 
# Move lmms binary
mv "$APP/Contents/bin/@CMAKE_PROJECT_NAME@" "$APP/Contents/$lmmsbin"
 
# Fix zyn linking 
mv "$APP/Contents/lib/lmms/RemoteZynAddSubFx" "$APP/Contents/$zynbin"
mv "$APP/Contents/lib/lmms/libZynAddSubFxCore.dylib" "$APP/Contents/$zynfmk"

install_name_tool -change @rpath/libZynAddSubFxCore.dylib \
   @loader_path/../$zynfmk \
  "$APP/Contents/$zynbin"

install_name_tool -change @rpath/libZynAddSubFxCore.dylib \
   @loader_path/../../$zynfmk \
  "$APP/Contents/$zynlib"
  
# Replace @rpath with @loader_path for Carla
# See also plugins/carlabase/CMakeLists.txt
# This MUST be done BEFORE calling macdeployqt
install_name_tool -change @rpath/libcarlabase.dylib \
   @loader_path/libcarlabase.dylib \
  "$APP/Contents/lib/lmms/libcarlapatchbay.so"

install_name_tool -change @rpath/libcarlabase.dylib \
  @loader_path/libcarlabase.dylib \
  "$APP/Contents/lib/lmms/libcarlarack.so"

# Link lmms binary
_executables="${_executables} -executable=$APP/Contents/$zynbin"
_executables="${_executables} -executable=$APP/Contents/$zynfmk"
 
# Build a list of shared objects in target/lib/lmms
for file in "$APP/Contents/lib/lmms/"*.so; do
   _thisfile="$APP/Contents/lib/lmms/${file##*/}"
   _executables="${_executables} -executable=$_thisfile"
done
 
# Build a list of shared objects in target/lib/lmms/ladspa
for file in "$APP/Contents/lib/lmms/ladspa/"*.so; do
   _thisfile="$APP/Contents/lib/lmms/ladspa/${file##*/}"
   _executables="${_executables} -executable=$_thisfile"
done

# Finalize .app
macdeployqt "$APP" $_executables

# Carla is a standalone plugin.  Remove library, look for it side-by-side LMMS.app
# This MUST be done AFTER calling macdeployqt
#
# For example:
#    /Applications/LMMS.app
#    /Applications/Carla.app
carlalibs=$(echo "@CARLA_LIBRARIES@"|tr ";" "\n")

# Loop over all libcarlas, fix linking
for file in "$APP/Contents/lib/lmms/"libcarla*; do
   _thisfile="$APP/Contents/lib/lmms/${file##*/}"
   for lib in $carlalibs; do
     _oldpath="../../Frameworks/lib${lib}.dylib"
     _newpath="Carla.app/Contents/MacOS/lib${lib}.dylib"
     install_name_tool -change @loader_path/$_oldpath \
       @executable_path/../../../$_newpath \
       "$_thisfile"
     rm -f "$APP/Contents/Frameworks/lib${lib}.dylib"
   done
done

# Cleanup
rm -rf "$APP/Contents/bin"
echo -e "\nFinished.\n\n"
