#!/bin/sh
# lore installer: puts the `ai-memory` client on this machine and connects
# your coding agents to lore.
#
#   curl -fsSL https://app.lorememory.ai/web/install.sh | sh
#
# It downloads the client built for this system, checks it against the
# published SHA-256, installs it in ~/.local/bin (or $LORE_INSTALL_DIR), then
# runs `ai-memory setup`, which signs this machine in through your browser
# (it prints a link and a code when there is no browser) and sets up every
# supported agent it finds. Arguments after `sh -s --` go to
# `setup`, e.g. `| sh -s -- --agent codex`. Running it again updates the
# client and rewrites the same configuration.
set -eu

SERVER="${LORE_SERVER:-https://app.lorememory.ai}"
BASE="$SERVER/web/dl"
DIR="${LORE_INSTALL_DIR:-$HOME/.local/bin}"
SHARE="$HOME/.local/share/lore"

say() { printf '%s\n' "$*"; }
fail() {
	printf 'lore install: %s\n' "$*" >&2
	exit 1
}

case "$(uname -s)" in
Darwin) os=macos ;;
Linux) os=linux ;;
*) fail "this installer supports macOS and Linux" ;;
esac
case "$(uname -m)" in
arm64 | aarch64) arch=aarch64 ;;
x86_64 | amd64) arch=x86_64 ;;
*) fail "no client is built for this processor ($(uname -m))" ;;
esac
asset="ai-memory-$os-$arch.tar.gz"

command -v curl >/dev/null 2>&1 || fail "needs curl"
command -v tar >/dev/null 2>&1 || fail "needs tar"
if command -v sha256sum >/dev/null 2>&1; then
	sha() { sha256sum "$1" | cut -d' ' -f1; }
elif command -v shasum >/dev/null 2>&1; then
	sha() { shasum -a 256 "$1" | cut -d' ' -f1; }
else
	fail "needs sha256sum or shasum to check the download"
fi

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

say "Downloading the lore client for $os/$arch..."
curl -fsSL "$BASE/$asset" -o "$tmp/$asset" || fail "could not download $BASE/$asset"
curl -fsSL "$BASE/SHA256SUMS" -o "$tmp/SHA256SUMS" || fail "could not download $BASE/SHA256SUMS"
want="$(grep " $asset\$" "$tmp/SHA256SUMS" | cut -d' ' -f1)"
[ -n "$want" ] || fail "no checksum is published for $asset"
[ "$(sha "$tmp/$asset")" = "$want" ] || fail "the download does not match its checksum; nothing was installed"

tar -xzf "$tmp/$asset" -C "$tmp"
[ -f "$tmp/ai-memory" ] && [ -d "$tmp/hooks" ] || fail "the download is incomplete; nothing was installed"
mkdir -p "$DIR" "$SHARE"
cp "$tmp/ai-memory" "$DIR/ai-memory.new"
chmod 0755 "$DIR/ai-memory.new"
mv -f "$DIR/ai-memory.new" "$DIR/ai-memory"
ln -sf ai-memory "$DIR/lore"
# The hook bundle `setup` installs from; it looks here by default.
rm -rf "$SHARE/hooks.new"
cp -R "$tmp/hooks" "$SHARE/hooks.new"
rm -rf "$SHARE/hooks"
mv "$SHARE/hooks.new" "$SHARE/hooks"
say "Installed $DIR/ai-memory ($("$DIR/ai-memory" --version))"
case ":$PATH:" in
*":$DIR:"*) ;;
*) say "Note: $DIR is not on your PATH. Add it to run \`lore\` and \`ai-memory\` by name." ;;
esac

# This script arrives on a pipe, so setup reads from the terminal itself
# (`--paste-key` asks there). `-r` is not enough: /dev/tty can exist and still
# fail to open (no controlling terminal), so try opening it. Browser sign-in
# needs no terminal, so setup runs either way.
if (: </dev/tty) 2>/dev/null; then
	"$DIR/ai-memory" setup --server-url "$SERVER" "$@" </dev/tty
else
	"$DIR/ai-memory" setup --server-url "$SERVER" "$@"
fi
