#!/bin/sh
# Installs demetra-cli's bundled single-file release. Uploaded to the CLI
# CDN's bucket root by demetra-cli-release.yml, served at
# https://cli.<zone>/install.sh — and at the bare https://cli.<zone> too,
# via the CloudFront 403->install.sh rewrite (cli_cdn.py).
set -eu

CDN_HOST="${DEMETRA_CLI_CDN_HOST:-cli.dev.demetra.simetrik.com}"
INSTALL_DIR="${DEMETRA_CLI_INSTALL_DIR:-$HOME/.demetra/bin}"

if ! command -v node >/dev/null 2>&1; then
  echo "demetra-cli needs Node.js >= 20 on PATH. Install it first (e.g. https://nodejs.org), then re-run this script." >&2
  exit 1
fi

NODE_MAJOR=$(node -e 'console.log(process.versions.node.split(".")[0])')
if [ "$NODE_MAJOR" -lt 20 ]; then
  echo "demetra-cli needs Node.js >= 20, found $(node -v). Upgrade Node, then re-run this script." >&2
  exit 1
fi

TMP_DIR=$(mktemp -d)
trap 'rm -rf "$TMP_DIR"' EXIT

echo "Downloading demetra-cli (stable) from https://$CDN_HOST ..."
# `demetra` and `demetra.sha256` — the exact names demetra-cli-release.yml
# uploads (it renames the esbuild bundle before publishing), not the raw
# `demetra-bundled.mjs` build artifact name.
curl -fsSL "https://$CDN_HOST/cli/stable/demetra" -o "$TMP_DIR/demetra"
curl -fsSL "https://$CDN_HOST/cli/stable/demetra.sha256" -o "$TMP_DIR/demetra.sha256"

cd "$TMP_DIR"
if command -v sha256sum >/dev/null 2>&1; then
  sha256sum -c demetra.sha256
elif command -v shasum >/dev/null 2>&1; then
  shasum -a 256 -c demetra.sha256
else
  echo "no sha256sum or shasum found — cannot verify the download's integrity, refusing to install an unverified binary" >&2
  exit 1
fi

mkdir -p "$INSTALL_DIR"
cp demetra "$INSTALL_DIR/demetra"
chmod +x "$INSTALL_DIR/demetra"
# SOUP-1000: record WHERE this binary came from, beside the binary — the CLI
# shows it next to the active environment (`demetra version --verbose`), so a
# dev-binary/prod-API combination is one visible line instead of a deduction.
printf '%s\n' "$CDN_HOST" > "$INSTALL_DIR/demetra.origin"

echo "Installed demetra to $INSTALL_DIR/demetra"

# "I installed it" and "my shell runs it" are different facts, and only the
# second one is the feature. Measured on a real machine on 2026-09-13: the
# correct 24-verb binary had been sitting in $INSTALL_DIR since the day before
# while `demetra` still resolved to a ten-day-old `npm -g` with 8 verbs — this
# script printed "Installed demetra to …" and changed nothing an operator could
# observe. Saying WHICH one wins turns a silent no-op into one visible line.
WINNER="$(command -v demetra 2>/dev/null || true)"

case ":$PATH:" in
  *":$INSTALL_DIR:"*)
    if [ -n "$WINNER" ] && [ "$WINNER" != "$INSTALL_DIR/demetra" ]; then
      echo
      echo "WARNING: another 'demetra' comes FIRST on your PATH, so typing"
      echo "'demetra' still runs that one, not the version just installed:"
      echo "  $WINNER"
      echo "Put this one ahead of it, or remove the other:"
      echo "  export PATH=\"$INSTALL_DIR:\$PATH\""
    else
      echo "Run: demetra login"
    fi
    ;;
  *)
    echo "Add $INSTALL_DIR to your PATH, e.g.:"
    echo "  echo 'export PATH=\"$INSTALL_DIR:\$PATH\"' >> ~/.bashrc   # or ~/.zshrc"
    if [ -n "$WINNER" ]; then
      echo "Prepend it, do NOT append: another 'demetra' already answers first:"
      echo "  $WINNER"
    fi
    echo "Then run: demetra login"
    ;;
esac

# A shell ALIAS beats PATH outright and is invisible from here — this script is
# not the interactive shell, so `command -v` cannot see one. On the machine
# above, `alias demetra=...tmux-launch.sh` in ~/.zshrc won over both binaries.
echo
echo "Check what your shell will actually run:"
echo "  type demetra                       # an alias wins over PATH"
echo "  demetra ship --help | head -1      # must say: Usage: demetra ship [options]"
