#!/usr/bin/env bash
# Velvet brain installer.
#
#   curl -fsSL https://getvelvet.tv/install.sh | bash
#
# What it does, and nothing else:
#   1. Picks the prebuilt velvet-brain for this machine (Linux x86_64, Linux arm64, macOS arm64).
#   2. Downloads that tarball and SHA256SUMS.txt from the latest GitHub release.
#   3. Verifies the tarball's SHA-256 against SHA256SUMS.txt and stops if it does not match.
#   4. Installs velvet-brain and the velvet symlink into ~/.local/bin, and the provider
#      plugins into ~/.local/share/velvet/plugins.
# It never uses sudo, never starts a service, and never asks for a key.
# To install from a mirror of the release assets, set VELVET_RELEASE_BASE to its URL.
set -euo pipefail

main() {
  local base target tarball sums_line verified tool
  base="${VELVET_RELEASE_BASE:-https://github.com/0xBreadguy/velvet/releases/latest/download}"

  case "$(uname -sm)" in
    "Linux x86_64")  target=x86_64-unknown-linux-gnu ;;
    "Linux aarch64") target=aarch64-unknown-linux-gnu ;;
    "Darwin arm64")  target=aarch64-apple-darwin ;;
    *)
      echo "velvet: no prebuilt binary for $(uname -sm)." >&2
      echo "velvet: use Docker or build from source: https://github.com/0xBreadguy/velvet" >&2
      exit 1
      ;;
  esac

  for tool in curl tar install; do
    command -v "$tool" >/dev/null 2>&1 || { echo "velvet: '$tool' is required but was not found." >&2; exit 1; }
  done

  tarball="velvet-brain-$target.tar.gz"
  # Not a local: the EXIT trap runs after main returns and still needs it.
  velvet_tmp="$(mktemp -d)"
  trap 'rm -rf "$velvet_tmp"' EXIT
  cd "$velvet_tmp"

  echo "velvet: downloading $tarball"
  curl -fsSLO "$base/$tarball"
  curl -fsSLO "$base/SHA256SUMS.txt"

  # Exactly one checksum line must name this tarball. Anything else is treated as tampering.
  sums_line="$(grep -E "^[0-9a-fA-F]{64}[[:space:]]+[*]?$tarball\$" SHA256SUMS.txt || true)"
  if [ -z "$sums_line" ] || [ "$(printf '%s\n' "$sums_line" | wc -l | tr -d ' ')" != "1" ]; then
    echo "velvet: SHA256SUMS.txt has no single entry for $tarball. Nothing was installed." >&2
    exit 1
  fi

  echo "velvet: verifying SHA-256"
  if command -v sha256sum >/dev/null 2>&1; then
    verified="$(printf '%s\n' "$sums_line" | sha256sum -c - >/dev/null 2>&1 && echo yes || echo no)"
  elif command -v shasum >/dev/null 2>&1; then
    verified="$(printf '%s\n' "$sums_line" | shasum -a 256 -c - >/dev/null 2>&1 && echo yes || echo no)"
  else
    echo "velvet: neither sha256sum nor shasum is available, so the download cannot be verified. Nothing was installed." >&2
    exit 1
  fi
  if [ "$verified" != "yes" ]; then
    echo "velvet: SHA-256 mismatch for $tarball. The download is corrupt or was altered. Nothing was installed." >&2
    exit 1
  fi

  tar xzf "$tarball"
  mkdir -p "$HOME/.local/bin" "$HOME/.local/share/velvet/plugins" "$HOME/.config/velvet"
  install -m 0755 "velvet-brain-$target/velvet-brain" "$HOME/.local/bin/velvet-brain"
  ln -sf velvet-brain "$HOME/.local/bin/velvet"
  cp "velvet-brain-$target"/plugins/*.component.wasm "$HOME/.local/share/velvet/plugins/"
  cp "velvet-brain-$target/velvet.example.toml" "$HOME/.local/share/velvet/"

  echo
  echo "velvet: installed velvet-brain to ~/.local/bin (checksum verified)."
  case ":$PATH:" in
    *":$HOME/.local/bin:"*) ;;
    *) echo "velvet: ~/.local/bin is not on your PATH. Add it to your shell profile first." ;;
  esac
  cat <<'NEXT'

Next steps:

  velvet-brain init --config ~/.config/velvet/velvet.toml
  velvet-brain config set brain.plugins_dir "$HOME/.local/share/velvet/plugins" --config ~/.config/velvet/velvet.toml
  velvet-brain config set brain.data_dir "$HOME/.local/share/velvet/data" --config ~/.config/velvet/velvet.toml
  velvet-brain service install --config ~/.config/velvet/velvet.toml
  velvet-brain setup-url --config ~/.config/velvet/velvet.toml

init writes the config, service install keeps the brain running across reboots, and
setup-url prints a private link for adding your provider keys from a phone or laptop.
Full guide: https://github.com/0xBreadguy/velvet/blob/master/docs/AGENT-SETUP.md
NEXT
}

main "$@"
