#!/usr/bin/env bash
#
# macos_app_bundle.sh
# 
# This script builds a .app bundle for your project on macOS,
# then optionally packages it into a drag-and-drop .dmg.
#
# Usage:
#   ./macos_app_bundle.sh
#
# Modify the variables below to suit your project names, paths, plugins, etc.

set -euo pipefail

#######################
# Configuration
#######################
APP_NAME="LavaApp"             # The name of your .app
BINARY_NAME="mumu"             # Your main Rust binary name
VERSION="0.9.1"                # Used in Info.plist
IDENTIFIER="com.example.mumu"  # In Info.plist
VOLNAME="LavaApp Installer"    # Volume name for the .dmg

# Where you want final artifacts:
DIST_DIR="dist"

# List of your plugin libraries to bundle. 
# (These are the .dylib files you want inside the .app.)
# Adjust or expand as needed:
PLUGIN_NAMES=(
  "mumumath"
  "mumunet"
  "mumusys"
  "mumustring"
  "mumuarray"
  "mumutest"
  "mumuprocess"
  "mumufs"
  "mumufile"
  "mumuevent"
)

#######################
# 1) Build with cargo
#######################
echo "==> Building Rust binaries (release mode)..."
cargo build --release

# Ensure we have a fresh dist folder:
rm -rf "$DIST_DIR"
mkdir -p "$DIST_DIR"

#######################
# 2) Create .app bundle structure
#######################
APP_FOLDER="$DIST_DIR/$APP_NAME.app"
echo "==> Creating .app bundle at '$APP_FOLDER'"
mkdir -p "$APP_FOLDER/Contents/MacOS"
mkdir -p "$APP_FOLDER/Contents/Frameworks"
mkdir -p "$APP_FOLDER/Contents/Resources"

#######################
# 3) Copy main binary
#######################
echo "==> Copying main binary => $BINARY_NAME"
cp "target/release/$BINARY_NAME" "$APP_FOLDER/Contents/MacOS/"

#######################
# 4) Copy plugin .dylib files
#######################
echo "==> Copying plugin libraries..."
for plugin in "${PLUGIN_NAMES[@]}"; do
    LIBPATH="target/release/lib${plugin}.dylib"
    if [ -f "$LIBPATH" ]; then
        echo "     - found $LIBPATH => copying to Frameworks"
        cp "$LIBPATH" "$APP_FOLDER/Contents/Frameworks/"
    else
        echo "     - WARNING: $LIBPATH not found => skipping"
    fi
done

#######################
# 5) Write Info.plist
#######################
PLIST_FILE="$APP_FOLDER/Contents/Info.plist"
echo "==> Creating Info.plist => $PLIST_FILE"
cat <<EOF > "$PLIST_FILE"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
  "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>CFBundleName</key>
  <string>$APP_NAME</string>

  <key>CFBundleIdentifier</key>
  <string>$IDENTIFIER</string>

  <key>CFBundleVersion</key>
  <string>$VERSION</string>

  <key>CFBundleShortVersionString</key>
  <string>$VERSION</string>

  <key>CFBundleExecutable</key>
  <string>$BINARY_NAME</string>

  <key>CFBundlePackageType</key>
  <string>APPL</string>

  <!-- Optional icon if you have an .icns -->
  <!-- 
  <key>CFBundleIconFile</key>
  <string>AppIcon.icns</string>
  -->

</dict>
</plist>
EOF

#######################
# 6) Fix rpaths so the .dylibs are found
#######################
echo "==> Running install_name_tool => set rpaths to find Frameworks"
# The main binary:
install_name_tool -add_rpath "@executable_path/../Frameworks" "$APP_FOLDER/Contents/MacOS/$BINARY_NAME"

# You may also need to fix each .dylib if they depend on each other or if they have absolute paths.
# For example, to fix a plugin's own ID references, you might do:
# for plugin in "${PLUGIN_NAMES[@]}"; do
#     DLIB="$APP_FOLDER/Contents/Frameworks/lib${plugin}.dylib"
#     if [ -f "$DLIB" ]; then
#         install_name_tool -id "@rpath/lib${plugin}.dylib" "$DLIB"
#         # if this .dylib depends on other .dylibs, fix them similarly:
#         # install_name_tool -change /old/path/libXXX.dylib @rpath/libXXX.dylib "$DLIB"
#     fi
# done

#######################
# 7) (Optional) Sign the .app
#######################
# If you have a Developer ID certificate installed, you could do:
# codesign --deep --force --sign "Developer ID Application: YOUR_NAME (TEAMID)" "$APP_FOLDER"
# echo "==> codesign done."

#######################
# 8) Optionally create a DMG
#######################
echo "==> Packaging .dmg..."
DMG_NAME="$DIST_DIR/${APP_NAME}.dmg"
rm -f "$DMG_NAME"

# Make a temp folder to structure the DMG contents:
DMG_TEMP="$DIST_DIR/dmgtemp"
mkdir -p "$DMG_TEMP"
cp -R "$APP_FOLDER" "$DMG_TEMP/"

# Add a symlink to /Applications for drag-and-drop
ln -s /Applications "$DMG_TEMP/Applications"

# Possibly add a background image here if desired:
# mkdir -p "$DMG_TEMP/.background"
# cp path/to/your/background.png "$DMG_TEMP/.background/"

# Create the DMG
hdiutil create \
  -volname "$VOLNAME" \
  -srcfolder "$DMG_TEMP" \
  -ov \
  -format UDZO \
  "$DMG_NAME"

echo "==> Done. See $DMG_NAME"

