#!/usr/bin/env sh

# Builds a release of amp: a binary for each target, smoke-tests the native one, and writes the
# release's files into OUT_DIR: each binary gzipped, as amp-<os>-<arch>.gz, and install.sh. It also
# writes LICENSE, NOTICE and THIRD_PARTY_LICENSES, which Amp's license and its dependencies' require
# to go with the binaries, and manifest.json, which lists the files with their sizes, SHA-256
# digests and content types, in the form of a manifest of Gauss's artifacts: publishing uploads each
# listed file as a file of the release's artifact, and Gauss serves the manifest. install.sh and
# `amp update` read the release's signed manifest instead, release.json, which releaselog writes
# and signs (bin/publish), and which a release's artifact carries.
#
#   scripts/package VERSION OUT_DIR
#
# VERSION is what `amp version` reports, such as 2026_10_0007. It runs from any directory, on any
# platform: each binary is cross-compiled without cgo.

set -e -u

VERSION="${1:?usage: scripts/package VERSION OUT_DIR}"
OUT="${2:?usage: scripts/package VERSION OUT_DIR}"
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
TARGETS="darwin-arm64 darwin-amd64 linux-amd64 linux-arm64 windows-amd64 windows-arm64"

fail() {
    echo "scripts/package: $1" >&2
    exit 1
}

command -v go >/dev/null 2>&1 || fail "install Go from https://go.dev/dl"
rm -rf "$OUT"
mkdir -p "$OUT"
OUT="$(cd "$OUT" && pwd)"

# A release that pins no release-signing key could never verify an update.
if [ "$VERSION" != dev ]; then
    sh "$ROOT/scripts/check_release_keys" || exit 1
fi

echo "==> Testing"
(cd "$ROOT" && go test ./...)

echo "==> Building amp $VERSION"
for target in $TARGETS; do
    os="${target%-*}"
    arch="${target#*-}"
    (cd "$ROOT" && CGO_ENABLED=0 GOOS="$os" GOARCH="$arch" go build -trimpath \
        -ldflags "-s -w -X gauss.motor.dev/motor/amp/cli.Version=$VERSION" \
        -o "$OUT/amp-$target" ./cmd/amp)
    echo "    built amp-$target"
done

native="$(go env GOOS)-$(go env GOARCH)"
"$OUT/amp-$native" version | grep -q "$VERSION" || fail "amp-$native doesn't report version $VERSION"
SMOKE="$(mktemp -d)"
trap 'rm -rf "$SMOKE"' EXIT
(
    cd "$SMOKE"
    "$OUT/amp-$native" init >/dev/null
    echo hello > hello.txt
    "$OUT/amp-$native" record -m smoke --agent package --model smoke-test >/dev/null
    "$OUT/amp-$native" log --hashes | grep -q .
) || fail "the smoke test failed"
echo "    smoke-tested amp-$native"

FILES=""
add() { # name content-type
    if command -v sha256sum >/dev/null 2>&1; then
        sha="$(sha256sum "$OUT/$1" | cut -d' ' -f1)"
    else
        sha="$(shasum -a 256 "$OUT/$1" | cut -d' ' -f1)"
    fi
    size="$(wc -c < "$OUT/$1" | tr -d ' ')"
    FILES="$FILES${FILES:+,}
    { \"name\": \"$1\", \"size\": $size, \"sha256\": \"$sha\", \"content_type\": \"$2\" }"
}
for target in $TARGETS; do
    gzip -9 -n -c "$OUT/amp-$target" > "$OUT/amp-$target.gz"
    rm "$OUT/amp-$target"
    add "amp-$target.gz" application/gzip
done
cp "$ROOT/scripts/install.sh" "$OUT/install.sh"
add install.sh text/x-shellscript
cp "$ROOT/scripts/install.ps1" "$OUT/install.ps1"
add install.ps1 text/plain
for f in LICENSE NOTICE THIRD_PARTY_LICENSES; do
    cp "$ROOT/$f" "$OUT/$f"
    add "$f" text/plain
done

cat > "$OUT/manifest.json" <<JSON
{
  "version": "$VERSION",
  "files": [$FILES
  ]
}
JSON
echo "Built amp $VERSION in $OUT"
