#!/usr/bin/env bash
set -euo pipefail

if [[ $# -ne 1 ]]; then
  echo "Usage: s/release <version>" >&2
  exit 1
fi

version="$1"
if [[ ! "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+([-.][0-9A-Za-z.-]+)?$ ]]; then
  echo "Invalid version: $version" >&2
  exit 1
fi

if ! git diff --quiet || ! git diff --cached --quiet; then
  echo "Working tree is not clean. Commit or stash changes before releasing." >&2
  exit 1
fi

perl -0pi -e "s/^version = \"[^\"]+\"/version = \"$version\"/m" Cargo.toml

git add Cargo.toml
git commit -m "Release v$version"
git tag "v$version"

git push origin HEAD
git push origin "v$version"

echo "Release v$version pushed. CI will publish artifacts." 
