#!/bin/sh
# Install agent-memory from a release tarball (no clone, no Go, no bun).
#
#   curl -fsSL https://1chain.net/download/install.sh | sh
#   curl -fsSL https://1chain.net/download/install.sh | sh -s -- --version 0.9.0 --bin-dir ~/bin
#
# Steps: detect OS/arch, download the tarball and SHA256SUMS, verify the
# checksum, copy agent-memory and am into the bin dir, then run
# `agent-memory install --no-tui --no-init`, which downloads ONNX Runtime and
# the MiniLM model into ~/.agent-memory.
set -eu

BASE_URL="${AGENT_MEMORY_BASE_URL:-https://1chain.net/download}"
VERSION="${AGENT_MEMORY_VERSION:-latest}"
BIN_DIR="${AGENT_MEMORY_BIN_DIR:-$HOME/.local/bin}"
RUN_SETUP=1

while [ $# -gt 0 ]; do
  case "$1" in
    --version) VERSION="$2"; shift 2 ;;
    --bin-dir) BIN_DIR="$2"; shift 2 ;;
    --base-url) BASE_URL="$2"; shift 2 ;;
    --no-setup) RUN_SETUP=0; shift ;;
    -h|--help) sed -n '2,10p' "$0" 2>/dev/null || true; exit 0 ;;
    *) echo "install: unknown flag $1" >&2; exit 2 ;;
  esac
done

die() { echo "install: $*" >&2; exit 1; }
need() { command -v "$1" >/dev/null 2>&1 || die "$1 is required"; }
need curl; need tar; need uname

case "$(uname -s)" in
  Darwin) os=darwin ;;
  Linux) os=linux ;;
  *) die "unsupported OS $(uname -s); macOS and Linux only" ;;
esac
case "$(uname -m)" in
  arm64|aarch64) arch=arm64 ;;
  x86_64|amd64) arch=amd64 ;;
  *) die "unsupported CPU $(uname -m)" ;;
esac

if [ "$VERSION" = latest ]; then
  VERSION="$(curl -fsSL "$BASE_URL/latest.txt" | tr -d ' \r\n')" || die "cannot read $BASE_URL/latest.txt"
  [ -n "$VERSION" ] || die "empty latest.txt"
fi

name="agent-memory_${VERSION}_${os}_${arch}"
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT INT TERM

echo "==> downloading $name"
curl -fsSL -o "$tmp/$name.tar.gz" "$BASE_URL/$VERSION/$name.tar.gz" || die "download failed: $BASE_URL/$VERSION/$name.tar.gz"
curl -fsSL -o "$tmp/SHA256SUMS" "$BASE_URL/$VERSION/SHA256SUMS" || die "download failed: SHA256SUMS"

want="$(awk -v f="$name.tar.gz" '$2==f {print $1}' "$tmp/SHA256SUMS")"
[ -n "$want" ] || die "no checksum for $name.tar.gz"
if command -v sha256sum >/dev/null 2>&1; then
  got="$(sha256sum "$tmp/$name.tar.gz" | awk '{print $1}')"
else
  got="$(shasum -a 256 "$tmp/$name.tar.gz" | awk '{print $1}')"
fi
[ "$want" = "$got" ] || die "checksum mismatch for $name.tar.gz"
echo "    checksum ok"

tar -xzf "$tmp/$name.tar.gz" -C "$tmp"
mkdir -p "$BIN_DIR"
for b in agent-memory am; do
  # Copy then rename so a running board keeps its old file.
  cp "$tmp/$name/$b" "$BIN_DIR/.$b.tmp"
  chmod 0755 "$BIN_DIR/.$b.tmp"
  mv -f "$BIN_DIR/.$b.tmp" "$BIN_DIR/$b"
done
echo "==> installed agent-memory and am into $BIN_DIR"

case ":$PATH:" in
  *":$BIN_DIR:"*) ;;
  *) echo "    add $BIN_DIR to PATH, e.g. export PATH=\"$BIN_DIR:\$PATH\"" ;;
esac

if [ "$RUN_SETUP" -eq 1 ]; then
  echo "==> downloading ONNX Runtime and the embedding model"
  "$BIN_DIR/agent-memory" install --no-tui --no-init --bin-dir "$BIN_DIR" </dev/null
fi
echo "==> done. Next: cd your-project && am connect claude-code"
