Repository Public
1#!/usr/bin/env bash2# Author: Simon-Pierre Boucher — contact@spboucher.ai3# File: Scripts/notarize.sh4# Description: Notarizes dist/SP Vault.app with the EXISTING notarytool keychain profile, staples it, then builds,5# signs, notarizes and staples the DMG.6#7# Usage: Scripts/notarize.sh8# NOTARY_PROFILE keychain profile created earlier with `xcrun notarytool store-credentials` (default: MacLustr-Notarize).9# The script never creates, prints or modifies credentials. "notarized" is printed only when Apple answered Accepted.10set -euo pipefail11cd "$(dirname "$0")/.."12PROFILE="${NOTARY_PROFILE:-MacLustr-Notarize}"; APPNAME="SP Vault"; APP="dist/$APPNAME.app"13[[ -d "$APP" ]] || { echo "ERROR: $APP not found. Run Scripts/release-mac.sh first." >&2; exit 1; }14VERSION="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleShortVersionString' "$APP/Contents/Info.plist")"15DMG="dist/SPVault-$VERSION.dmg"; ZIP="build/SPVault-notarize.zip"; mkdir -p build1617submit() { # submit <file> <json-output>; returns 0 only when status is Accepted18 local file="$1" out="$2"19 if ! xcrun notarytool submit "$file" --keychain-profile "$PROFILE" --wait --output-format json >"$out" 2>"$out.err"; then20 echo "Notary service refused the submission of $file:" >&2; sed -e 's/^/ /' "$out.err" | head -8 >&2; [[ -s "$out" ]] && head -c 600 "$out" >&2 && echo >&2; return 121 fi22 local status id23 status="$(/usr/bin/python3 -c 'import json,sys; print(json.load(open(sys.argv[1])).get("status",""))' "$out")"24 id="$(/usr/bin/python3 -c 'import json,sys; print(json.load(open(sys.argv[1])).get("id",""))' "$out")"25 echo "Submission $id: $status"26 if [[ "$status" != "Accepted" ]]; then27 xcrun notarytool log "$id" --keychain-profile "$PROFILE" "build/notary-log-$id.json" >/dev/null 2>&1 || true28 echo "See build/notary-log-$id.json for the reasons." >&2; return 129 fi30}3132echo "== 1/4 Submitting the application =="33rm -f "$ZIP"; ditto -c -k --keepParent "$APP" "$ZIP"34submit "$ZIP" build/notary-app.json || { echo "RESULT: NOT notarized. $APP is signed but carries no notarization ticket." >&2; exit 2; }35echo "== 2/4 Stapling the application =="36xcrun stapler staple "$APP"; xcrun stapler validate "$APP"3738echo "== 3/4 Building the disk image =="39IDENTITY="$(codesign -dvv "$APP" 2>&1 | sed -n 's/^Authority=\(Developer ID Application: .*\)$/\1/p' | head -1)"40rm -f "$DMG"; STAGE="$(mktemp -d)"41ditto "$APP" "$STAGE/$APPNAME.app"; ln -s /Applications "$STAGE/Applications"42hdiutil create -volname "$APPNAME" -srcfolder "$STAGE" -ov -format UDZO "$DMG" >/dev/null; rm -rf "$STAGE"43codesign --force --timestamp --sign "$IDENTITY" "$DMG"4445echo "== 4/4 Submitting and stapling the disk image =="46submit "$DMG" build/notary-dmg.json || { echo "RESULT: the application is notarized and stapled, but the disk image is NOT." >&2; exit 3; }47xcrun stapler staple "$DMG"; xcrun stapler validate "$DMG"48(cd dist && shasum -a 256 "$(basename "$DMG")") | tee "$DMG.sha256"49echo "RESULT: notarized and stapled — $APP and $DMG"50