#!/usr/bin/env bash
# Writes the tree that release.yml publishes to toarchkumar/mnemo-swift
# (MOBILE-1e): a release Package.swift whose binary target is the
# XCFramework zip on this repo's GitHub Release, the generated Swift
# bindings, the Swift layer's sources, a README and the licence.
#
#   scripts/make-release-package.sh <version> <zip url> <checksum> <out dir>
#
# Run scripts/build-xcframework.sh first: the generated bindings in
# Sources/Mnemo/Generated/ must match the zip. The checksum is the zip's
# `swift package compute-checksum` (its SHA-256).
set -euo pipefail

if [ $# -ne 4 ]; then
  echo "usage: $0 <version> <zip url> <checksum> <out dir>" >&2
  exit 2
fi
VERSION="$1"
URL="$2"
CHECKSUM="$3"
OUT="$4"

HERE="$(cd "$(dirname "$0")/.." && pwd)"   # mnemo/bindings/swift
ROOT="$(cd "$HERE/../../.." && pwd)"        # the repository

if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  echo "version must be MAJOR.MINOR.PATCH, got \"$VERSION\"" >&2
  exit 2
fi
if ! [[ "$CHECKSUM" =~ ^[0-9a-f]{64}$ ]]; then
  echo "checksum must be 64 hex digits, got \"$CHECKSUM\"" >&2
  exit 2
fi
if [ ! -s "$HERE/Sources/Mnemo/Generated/mnemo_uniffi.swift" ]; then
  echo "no generated bindings: run scripts/build-xcframework.sh first" >&2
  exit 1
fi

rm -rf "$OUT"
mkdir -p "$OUT/Sources"
cp -R "$HERE/Sources/Mnemo" "$OUT/Sources/Mnemo"
cp "$ROOT/LICENSE" "$OUT/LICENSE"

cat > "$OUT/Package.swift" <<SWIFT
// swift-tools-version:6.0
//
// MNemo $VERSION for Swift. Generated by the release workflow of
// https://github.com/toarchkumar/mnemo from mnemo/bindings/swift/; don't
// edit this repository by hand.
import PackageDescription

let package = Package(
    name: "Mnemo",
    platforms: [.iOS(.v15), .macOS(.v12)],
    products: [
        .library(name: "Mnemo", targets: ["Mnemo"]),
    ],
    targets: [
        .binaryTarget(
            name: "MnemoFFI",
            url: "$URL",
            checksum: "$CHECKSUM"
        ),
        .target(
            name: "Mnemo",
            dependencies: ["MnemoFFI"],
            path: "Sources/Mnemo",
            linkerSettings: [.linkedFramework("Security")]
        ),
    ],
    // The UniFFI-generated Swift predates Swift 6's strict concurrency checks.
    swiftLanguageModes: [.v5]
)
SWIFT

cat > "$OUT/README.md" <<MD
# MNemo for Swift

An encrypted, single-file agent memory for iOS and macOS apps: vectors,
text, metadata and conversation threads in one portable \`.mnemo\` file.

This repository is the Swift package's release mirror. The release
workflow of [toarchkumar/mnemo](https://github.com/toarchkumar/mnemo)
writes it on every tag; the source, issues and documentation live there.

## Install

In Xcode, **File > Add Package Dependencies...**, then enter
\`https://github.com/toarchkumar/mnemo-swift\`. Or, in \`Package.swift\`:

\`\`\`swift
.package(url: "https://github.com/toarchkumar/mnemo-swift", from: "$VERSION"),
\`\`\`

and add the \`Mnemo\` product to your target. It needs iOS 15 or macOS 12.

## Documentation

See the package guide, in
[mnemo/bindings/swift/README.md](https://github.com/toarchkumar/mnemo/blob/v$VERSION/mnemo/bindings/swift/README.md):
it covers \`MnemoStore\`, keys and backups, and the background rules.
The changelog is
[CHANGELOG.md](https://github.com/toarchkumar/mnemo/blob/v$VERSION/CHANGELOG.md).

Apache-2.0.
MD

echo "==> release package $VERSION in $OUT"
find "$OUT" -type f | sed "s|^$OUT/||" | sort
