#!/bin/sh
# release-dist.sh - thin wrapper around the shared multi-arch release tool.
#
# The real cross-target build + dist publish lives in scorpiox-cli-botplugin
# (it owns the musl / aarch64 / MinGW / macOS toolchains, the dist upload and
# the plugingit mirror). This script exists so the repo releases the same way
# every other oss plugin does, and so a stale release fails loudly instead of
# shipping a 5-target manifest that still says "release complete".
#
#   ./release-dist.sh                 # build + publish the version in plugin.json
#   VERSION=1.0.1 ./release-dist.sh   # explicit version
#
# Run it on the build host that has musl-gcc, aarch64-linux-gnu-gcc,
# x86_64-w64-mingw32-gcc and ssh access to the macOS builder. Nothing here
# contains a host address or a credential.

set -eu

SLUG=github
VERSION="${VERSION:-1.0.0}"
PLUGINGIT_TOKEN_FILE="${PLUGINGIT_TOKEN_FILE:-/etc/scorpiox-plugingit.token}"

ROOT=$(cd "$(dirname "$0")" && pwd)

if ! command -v scorpiox-cli-botplugin >/dev/null 2>&1; then
    echo "scorpiox-cli-botplugin not found on PATH" >&2
    exit 1
fi

echo "==> audit $SLUG"
scorpiox-cli-botplugin audit "$ROOT" --json || { echo "audit blocked the release" >&2; exit 4; }

echo "==> publish $SLUG $VERSION"
scorpiox-cli-botplugin publish "$ROOT" \
    --slug "$SLUG" \
    --version "$VERSION" \
    --json

echo "==> reindex"
scorpiox-cli-botplugin reindex --json >/dev/null 2>&1 || true
echo "check dist/bot-plugins/$SLUG/$VERSION/manifest.json has 6 targets before calling it done"
