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

usage() {
  cat <<'EOF'
Usage:
  bash scripts/release-boujot-cli.sh <version> [--skip-build]

What it does, on master only:
  - Requires a clean working tree on the master branch
  - Checks the rebrand: `yarn rebrand` must change nothing, since master is already @boujot/happy-coder
  - Bumps package.json to <version>
  - Optionally runs: npm run build (the tests run once, in Drone's build of the tag)
  - Commits "release: @boujot/happy-coder <version>" on master
  - Tags it boujot-cli-v<version> (annotated)

Then publishing runs in Drone, never from this machine:
  - git push sn1994 master boujot-cli-v<version>
  - Drone builds and tests the tag once, then publishes the version under the dist-tag latest (no beta step: the
    owner, 28 October 2026).
  - To move latest back to a version already on npm: promote that release commit's master build to npm-latest
    (moves the tag; no new build): drone build promote meow/happy-cli <build> npm-latest
    An agent with Software Delivery tools does the same with drone_build_promote.
  - The npm token is on Software Delivery's Credentials page (credential boujot), not in a local file.

Notes:
  - Releases live on master as a version commit plus a tag; there are no release branches.
  - It will NOT push to any remote.
EOF
}

if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
  usage
  exit 0
fi

if [[ $# -lt 1 ]]; then
  usage
  exit 2
fi

VERSION="$1"
shift

SKIP_BUILD="0"

while [[ $# -gt 0 ]]; do
  case "$1" in
    --skip-test)
      echo "The helper runs no tests: Drone's build of the tag runs them once, before it publishes." >&2
      exit 2
      ;;
    --skip-build)
      SKIP_BUILD="1"
      shift
      ;;
    --tag|--publish)
      echo "Publishing runs in Drone: push master and the tag; the tag's build tests and publishes under latest (see --help)." >&2
      exit 2
      ;;
    *)
      echo "Unknown arg: $1" >&2
      usage
      exit 2
      ;;
  esac
done

REPO_ROOT="$(git rev-parse --show-toplevel)"
CLI_DIR="$REPO_ROOT"

if [[ -n "$(git status --porcelain)" ]]; then
  echo "Working tree is not clean. Commit/stash changes before releasing." >&2
  git status --short
  exit 1
fi

BRANCH="$(git branch --show-current)"
if [[ "$BRANCH" != "master" ]]; then
  echo "Releases are cut on master (current branch: ${BRANCH:-detached HEAD}). Merge your work into master first." >&2
  exit 1
fi

RELEASE_TAG="boujot-cli-v$VERSION"
if git rev-parse -q --verify "refs/tags/$RELEASE_TAG" >/dev/null; then
  echo "Tag already exists: $RELEASE_TAG" >&2
  exit 1
fi

echo "Branch: master at $(git rev-parse --short HEAD)"
echo "Version: $VERSION"
echo "Tag: $RELEASE_TAG"

cd "$CLI_DIR"

echo "Checking the rebrand changes nothing..."
yarn rebrand >/dev/null
if [[ -n "$(git status --porcelain)" ]]; then
  echo "The rebrand changed files on master, so master is not fully @boujot/happy-coder:" >&2
  git status --short >&2
  echo "Commit those changes to master (or fix scripts/rebrand.ts), then release again. Nothing was committed or tagged." >&2
  exit 1
fi

echo "Bumping version..."
npm version "$VERSION" --no-git-tag-version >/dev/null

if [[ "$SKIP_BUILD" != "1" ]]; then
  echo "Building..."
  npm run build
else
  echo "Skipping build."
fi

git add -A "$CLI_DIR"
git commit -m "release: @boujot/happy-coder $VERSION" >/dev/null
git tag -a "$RELEASE_TAG" -m "@boujot/happy-coder $VERSION"

echo
echo "Release commit on master, tagged $RELEASE_TAG:"
git log --oneline -1
echo
echo "Push it (always a fast-forward): git push <remote> master $RELEASE_TAG"
echo "Remotes: $(git remote | tr '\n' ' ')"
echo

echo "Drone's build of the tag runs the tests once and publishes the version under latest."
