#!/bin/bash set -euo pipefail ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)" BUILD_DIR="$ROOT_DIR/.build" ARCHIVE_PATH="$BUILD_DIR/TonuinoManager.xcarchive" APP_VERSION="${APP_VERSION:-1.0.0}" APP_BUILD="${APP_BUILD:-1}" : "${DEVELOPMENT_TEAM:?Set DEVELOPMENT_TEAM to the Apple Developer Team ID}" : "${DEVELOPER_ID_APPLICATION:?Set DEVELOPER_ID_APPLICATION to the Developer ID Application signing identity}" : "${NOTARY_PROFILE:?Set NOTARY_PROFILE to a stored notarytool keychain profile}" : "${FFMPEG_BINARY:?Set FFMPEG_BINARY to a self-contained universal FFmpeg executable with libmp3lame enabled}" : "${YTDLP_BINARY:?Set YTDLP_BINARY to a standalone universal yt-dlp executable}" : "${DENO_BINARY:?Set DENO_BINARY to a universal Deno executable for YouTube JavaScript challenges}" : "${FPCALC_BINARY:?Set FPCALC_BINARY to the universal Chromaprint fpcalc executable}" [[ -x "$FFMPEG_BINARY" ]] || { echo "FFMPEG_BINARY is not executable" >&2; exit 1; } [[ -x "$YTDLP_BINARY" ]] || { echo "YTDLP_BINARY is not executable" >&2; exit 1; } [[ -x "$DENO_BINARY" ]] || { echo "DENO_BINARY is not executable" >&2; exit 1; } [[ -x "$FPCALC_BINARY" ]] || { echo "FPCALC_BINARY is not executable" >&2; exit 1; } for TOOL_BINARY in "$FFMPEG_BINARY" "$YTDLP_BINARY" "$DENO_BINARY" "$FPCALC_BINARY"; do TOOL_ARCHS="$(lipo -archs "$TOOL_BINARY")" [[ "$TOOL_ARCHS" == *arm64* && "$TOOL_ARCHS" == *x86_64* ]] || { echo "$TOOL_BINARY must contain arm64 and x86_64 slices" >&2; exit 1; } done cd "$ROOT_DIR" xcodegen generate xcodebuild -project TonuinoManager.xcodeproj -scheme TonuinoManager -configuration Release \ -archivePath "$ARCHIVE_PATH" -derivedDataPath "$BUILD_DIR/DerivedData" \ -clonedSourcePackagesDirPath "$BUILD_DIR/DerivedData/SourcePackages" \ -destination 'generic/platform=macOS' \ ARCHS='arm64 x86_64' ONLY_ACTIVE_ARCH=NO DEVELOPMENT_TEAM="$DEVELOPMENT_TEAM" \ CODE_SIGN_STYLE=Manual CODE_SIGN_IDENTITY="$DEVELOPER_ID_APPLICATION" MARKETING_VERSION="$APP_VERSION" \ CURRENT_PROJECT_VERSION="$APP_BUILD" archive APP_PATH="$(find "$ARCHIVE_PATH/Products/Applications" -maxdepth 1 -type d -name '*.app' -print -quit)" [[ -n "$APP_PATH" ]] || { echo "App bundle is missing from the archive" >&2; exit 1; } mkdir -p "$APP_PATH/Contents/Resources" install -m 755 "$FFMPEG_BINARY" "$APP_PATH/Contents/Resources/ffmpeg" install -m 755 "$YTDLP_BINARY" "$APP_PATH/Contents/Resources/yt-dlp" install -m 755 "$DENO_BINARY" "$APP_PATH/Contents/Resources/deno" install -m 755 "$FPCALC_BINARY" "$APP_PATH/Contents/Resources/fpcalc" codesign --force --options runtime --timestamp --sign "$DEVELOPER_ID_APPLICATION" "$APP_PATH/Contents/Resources/ffmpeg" codesign --force --options runtime --timestamp --sign "$DEVELOPER_ID_APPLICATION" "$APP_PATH/Contents/Resources/yt-dlp" codesign --force --options runtime --timestamp --sign "$DEVELOPER_ID_APPLICATION" "$APP_PATH/Contents/Resources/deno" codesign --force --options runtime --timestamp --sign "$DEVELOPER_ID_APPLICATION" "$APP_PATH/Contents/Resources/fpcalc" codesign --force --deep --options runtime --timestamp --entitlements Sources/TonuinoManager/TonuinoManager.entitlements \ --sign "$DEVELOPER_ID_APPLICATION" "$APP_PATH" mkdir -p "$BUILD_DIR/release" NOTARY_ZIP="$BUILD_DIR/Tonuino-Manager-notary.zip" ditto -c -k --sequesterRsrc --keepParent "$APP_PATH" "$NOTARY_ZIP" xcrun notarytool submit "$NOTARY_ZIP" --keychain-profile "$NOTARY_PROFILE" --wait xcrun stapler staple "$APP_PATH" rm -f "$NOTARY_ZIP" ditto -c -k --sequesterRsrc --keepParent "$APP_PATH" "$BUILD_DIR/release/Tonuino-Manager-$APP_VERSION.zip" hdiutil create -volname 'Tonuino Manager' -srcfolder "$APP_PATH" -ov -format UDZO \ "$BUILD_DIR/release/Tonuino-Manager-$APP_VERSION-universal.dmg" xcrun notarytool submit "$BUILD_DIR/release/Tonuino-Manager-$APP_VERSION-universal.dmg" --keychain-profile "$NOTARY_PROFILE" --wait xcrun stapler staple "$BUILD_DIR/release/Tonuino-Manager-$APP_VERSION-universal.dmg" echo "Created $BUILD_DIR/release/Tonuino-Manager-$APP_VERSION-universal.dmg and Sparkle ZIP"