#!/usr/bin/env bash
# Publish the single "Experimental" BlueGit release for experimental/tspl2.
#
# - Only on branch experimental/tspl2; fixed tag experimental-tspl2 (never X.Y.Z).
# - There is always at most one such release: an existing one is deleted
#   (with its tag and assets) before the new one is created.
# - Uses CI_JOB_TOKEN to create. BlueGit only lets user tokens delete releases,
#   so replacing needs EXPERIMENTAL_RELEASE_TOKEN; without it the job fails
#   when the release already exists (it never creates a second one).
# - No AUR, no other secrets.
set -euo pipefail
: "${CI_API_URL:?}" "${CI_JOB_TOKEN:?}" "${CI_COMMIT_SHA:?}" "${CI_PROJECT_PATH:?}"
cd "$(dirname "$0")/../.."
TAG="experimental-tspl2"
BRANCH="experimental/tspl2"
[[ "${CI_COMMIT_BRANCH:-}" == "$BRANCH" ]] || { echo "Refusing: experimental release only from $BRANCH (got '${CI_COMMIT_BRANCH:-}')" >&2; exit 1; }
[[ ! "$TAG" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+$ ]] || { echo "Refusing: version-like tag" >&2; exit 1; }
v="$(cat artifacts/version.txt)"
[[ "$v" == *-tspl2.* ]] || { echo "Refusing: not an experimental version: $v" >&2; exit 1; }
for f in dist/posprint-experimental-linux-x86_64.tar.gz dist/posprint-experimental-windows-x86_64.exe; do
  test -s "$f" || { echo "Missing artifact $f" >&2; exit 1; }
done

remote="${CI_PROJECT_URL:-${CI_API_URL%/api/v1}/$CI_PROJECT_PATH}.git"
exists=0
if [[ -n "$(git ls-remote --tags "$remote" "refs/tags/$TAG" 2>/dev/null || true)" ]]; then exists=1; fi
if [[ -n "$(git ls-remote --tags origin "refs/tags/$TAG" 2>/dev/null || true)" ]]; then exists=1; fi

if [[ $exists == 1 ]]; then
  if [[ -z "${EXPERIMENTAL_RELEASE_TOKEN:-}" ]]; then
    echo "Release $TAG already exists and EXPERIMENTAL_RELEASE_TOKEN is not set; not creating a second release." >&2
    exit 1
  fi
  echo "Replacing existing release $TAG"
  code="$(curl -sS -o /dev/null -w '%{http_code}' -X DELETE \
    "$CI_API_URL/projects/$CI_PROJECT_PATH/releases/$TAG?delete_tag=true" \
    -H "Authorization: Bearer $EXPERIMENTAL_RELEASE_TOKEN")" || code=000
  [[ "$code" == 2* || "$code" == 404 ]] || { echo "Delete returned HTTP $code" >&2; exit 1; }
fi

asset() { printf '{"job":"%s","path":"%s","name":"%s"}' "$1" "$2" "$3"; }
assets="$(asset exp:build:linux dist/posprint-experimental-linux-x86_64.tar.gz posprint-experimental-linux-x86_64.tar.gz),$(asset exp:build:windows dist/posprint-experimental-windows-x86_64.exe posprint-experimental-windows-x86_64.exe)"
short="${CI_COMMIT_SHA:0:8}"
desc="EXPERIMENTAL / PRERELEASE - niet stabiel, niet in AUR.\n\nVersie ${v} van branch ${BRANCH} (commit ${short}).\nTSPL/TSPL2-labelprinters (getest op opmaak, nog niet fysiek): protocol wordt automatisch herkend; labelformaat per printermodel.\n\n* Linux x86_64: posprint-experimental-linux-x86_64.tar.gz\n* Windows x86_64: posprint-experimental-windows-x86_64.exe\n\nDeze release wordt bij elke pipeline op ${BRANCH} vervangen."
body="$(printf '{"tag":"%s","commit":"%s","name":"%s","body":"%s","assets":[%s]}' "$TAG" "$CI_COMMIT_SHA" "Experimental: TSPL2 labelprinters (${v})" "$desc" "$assets")"
echo "Experimental release $TAG ($v) on $CI_COMMIT_SHA"
resp="$(mktemp)"
code="$(curl -sS -o "$resp" -w '%{http_code}' -X POST "$CI_API_URL/repos/$CI_PROJECT_PATH/releases" \
  -H "Authorization: Bearer $CI_JOB_TOKEN" -H "Content-Type: application/json" -d "$body")" || code=000
cat "$resp"; echo
[[ "$code" == 2* ]] || { echo "Release API returned HTTP $code" >&2; exit 1; }
