#!/bin/bash
# Assembles a double-clickable .app around the SwiftPM binary — and, when
# asked, the disk image people install it from and the ZIP the updater
# fetches.
#
#   ./build.sh                 debug-free release build, ad-hoc signed: runs here
#   HALO_DEV=1 ./build.sh    the same as a dev build: yellow icon, named for
#                              the branch (Halo-split-view from
#                              claude/split-view-4a6927)
#   HALO_DEV=1 HALO_FEATURE=name ./build.sh
#                              the same, named Halo-name
#   ./build.sh release dmg     + build/Halo.dmg, build/Halo.zip and
#                                build/appcast.json, signed with Developer ID
#                                if there is one in the keychain
#   ./build.sh release ship    + both notarised, the DMG stapled
#
#   HALO_ARCH=x86_64 ./build.sh release ship
#                              the same for Intel Macs, into build/intel/
#
# Same shape as the one next door: SwiftPM builds the executable, and a macOS
# app bundle is just a folder with a plist and the binary in the right place.
#
# The three files keep the same names from release to release, so the site
# links to them once and the updater reads one address forever. ./publish.sh
# copies them into the site.
#
# Two builds from 1.0.5, each for one kind of Mac: Apple Silicon, in build/,
# and Intel, in build/intel/, served from the site's search/intel/ folder
# with an appcast of its own. A single universal app would have weighed
# twice as much for everyone (10.9 MB of binary instead of 5.4). The app's
# updater reads the feed for the chip it was built for (Updater.feed).
# HALO_ARCH picks the chip; without it, the Mac's own.
#
# "dmg" lays the disk image's window out with dmgbuild, installed into .build
# on first use (Python 3 and a network, once).
#
# What "ship" needs, once:
#   - a Developer ID Application certificate in the login keychain
#     (HALO_SIGN_IDENTITY names it; otherwise the first one found is used)
#   - a notarytool profile: xcrun notarytool store-credentials "search"
#     (HALO_NOTARY_PROFILE names it; default "search")
#   - HALO_DOWNLOAD_URL, the https folder the three files are served from,
#     for the appcast. Default https://example.invalid/search, which is
#     where Updater.feed in Updater.swift looks.
#
# NOTES.md, next to this script, is what's new: newest release first, one
# paragraph each. The first paragraph goes into the appcast, and from there
# under the version line in Settings.
set -euo pipefail

cd "$(dirname "$0")"
CONFIG="${1:-release}"
STEP="${2:-app}"
ARCH="${HALO_ARCH:-$(uname -m)}"
case "$ARCH" in
  arm64) OUT="build"; SUBFOLDER="" ;;
  x86_64) OUT="build/intel"; SUBFOLDER="/intel" ;;
  *) echo "HALO_ARCH is arm64 or x86_64, not “$ARCH”" >&2; exit 1 ;;
esac
APP="$OUT/Halo.app"
NAME="Halo"
# Asked for with HALO_DEV=1, a dev build, on a yellow plate and named for
# the feature it's built from: "Halo-split-view" in the menu bar, the Dock
# and ⌘Tab, so it's never taken for the installed app beside it, or for the
# dev build from another worktree. Only the name people see changes. The
# bundle id stays, so it shares the real app's settings, and so does the
# folder, build/Halo.app, which fresh.sh and the tests open. Not by
# default: a plain ./build.sh is also the Halo people here use every day
# and the release candidates. What goes in a DMG or a ZIP is never one.
case "${HALO_DEV:-}" in
  0|1) DEV="$HALO_DEV" ;;
  "") DEV=0 ;;
  *) echo "HALO_DEV is 0 or 1, not “$HALO_DEV”" >&2; exit 1 ;;
