#!/bin/bash
# Double-click this file on a Mac. It builds "Tap to Sibelius.app",
# signs it, and puts it in a shareable disk image next to this file,
# together with TapToSibeliusPanel.plg.
#
# Optional, for sharing with other people without a Gatekeeper warning:
#   export DEVELOPER_ID="Developer ID Application: Your Name (TEAMID)"
#   export NOTARY_PROFILE="tap2sib"      # made once with:
#   #   xcrun notarytool store-credentials tap2sib --apple-id you@example.com \
#   #     --team-id TEAMID --password <app-specific-password>
# Without those, the image is ad-hoc signed: fine on your own Mac,
# but other Macs will warn on first open.

set -u
cd "$(dirname "$0")" || exit 1

NAME="Tap to Sibelius"
VERSION="1.0"
STAGE="/tmp/tap2sib-dmg/$NAME"
DMG="./Tap to Sibelius $VERSION.dmg"
SRC="./main.swift"
[ -f "$SRC" ] || SRC="./TapToSibeliusApp.swift"
PLG="./TapToSibeliusPanel.plg"

echo ""
echo "Tap to Sibelius installer builder"
echo "================================="

if ! xcode-select -p >/dev/null 2>&1; then
  echo "One time setup: macOS needs its developer tools."
  echo "Click Install in the window that appears, wait for it to finish,"
  echo "then double-click this file again."
  xcode-select --install
  exit 0
fi

if [ ! -f "$SRC" ]; then
  echo "Getting the app source..."
  SRC="/tmp/TapToSibeliusApp.swift"
  curl -fsSL https://tap2sib.lovable.app/TapToSibeliusApp.swift -o "$SRC" || {
    echo "Could not download the app source. Check the internet connection."
    exit 1
  }
fi

if [ ! -f "$PLG" ]; then
  echo "Getting the Sibelius panel..."
  PLG="/tmp/TapToSibeliusPanel.plg"
  curl -fsSL https://tap2sib.lovable.app/TapToSibeliusPanel.plg -o "$PLG" || {
    echo "Could not download TapToSibeliusPanel.plg."
    exit 1
  }
fi

rm -rf "/tmp/tap2sib-dmg"
APP="$STAGE/$NAME.app"
mkdir -p "$APP/Contents/MacOS" "$APP/Contents/Resources" || exit 1

cat > "$APP/Contents/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>CFBundleName</key><string>Tap to Sibelius</string>
  <key>CFBundleDisplayName</key><string>Tap to Sibelius</string>
  <key>CFBundleExecutable</key><string>TapToSibelius</string>
  <key>CFBundleIdentifier</key><string>app.lovable.tap2sib</string>
  <key>CFBundleVersion</key><string>1.0</string>
  <key>CFBundleShortVersionString</key><string>1.0</string>
  <key>CFBundlePackageType</key><string>APPL</string>
  <key>LSMinimumSystemVersion</key><string>13.0</string>
  <key>LSUIElement</key><true/>
  <key>NSHighResolutionCapable</key><true/>
</dict>
</plist>
PLIST

echo "Building the app..."
if ! swiftc -O "$SRC" -o "$APP/Contents/MacOS/TapToSibelius"; then
  echo "Build failed. Send me the message above."
  exit 1
fi

if [ -n "${DEVELOPER_ID:-}" ]; then
  echo "Signing with your Developer ID..."
  if ! codesign --force --timestamp --options runtime \
      --sign "$DEVELOPER_ID" "$APP"; then
    echo "Signing failed. Check DEVELOPER_ID matches a certificate in your keychain."
    exit 1
  fi
else
  echo "No DEVELOPER_ID set, using an ad-hoc signature (local use only)."
  codesign --force --sign - "$APP" >/dev/null 2>&1
fi

cp "$PLG" "$STAGE/TapToSibeliusPanel.plg" || exit 1
ln -s /Applications "$STAGE/Applications"

cat > "$STAGE/Read me first.txt" <<'TXT'
Tap to Sibelius

1. Drag "Tap to Sibelius" onto the Applications folder here, then open it.
   Look for the small note icon in the menu bar, top right.
2. Allow keyboard access when macOS asks, then turn on Launch at login.
3. Optional: put TapToSibeliusPanel.plg in
   ~/Library/Application Support/Avid/Sibelius/Plugins/Other/
   and restart Sibelius. It adds Plug-ins > Tap to Sibelius with the steps.
4. In Sibelius: File > Preferences > Input Devices.
   Tap to Sibelius: Use = Yes, Type = MIDI Keyboard.
5. Select a bar, start Flexi-time yourself, wait for the count-in, tap K.
TXT

echo "Making the disk image..."
rm -f "$DMG"
if ! hdiutil create -volname "$NAME" -srcfolder "$STAGE" \
    -ov -format UDZO "$DMG" >/dev/null; then
  echo "Could not create the disk image."
  exit 1
fi

if [ -n "${DEVELOPER_ID:-}" ]; then
  codesign --force --sign "$DEVELOPER_ID" "$DMG" || exit 1
  if [ -n "${NOTARY_PROFILE:-}" ]; then
    echo "Sending to Apple for notarization (this can take a few minutes)..."
    if xcrun notarytool submit "$DMG" --keychain-profile "$NOTARY_PROFILE" --wait; then
      xcrun stapler staple "$DMG"
    else
      echo "Notarization did not complete. The image still works, but other"
      echo "Macs will warn on first open."
    fi
  else
    echo "Signed, not notarized. Set NOTARY_PROFILE to finish that step."
  fi
fi

echo ""
echo "Done: $(cd "$(dirname "$DMG")" && pwd)/$(basename "$DMG")"
spctl -a -vv "$APP" 2>&1 | sed -n '1,3p'
open -R "$DMG" >/dev/null 2>&1
