#!/bin/bash
# Build and sign standalone Meshy bundle for macOS
#
# NOTE: Notarization is currently DISABLED for standalone bundles due to
# Python.framework signing complexity. Use the Homebrew bundle for
# notarized distribution.
#
# Prerequisites:
# 1. Developer ID Application certificate installed in Keychain
#
# Usage: Run from project root directory:
#   ./build-aux/macos/build-and-notarize.sh

set -e

# Change to project root directory
cd "$(dirname "$0")/../.."

# Configuration - UPDATE THESE VALUES
SIGNING_IDENTITY="Developer ID Application: Jaroslav Reznik (LG87HQ83LT)"
KEYCHAIN_PROFILE="meshy-notary"  # Name you used in 'notarytool store-credentials'

# Get version from build
VERSION=$(date +%y.%m.%d)

echo "===> Building and signing Meshy v${VERSION}..."
echo ""

# Step 1: Build signed standalone bundle with DMG
echo "===> Step 1/4: Building signed app bundle and DMG..."
./build-aux/macos/create-standalone-bundle.sh \
  --sign "${SIGNING_IDENTITY}" \
  --dmg

DMG_FILE="dist/Meshy-${VERSION}.dmg"

if [ ! -f "${DMG_FILE}" ]; then
    echo "Error: DMG not found at ${DMG_FILE}"
    exit 1
fi

echo "✓ Built and signed: ${DMG_FILE}"
echo ""

# Step 2: Verify code signature
echo "===> Step 2/2: Verifying code signature..."
codesign --verify --verbose dist/Meshy.app
echo "✓ Code signature verified"
echo ""

echo "===> ⚠️  NOTARIZATION SKIPPED"
echo ""
echo "Standalone bundle notarization is currently disabled due to"
echo "Python.framework signing complexity issues."
echo ""
echo "For notarized distribution, use the Homebrew bundle:"
echo "  ./build-aux/macos/build-homebrew-bundle.sh --sign \"${SIGNING_IDENTITY}\" --dmg"
echo ""
echo "Signed (but not notarized) DMG created:"
echo "  ${DMG_FILE}"
echo ""
echo "Note: This DMG will show Gatekeeper warnings when downloaded."
echo "Use for testing only, not for public distribution."