esac
[ "$STEP" != "app" ] && [ "$DEV" = 1 ] && { echo "a dev build doesn't go in a DMG" >&2; exit 1; }
SHOWN="$NAME"
if [ "$DEV" = 1 ]; then
  # The feature: HALO_FEATURE, or the branch's last part, without the
  # six-character hash a worktree's name ends in (claude/split-view-4a6927
  # is split-view). A hash has a digit in it; a word like "decade" doesn't.
  FEATURE="${HALO_FEATURE:-$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)}"
  FEATURE="${FEATURE##*/}"
  if [[ "$FEATURE" =~ ^(.+)-([0-9a-f]{6})$ ]]; then
    STEM="${BASH_REMATCH[1]}" HASH="${BASH_REMATCH[2]}"
    [[ "$HASH" =~ [0-9] ]] && FEATURE="$STEM"
  fi
  # Only what's safe in a name and in InfoPlist.strings.
  FEATURE="$(printf '%s' "$FEATURE" | tr -cd 'A-Za-z0-9._-')"
  case "$FEATURE" in
    ""|HEAD) SHOWN="$NAME Dev" ;;   # no branch to name it for
    *) SHOWN="$NAME-$FEATURE" ;;
  esac
fi
VERSION="$(tr -d '[:space:]' < VERSION)"
# A build number that only ever goes up, so the updater can tell newer from
# older without parsing version strings.
BUILD="$(date +%Y%m%d%H%M)"
# The oldest macOS this runs on — in the plist, and in the appcast so an
# older Mac is not handed a build it can't open.
MINIMUM="14.0"

# -Osize for a release: 14% less binary (5.39 → 4.65 MB) at the same speed —
# launch 337 against 338 ms, a scroll frame 0.26 against 0.25 ms, a key typed
# 0.41 ms either way, measured interleaved on 1.0.4 (27 Sep 2026).
SWIFTFLAGS=(-c "$CONFIG" --arch "$ARCH")
[ "$CONFIG" = "release" ] && SWIFTFLAGS+=(-Xswiftc -Osize)
swift build "${SWIFTFLAGS[@]}"
BINARY="$(swift build "${SWIFTFLAGS[@]}" --show-bin-path)/Halo"
[ "$(lipo -archs "$BINARY")" = "$ARCH" ] || { echo "$BINARY is not a $ARCH binary" >&2; exit 1; }

rm -rf "$APP"
mkdir -p "$APP/Contents/MacOS" "$APP/Contents/Resources"
cp "$BINARY" "$APP/Contents/MacOS/$NAME"
# The binary says which SDK it was built with, and AppKit draws the app the
# way that SDK's macOS drew it: built with the macOS 27 SDK, a Mac on 27 gets
# 27's window buttons and controls; said to be built with an older one, it
# gets that one's. Swift Build, SwiftPM's default since Xcode 27, writes the
# oldest macOS the app runs on in both places (minos 14.0, sdk 14.0 — the
# native build system wrote sdk 27.0 from the same sources), and the app came
# out in macOS 14's looks on every Mac. The SDK it really was built with is
# put back; the oldest macOS stays as it is.
SDK="$(xcrun --sdk macosx --show-sdk-version)"
vtool -set-build-version macos "$MINIMUM" "$SDK" -replace \
  -output "$APP/Contents/MacOS/$NAME.sdk" "$APP/Contents/MacOS/$NAME"
