#!/usr/bin/env bash
# Post-process the Flutter macOS Release bundle so it actually launches:
#
#   1. Wrap target/release/libchanora_bridge.dylib into a proper
#      chanora_bridge.framework bundle (Versions/A layout, Info.plist,
#      symlinks). FRB on macOS dlopen()s chanora_bridge.framework/
#      chanora_bridge — not the plain dylib.
#   2. Rewrite the dylib's LC_ID_DYLIB so dyld resolves it as
#      @rpath/chanora_bridge.framework/chanora_bridge.
#   3. Ad-hoc codesign the framework + the .app bundle.
#
# This is the macOS analogue of buildit.cmd on Windows. cocogkit /
# corrosion-style auto-integration into the Xcode project is a P1
# carryover — for now we run this script after `flutter build macos
# --release`.
#
# Usage:
#   tools/macos-postbuild.sh [Release|Debug]   (defaults to Release)

set -euo pipefail

CONFIG="${1:-Release}"
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
APP="$REPO_ROOT/apps/chanora_flutter/build/macos/Build/Products/$CONFIG/chanora_flutter.app"
DYLIB="$REPO_ROOT/target/release/libchanora_bridge.dylib"
FW="$APP/Contents/Frameworks/chanora_bridge.framework"

if [[ ! -d "$APP" ]]; then
  echo "ERROR: app bundle not found at $APP" >&2
  echo "       run \`flutter build macos --$(echo "$CONFIG" | tr '[:upper:]' '[:lower:]')\` first" >&2
  exit 1
fi
if [[ ! -f "$DYLIB" ]]; then
  echo "ERROR: bridge dylib not found at $DYLIB" >&2
  echo "       run \`cargo build --release -p chanora_bridge\` first" >&2
  exit 2
fi

# Step 1: build the chanora_bridge.framework layout.
echo "[postbuild] wrapping bridge into chanora_bridge.framework"
rm -rf "$FW"
mkdir -p "$FW/Versions/A/Resources"
cp "$DYLIB" "$FW/Versions/A/chanora_bridge"

cat > "$FW/Versions/A/Resources/Info.plist" << 'PLIST'
<?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>CFBundleExecutable</key>
  <string>chanora_bridge</string>
  <key>CFBundleIdentifier</key>
  <string>app.chanora.bridge</string>
  <key>CFBundleName</key>
  <string>chanora_bridge</string>
  <key>CFBundlePackageType</key>
  <string>FMWK</string>
  <key>CFBundleShortVersionString</key>
  <string>1.0.0</string>
  <key>CFBundleVersion</key>
  <string>1</string>
</dict>
</plist>
PLIST

ln -sf A "$FW/Versions/Current"
ln -sf Versions/Current/chanora_bridge "$FW/chanora_bridge"
ln -sf Versions/Current/Resources "$FW/Resources"

# Step 2: rewrite install_name so dyld finds the bundled copy.
echo "[postbuild] install_name_tool -id @rpath/chanora_bridge.framework/chanora_bridge"
install_name_tool \
  -id "@rpath/chanora_bridge.framework/chanora_bridge" \
  "$FW/Versions/A/chanora_bridge"

# Drop a stale plain dylib if a previous build left one alongside the
# framework.
rm -f "$APP/Contents/Frameworks/libchanora_bridge.dylib"

# Step 3: codesign. Ad-hoc identity ('-') is sufficient for local
# launches; replace with a real Developer ID when distributing.
echo "[postbuild] codesigning framework + app (ad-hoc)"
codesign --force -s - --timestamp=none "$FW/Versions/A/chanora_bridge"
codesign --force -s - --timestamp=none "$FW"
codesign --force --deep -s - --timestamp=none "$APP"

codesign --verify --deep --strict "$APP"

echo "[postbuild] OK: $APP is launchable"
