#!/usr/bin/env bash
set -euo pipefail

if [[ "$(uname -s)" != "Darwin" ]]; then
  echo "[doc-reader] Native macOS app build is only available on macOS." >&2
  exit 1
fi

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
APP_ROOT="${1:-$HOME/.doc-reader-managed}"
APP_BUNDLE="$APP_ROOT/Doc Reader.app"
CONTENTS="$APP_BUNDLE/Contents"
MACOS="$CONTENTS/MacOS"
RESOURCES="$CONTENTS/Resources"
SOURCE="$SCRIPT_DIR/macos/DocReaderApp/Sources/DocReaderApp/main.swift"
ICON_SOURCE="$SCRIPT_DIR/macos/DocReaderApp/Tools/generate-app-icon.swift"
EXECUTABLE="$MACOS/DocReader"
INFO_PLIST="$CONTENTS/Info.plist"
ICON_ICNS="$RESOURCES/DocReaderIcon.icns"

if ! command -v swiftc >/dev/null 2>&1; then
  echo "[doc-reader] Apple's Command Line Tools are required to build the native macOS wrapper." >&2
  echo "[doc-reader] Install them with:" >&2
  echo "[doc-reader]   xcode-select --install" >&2
  echo "[doc-reader] Then rerun:" >&2
  echo "[doc-reader]   read-docs install" >&2
  exit 1
fi

if [[ ! -f "$SOURCE" ]]; then
  echo "[doc-reader] Missing native app source: $SOURCE" >&2
  exit 1
fi

mkdir -p "$MACOS" "$RESOURCES"

swiftc \
  -swift-version 5 \
  -O \
  -framework AppKit \
  -framework ApplicationServices \
  -framework AVFoundation \
  -framework Security \
  "$SOURCE" \
  -o "$EXECUTABLE"

cat > "$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>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleExecutable</key>
    <string>DocReader</string>
    <key>CFBundleIdentifier</key>
    <string>com.sproutseeds.read-docs</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleIconFile</key>
    <string>DocReaderIcon</string>
    <key>CFBundleName</key>
    <string>Doc Reader</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>0.3.0</string>
    <key>CFBundleVersion</key>
    <string>0.3.0</string>
    <key>LSMinimumSystemVersion</key>
    <string>13.0</string>
    <key>LSUIElement</key>
    <true/>
    <key>NSHighResolutionCapable</key>
    <true/>
    <key>NSMicrophoneUsageDescription</key>
    <string>Doc Reader records microphone audio for opt-in local 4090 dictation.</string>
    <key>NSInputMonitoringUsageDescription</key>
    <string>Doc Reader watches the Option key for opt-in hold-to-record dictation.</string>
    <key>NSAccessibilityUsageDescription</key>
    <string>Doc Reader inserts completed dictation into the active text field when you enable automatic insertion.</string>
  </dict>
</plist>
PLIST

plutil -lint "$INFO_PLIST" >/dev/null

if [[ -f "$ICON_SOURCE" ]]; then
  if ! command -v iconutil >/dev/null 2>&1; then
    echo "[doc-reader] iconutil is required to build the native app icon." >&2
    exit 1
  fi
  ICON_TMP="$(mktemp -d "${TMPDIR:-/tmp}/doc-reader-icon.XXXXXX")"
  trap 'rm -rf "${ICON_TMP:-}"' EXIT
  swift "$ICON_SOURCE" "$ICON_TMP/DocReaderIcon.iconset"
  iconutil -c icns "$ICON_TMP/DocReaderIcon.iconset" -o "$ICON_ICNS"
  echo "[doc-reader] Built native app icon: $ICON_ICNS"
fi

SIGN_IDENTITY="${DOC_READER_CODESIGN_IDENTITY:-}"
if [[ -z "$SIGN_IDENTITY" ]] && command -v security >/dev/null 2>&1; then
  SIGN_IDENTITY="$(
    security find-identity -v -p codesigning 2>/dev/null \
      | sed -n 's/.*"\(Developer ID Application: [^"]*\)".*/\1/p' \
      | head -n 1
  )"
fi
if [[ -z "$SIGN_IDENTITY" ]] && command -v security >/dev/null 2>&1; then
  SIGN_IDENTITY="$(
    security find-identity -v -p codesigning 2>/dev/null \
      | sed -n 's/.*"\(Apple Development: [^"]*\)".*/\1/p' \
      | head -n 1
  )"
fi

if [[ -n "$SIGN_IDENTITY" ]]; then
  codesign --force --deep --sign "$SIGN_IDENTITY" "$APP_BUNDLE"
  echo "[doc-reader] Signed native app with: $SIGN_IDENTITY"
elif command -v codesign >/dev/null 2>&1; then
  codesign --force --deep --sign - "$APP_BUNDLE"
  echo "[doc-reader] Signed native app ad-hoc."
fi

echo "[doc-reader] Built native app: $APP_BUNDLE"