mv "$APP/Contents/MacOS/$NAME.sdk" "$APP/Contents/MacOS/$NAME"
# The AppleScript dictionary (Scripting.swift): read-only, tabs' addresses
# and titles. The plist below points to it.
cp Halo.sdef "$APP/Contents/Resources/"
# Face-gate models (personal use only: the antelopev2 weights are non-commercial).
cp -R Models/*.mlpackage "$APP/Contents/Resources/" 2>/dev/null || true

# Symbols stay out of the app. The linker leaves every function's name and a
# map back to the source in the binary — 15,000 entries, more than half of
# what the app weighed (6.5 MB of binary, 2.7 without them), and nothing the
# app reads while it runs. They are kept beside the build instead, as a dSYM
# that turns the addresses in a crash report back into names (Console, or
# atos -o build/Halo.app.dSYM/Contents/Resources/DWARF/Halo).
if [ "$CONFIG" = "release" ]; then
  rm -rf "$APP.dSYM"
  dsymutil "$BINARY" -o "$APP.dSYM" 2>/dev/null || echo "no dSYM this time" >&2
  strip -x "$APP/Contents/MacOS/$NAME"
fi

# The icon, drawn fresh each time — it is thirty lines of Swift, not an asset
# to keep in step with anything.
ICONSET="build/AppIcon.iconset"
ICONDOC="build/AppIcon.icon"
rm -rf "$ICONSET" "$ICONDOC"
HALO_DEV="$DEV" swift Icon/icon.swift "$ICONSET" "$ICONDOC" > /dev/null
iconutil -c icns "$ICONSET" -o "$APP/Contents/Resources/AppIcon.icns"
rm -rf "$ICONSET"
# macOS 26's Dark, Clear and Tinted Dock styles read the icon from an asset
# catalog compiled from the Icon Composer document; without one the Dock
# darkens the flat image and the mark goes black on black (#337). actool
# comes with Xcode 26 — with anything older, or only the command-line tools,
# the app keeps the .icns alone, as before. Only Assets.car is kept, not
# actool's own .icns: the one above goes on being the disk image's icon and
# the fallback. (macOS 14 and 15 show the flat pictures actool puts in
# Assets.car, drawn from the same document: the same mark, to within a
# pixel, on a plate with Apple's own corners.) --optimization space keeps
# those pictures zipped rather than lzfse'd: 666 KB of catalog instead of
# 800, every style still in it.
ICONNAME=""
ICONCAR="build/AppIcon.car"
rm -rf "$ICONCAR"
mkdir -p "$ICONCAR"
# Full paths: actool hands the document to a helper that runs elsewhere, and
# with "build/…" it finds nothing ("Icon export exited with status 255").
if xcrun actool "$PWD/$ICONDOC" --compile "$PWD/$ICONCAR" --platform macosx \
     --minimum-deployment-target "$MINIMUM" --app-icon AppIcon --optimization space \
     --output-partial-info-plist "$PWD/$ICONCAR/partial.plist" > /dev/null 2>&1 \
   && [ -f "$ICONCAR/Assets.car" ]; then
  cp "$ICONCAR/Assets.car" "$APP/Contents/Resources/Assets.car"
  ICONNAME="<key>CFBundleIconName</key><string>AppIcon</string>"
else
  echo "note: actool from Xcode 26 didn't compile the icon — no Dark or Tinted style this time" >&2
fi
rm -rf "$ICONCAR" "$ICONDOC"

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>$NAME</string>
  <key>CFBundleDisplayName</key><string>$NAME</string>
  <key>CFBundleExecutable</key><string>$NAME</string>
  <key>CFBundleIdentifier</key><string>com.example.halo</string>
  <key>CFBundlePackageType</key><string>APPL</string>
  <key>CFBundleShortVersionString</key><string>$VERSION</string>
  <key>CFBundleVersion</key><string>$BUILD</string>
  <key>CFBundleIconFile</key><string>AppIcon</string>
  $ICONNAME
  <key>LSMinimumSystemVersion</key><string>$MINIMUM</string>
  <key>LSApplicationCategoryType</key><string>public.app-category.productivity</string>
  <key>NSHumanReadableCopyright</key><string>© Halo</string>
  <key>NSHighResolutionCapable</key><true/>
  <key>NSAppleScriptEnabled</key><true/>
  <key>OSAScriptingDefinition</key><string>Halo.sdef</string>
  <!-- Handoff of web pages (Handoff.swift): a page picked up from another
       device opens here when Halo is the default browser. -->
  <key>NSUserActivityTypes</key>
  <array><string>NSUserActivityTypeBrowsingWeb</string></array>
  <!-- Owning http and https is what sends a link clicked in Mail here.
       Appearing in Desktop & Dock → Default web browser also needs the
       XHTML document type below. -->
  <key>CFBundleURLTypes</key>
  <array>
    <dict>
      <key>CFBundleURLName</key><string>Web address</string>
      <key>CFBundleURLSchemes</key>
      <array><string>http</string><string>https</string></array>
    </dict>
    <!-- mailto, so Halo can be the Mac's email app: an email link then
         opens the webmail chosen in Settings › General › Email links open
         in (Webmail.swift). Only ever made the default from that page's
         button; with no webmail chosen, a link that comes anyway goes on to
         the next mail app. -->
    <dict>
      <key>CFBundleURLName</key><string>Email address</string>
      <key>CFBundleURLSchemes</key>
      <array><string>mailto</string></array>
    </dict>
  </array>
  <key>CFBundleDocumentTypes</key>
  <array>
    <dict>
      <key>CFBundleTypeName</key><string>Web page</string>
      <key>CFBundleTypeRole</key><string>Viewer</string>
      <key>LSItemContentTypes</key>
      <array><string>public.html</string><string>com.apple.web-internet-location</string></array>
    </dict>
    <!-- macOS only lists an app under Desktop & Dock → Default web browser
         when it claims public.xhtml as well as public.html. http and https
         alone, which Halo already had, are not enough. -->
    <dict>
      <key>CFBundleTypeName</key><string>XHTML page</string>
      <key>CFBundleTypeRole</key><string>Viewer</string>
      <key>LSItemContentTypes</key>
      <array><string>public.xhtml</string></array>
    </dict>
    <!-- What a browser shows besides pages, listed under the Finder's Open
         With (#524): a PDF, a picture, plain text (code and logs conform to
         it), JSON. Alternate, so Halo is offered there and never becomes
         what opens them on a double-click. Pictures by name rather than
         public.image, which takes in SVG: an SVG can carry a script, and
         opened from the Finder it would run with its folder to read. -->
    <dict>
      <key>CFBundleTypeName</key><string>Document</string>
      <key>CFBundleTypeRole</key><string>Viewer</string>
      <key>LSHandlerRank</key><string>Alternate</string>
      <key>LSItemContentTypes</key>
      <array><string>com.adobe.pdf</string><string>public.png</string><string>public.jpeg</string><string>com.compuserve.gif</string><string>public.heic</string><string>org.webmproject.webp</string><string>public.tiff</string><string>public.plain-text</string><string>public.json</string></array>
    </dict>
  </array>
  <!-- A browser goes wherever it is pointed, including at http sites and at
       whatever is running on localhost. -->
  <key>NSAppTransportSecurity</key>
  <dict><key>NSAllowsArbitraryLoads</key><true/></dict>
  <!-- A browser is asked for these by the pages it shows, not by itself. macOS
       still wants a sentence to put in its own prompt, and touching the APIs
       without one is a crash rather than a refusal. -->
  <key>NSCameraUsageDescription</key>
  <string>Websites you visit can ask to use your camera. Halo asks you the first time each site does and keeps your answer; Settings › Privacy forgets them.</string>
  <key>NSMicrophoneUsageDescription</key>
  <string>Websites you visit can ask to use your microphone. Halo asks you the first time each site does and keeps your answer; Settings › Privacy forgets them.</string>
  <!-- Location: the first key is the one macOS has always read; the second
       is the one CoreLocation's requestWhenInUseAuthorization documents.
       Same sentence in both. -->
  <key>NSLocationUsageDescription</key>
  <string>Websites you visit can ask for your location. Halo asks you each time a site does, unless you choose Always allow for it; Settings › Privacy forgets those choices.</string>
  <key>NSLocationWhenInUseUsageDescription</key>
  <string>Websites you visit can ask for your location. Halo asks you each time a site does, unless you choose Always allow for it; Settings › Privacy forgets those choices.</string>
  <key>NSDownloadsFolderUsageDescription</key>
  <string>Files you download are saved to your Downloads folder.</string>
  <!-- Only once you turn on Settings › Passwords › Fill in your contact
       details: your own card, never anyone else's (MeCard.swift). -->
  <key>NSContactsUsageDescription</key>
  <string>Halo fills in forms with the details on your own card in Contacts, once you pick them under a box. It reads no one else's card and saves no copy of yours.</string>
</dict>
</plist>
PLIST
# A dev build's name, as a localised one: under the icon in the Dock, in
# ⌘Tab and in Finder, macOS shows the bundle's folder name otherwise. The
# plain names above stay "Halo", matching the folder, since a folder named
# otherwise reads to macOS as renamed by hand, and then the folder wins.
if [ "$DEV" = 1 ]; then
  mkdir -p "$APP/Contents/Resources/en.lproj"
  printf '"CFBundleDisplayName" = "%s";\n"CFBundleName" = "%s";\n' "$SHOWN" "$SHOWN" \
    > "$APP/Contents/Resources/en.lproj/InfoPlist.strings"
  /usr/libexec/PlistBuddy -c "Add :LSHasLocalizedDisplayName bool true" "$APP/Contents/Info.plist"
fi

# Signing. A Developer ID certificate, when there is one, with the hardened
# runtime Gatekeeper insists on for anything notarised; otherwise ad-hoc,
# which is enough for the app to run on the machine that built it — and
# which the updater refuses to swap anything in under.
IDENTITY="${HALO_SIGN_IDENTITY:-$(security find-identity -v -p codesigning 2>/dev/null \
  | grep -o '"Developer ID Application: [^"]*"' | head -1 | tr -d '"' || true)}"
# Passkeys need an entitlement Apple grants to browsers on request, and a
# Developer ID provisioning profile that carries it. With the profile next to
# this script, both go in; without it, the app is signed as before, because
# a restricted entitlement with no profile behind it is an app that won't open.
ENTITLEMENTS="Halo.entitlements"
# A release that ships without it loses passkeys and the keychain items the
# entitled builds saved (1.0.6, build 202610241534, went out that way from a
# worktree the git-ignored profile hadn't been copied into): ship refuses.
if [ "$STEP" = "ship" ] && [ ! -f "Halo.provisionprofile" ] && [ -z "${HALO_ALLOW_NO_PASSKEYS:-}" ]; then
  echo "ship: Halo.provisionprofile is missing here (it's git-ignored: copy it from the main checkout)" >&2
  exit 1
fi
if [ -f "Halo.provisionprofile" ]; then
  cp "Halo.provisionprofile" "$APP/Contents/embedded.provisionprofile"
  ENTITLEMENTS="Halo.passkeys.entitlements"
  echo "passkeys: profile embedded"
  # Sync with iCloud (ICloudSync.swift) needs the iCloud entitlements, and
  # those only open with a profile that carries the container: signed with
  # them and a profile from before, the app would not start. So they go in
  # only when the profile names iCloud.com.example.halo; otherwise
  # the build is as before and Settings says it needs a new one. Of the
  # two keys a profile may or may not carry (the container's environment,
  # the pushes), only what it does carry is kept.
  PROFILE_PLIST="$(mktemp -t search-profile)"
  security cms -D -i "Halo.provisionprofile" > "$PROFILE_PLIST" 2>/dev/null || true
  profile_has() { /usr/libexec/PlistBuddy -c "Print :Entitlements:$1" "$PROFILE_PLIST" >/dev/null 2>&1; }
  if /usr/libexec/PlistBuddy -c "Print :Entitlements:com.apple.developer.icloud-container-identifiers" "$PROFILE_PLIST" 2>/dev/null \
      | grep -q "iCloud.com.example.halo"; then
    ENTITLEMENTS="build/Halo.icloud.entitlements"
    mkdir -p build
    cp "Halo.icloud.entitlements" "$ENTITLEMENTS"
    for key in com.apple.developer.icloud-container-environment com.apple.developer.aps-environment; do
      profile_has "$key" || /usr/libexec/PlistBuddy -c "Delete :$key" "$ENTITLEMENTS" >/dev/null
    done
    echo "iCloud: the profile carries iCloud.com.example.halo, Sync with iCloud is in"
  else
    echo "iCloud: the profile has no iCloud container, built without Sync with iCloud (Settings says it needs a new build)"
  fi
  rm -f "$PROFILE_PLIST"
fi
if [ -n "$IDENTITY" ]; then
  codesign --force --deep --timestamp --options runtime \
    --entitlements "$ENTITLEMENTS" \
    --sign "$IDENTITY" "$APP"
  echo "signed as: $IDENTITY"
else
  # A build that cannot sign at all is not a build: `|| true` here let one
  # through as though it had finished, leaving a bundle that would not open.
  # set -e stops it now, with codesign's own words above.
  codesign --force --deep --sign - "$APP"
  [ "$STEP" != "app" ] && echo "no Developer ID certificate found — the DMG will only open on this Mac" >&2
fi

echo "built: $APP as $SHOWN ($VERSION, $ARCH, build $BUILD)"
[ "$STEP" = "app" ] && exit 0

# The disk image: the app beside a shortcut to Applications, on a white
# window with an arrow between them — drawn by Installer/background.swift and
# laid out by Installer/dmg.py through dmgbuild, which writes the Finder's
# layout file itself, so no Finder is scripted and no window opens mid-build.
# dmgbuild is installed into .build the first time, and needs Python 3 and a
# network then; without it the image is the plain one it always was.
DMG="$OUT/$NAME.dmg"
ART="build/installer"
rm -rf "$ART" "$DMG"
DMGBUILD=".build/dmgbuild/bin/dmgbuild"
if [ ! -x "$DMGBUILD" ]; then
  { python3 -m venv .build/dmgbuild && .build/dmgbuild/bin/pip install --quiet "dmgbuild==1.6.7"; } >/dev/null 2>&1 || true
fi
if [ -x "$DMGBUILD" ] \
  && swift Installer/background.swift "$ART" >/dev/null \
  && tiffutil -cathidpicheck "$ART/background.png" "$ART/background@2x.png" -out "$ART/background.tiff" >/dev/null 2>&1
then
  "$DMGBUILD" -s Installer/dmg.py \
    -D app="$APP" -D background="$ART/background.tiff" -D icon="$APP/Contents/Resources/AppIcon.icns" \
    "$NAME" "$DMG" >/dev/null
else
  echo "note: no dmgbuild — a plain disk image, without its window laid out" >&2
  STAGE="build/dmg"
  rm -rf "$STAGE"
  mkdir -p "$STAGE"
  cp -R "$APP" "$STAGE/"
  ln -s /Applications "$STAGE/Applications"
  hdiutil create -volname "$NAME" -srcfolder "$STAGE" -ov -format UDZO -quiet "$DMG"
  rm -rf "$STAGE"
fi
rm -rf "$ART"
[ -n "$IDENTITY" ] && codesign --force --timestamp --sign "$IDENTITY" "$DMG"
echo "packed: $DMG"

# The ZIP is what the updater fetches, and its hash is what the updater
# checks before opening it.
ZIP="$OUT/$NAME.zip"
rm -f "$ZIP"
ditto -c -k --keepParent "$APP" "$ZIP"
SHA="$(shasum -a 256 "$ZIP" | cut -d' ' -f1)"
echo "packed: $ZIP"

# What the updater reads. The first paragraph of NOTES.md, with the two
# characters JSON minds escaped, is the line under the version in Settings.
# Written last — after notarisation has stapled its ticket to the DMG, which
# changes it — so the DMG's hash is the one people download.
BASE="${HALO_DOWNLOAD_URL:-https://example.invalid/search}"
BASE="${BASE%/}$SUBFOLDER"
NOTES=""
if [ -f NOTES.md ]; then
  NOTES="$(awk 'NF { printf "%s%s", (n++ ? " " : ""), $0; next } n { exit }' NOTES.md \
    | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g')"
fi
# The AI add-on's engine (engine.sh), when this release offers one: Apple
# Silicon only for now. HALO_AI_ENGINE_VERSION names it — a number that
# only goes up, as Halo never takes a lower one — and the signed,
# notarised binary in build/engine/ goes beside the other files, under ai/,
# with its hash and size in the feed. The feed's own signature is what
# vouches for them; Halo then checks the engine's signature itself.
AIBLOCK=""
write_engine() {
  local ENGINE="build/engine/search-ai-engine-arm64"
  [ "$ARCH" = "arm64" ] && [ -n "${HALO_AI_ENGINE_VERSION:-}" ] || return 0
  [[ "$HALO_AI_ENGINE_VERSION" =~ ^[0-9]+$ ]] || { echo "HALO_AI_ENGINE_VERSION is a whole number" >&2; exit 1; }
  [ -f "$ENGINE" ] || { echo "$ENGINE is missing — ./engine.sh arm64 makes it" >&2; exit 1; }
  codesign --verify --strict -R='anchor apple generic and identifier "com.example.halo.ai-engine" and certificate leaf[subject.OU] = "TEAMID_PLACEHOLDER"' "$ENGINE" \
    || { echo "$ENGINE isn't signed with the Developer ID as com.example.halo.ai-engine" >&2; exit 1; }
  local FILE="search-ai-engine-arm64-$HALO_AI_ENGINE_VERSION"
  mkdir -p "$OUT/ai"
  cp "$ENGINE" "$OUT/ai/$FILE"
  AIBLOCK=",
  \"ai\": {\"engine\": {\"version\": $HALO_AI_ENGINE_VERSION, \"url\": \"$BASE/ai/$FILE\", \"sha256\": \"$(shasum -a 256 "$ENGINE" | cut -d' ' -f1)\", \"size\": $(stat -f%z "$ENGINE")}}"
  echo "engine: $OUT/ai/$FILE"
}
write_appcast() {
  local DMGSHA
  DMGSHA="$(shasum -a 256 "$DMG" | cut -d' ' -f1)"
  write_engine
  cat > "$OUT/appcast.json" <<JSON
{
  "version": "$VERSION",
  "build": $BUILD,
  "url": "$BASE/$NAME.zip",
  "dmg": "$BASE/$NAME.dmg",
  "sha256": "$SHA",
  "dmgSha256": "$DMGSHA",
  "notes": "$NOTES",
  "minimumSystemVersion": "$MINIMUM"$AIBLOCK
}
JSON
  echo "wrote: $OUT/appcast.json ($VERSION, $ARCH, build $BUILD)"
  # The same file, signed with the Developer ID that signs the app (codesign
  # keeps the signature in the file's extended attributes, ditto carries them
  # in the ZIP). Builds from 1.0.4 read only this one; older ones read the
  # plain file beside it. No key of its own to keep, or to lose.
  rm -f "$OUT/appcast.json.zip"
  if [ -n "$IDENTITY" ]; then
    local SIGNED
    SIGNED="$(mktemp -d)"
    cp "$OUT/appcast.json" "$SIGNED/appcast.json"
    codesign --force --timestamp --sign "$IDENTITY" --identifier com.example.halo.appcast "$SIGNED/appcast.json"
    ditto -c -k --sequesterRsrc "$SIGNED/appcast.json" "$OUT/appcast.json.zip"
    rm -rf "$SIGNED"
    echo "signed: $OUT/appcast.json.zip"
  fi
}
if [ "$STEP" = "dmg" ]; then write_appcast; exit 0; fi

# Notarisation: Apple looks both over. The ticket is stapled to the image,
# so it opens on a Mac that has never seen this app and is offline; the ZIP
# is fetched by an app that already trusts it, and is left as hashed.
[ -z "$IDENTITY" ] && { echo "can't ship without a Developer ID certificate" >&2; exit 1; }
for FILE in "$DMG" "$ZIP"; do
  xcrun notarytool submit "$FILE" --keychain-profile "${HALO_NOTARY_PROFILE:-search}" --wait
done
xcrun stapler staple "$DMG"
write_appcast
echo "shipped: $DMG, $ZIP, $OUT/appcast.json and its signed ZIP — ./publish.sh <folder> puts them on the site"
