#!/bin/sh
# a8 console installer — https://get.abliter8.ai/install.sh
#
# Read this before running it. That is not a formality: you were probably told to pipe it to a
# shell, and the only reason that is reasonable is that you can read it first and it is short.
#
#   curl -fsSL https://get.abliter8.ai/install.sh -o install.sh
#   less install.sh && sh install.sh
#
# What it does:
#   * checks for git and Node 22+, and STOPS if either is missing (it does not install them)
#   * clones the repo, or updates an existing clone
#   * checks out a pinned release tag, never a moving branch
#   * enables corepack, installs dependencies, builds
#
# What it will never do: use sudo, install a toolchain behind your back, edit your shell profile,
# or start anything listening beyond localhost.
#
# Re-running is safe. Nothing is deleted.

set -eu

REPO_URL="${A8_REPO_URL:-https://github.com/abliter8-ai/a8-fleet-console.git}"
TARGET="${A8_DIR:-$HOME/a8-fleet-console}"
# Pinned by the release process. The hosted script is a bootstrapper; the payload is a git tag.
PINNED_REF="${A8_REF:-main}"
DRY_RUN=0
NODE_MIN=22

for arg in "$@"; do
  case "$arg" in
    --dry-run) DRY_RUN=1 ;;
    --dir=*)   TARGET="${arg#--dir=}" ;;
    --ref=*)   PINNED_REF="${arg#--ref=}" ;;
    -h|--help)
      echo "usage: install.sh [--dry-run] [--dir=PATH] [--ref=TAG]"
      exit 0 ;;
    *) echo "unknown option: $arg" >&2; exit 2 ;;
  esac
done

say()  { printf '  %s\n' "$1"; }
step() { printf '\n▸ %s\n' "$1"; }
die()  { printf '\nstopped: %s\n' "$1" >&2; exit 1; }

run() {
  if [ "$DRY_RUN" -eq 1 ]; then
    printf '  would run: %s\n' "$*"
  else
    "$@"
  fi
}

printf 'a8 console installer\n'
[ "$DRY_RUN" -eq 1 ] && say "(dry run — nothing will be changed)"

# ── prerequisites ─────────────────────────────────────────────────────────
# Checked up front and together, so a missing tool is not discovered halfway through a clone.
step "Checking prerequisites"

command -v git >/dev/null 2>&1 || die "git is not installed. Install it, then run this again."
say "git $(git --version | awk '{print $3}')"

command -v node >/dev/null 2>&1 || die \
"Node $NODE_MIN or newer is not installed.

This installer deliberately does not install it for you — putting a toolchain on your machine
without asking is exactly the kind of thing a8 argues against. Install Node from
https://nodejs.org (or your package manager), then run this again."

NODE_MAJOR=$(node -p 'process.versions.node.split(".")[0]' 2>/dev/null || echo 0)
[ "$NODE_MAJOR" -ge "$NODE_MIN" ] || die \
"Node $NODE_MIN or newer is required; found $(node --version).

Upgrade Node, then run this again. Nothing has been changed."
say "node $(node --version)"

# ── source ────────────────────────────────────────────────────────────────
step "Getting the source"

if [ -d "$TARGET/.git" ]; then
  say "updating the existing clone at $TARGET"
  run git -C "$TARGET" fetch --tags --quiet origin
else
  say "cloning into $TARGET"
  run git clone --quiet "$REPO_URL" "$TARGET"
fi

# A tag, not a branch: the hosted script bootstraps, the tag is what actually runs.
if [ "$DRY_RUN" -eq 0 ]; then
  git -C "$TARGET" checkout --quiet "$PINNED_REF" 2>/dev/null \
    || die "could not check out '$PINNED_REF'. Pass --ref=<tag> with a release that exists."
  say "at $(git -C "$TARGET" describe --tags --always 2>/dev/null || echo "$PINNED_REF")"
else
  printf '  would run: git -C %s checkout %s\n' "$TARGET" "$PINNED_REF"
fi

# ── build ─────────────────────────────────────────────────────────────────
# corepack ships with Node and pins pnpm from the repo, so no global install is needed.
step "Installing dependencies"
run corepack enable
run sh -c "cd '$TARGET' && pnpm install --frozen-lockfile"

step "Building"
say "this is the real check — if it fails, do not ignore it"
run sh -c "cd '$TARGET' && pnpm build"

# ── done ──────────────────────────────────────────────────────────────────
if [ "$DRY_RUN" -eq 1 ]; then
  cat <<PLANNED

Dry run complete. Nothing was changed.

Run without --dry-run to install to $TARGET.
PLANNED
  exit 0
fi

cat <<DONE

Installed to $TARGET

Start it:

  cd $TARGET
  pnpm start

Then open http://localhost:3000/startup

It opens with no configuration. Nothing on your network is contacted until you ask it to look.
DONE
