#!/usr/bin/env bash
# local-ai-bridge 0.1.0 installer (macOS). Usage:
#   curl -fsSL https://rabbitxai.com/download/ai-bridge/get.sh | bash -s -- "$HOME/Documents:$HOME/Projects"
# The argument = folders the AIs may work in (full paths, separated by ":").
set -euo pipefail
version="0.1.0"
sha="a70de2081347b34ffb1a287d84dd82a0ce9fcc35b5523fd31f6ba87a5ece1cce"
url="https://rabbitxai.com/download/ai-bridge/local-ai-bridge-$version.zip"
dest="$HOME/.local/share/local-ai-bridge"

[ "$(uname)" = "Darwin" ] || { echo "This installer is for macOS." >&2; exit 1; }
[ -n "${1:-}" ] || { echo "Tell me which folders the AIs may use, e.g.: bash -s -- \"\$HOME/Documents\"" >&2; exit 1; }
if ! command -v uv >/dev/null; then
  echo "Installing uv (Python runner from astral.sh)..."
  curl -LsSf https://astral.sh/uv/install.sh | sh
  export PATH="$HOME/.local/bin:$PATH"
fi

tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT
curl -fsSL "$url" -o "$tmp/bridge.zip"
echo "$sha  $tmp/bridge.zip" | shasum -a 256 -c - >/dev/null || { echo "Download is damaged (checksum mismatch). Nothing was installed." >&2; exit 1; }
unzip -q "$tmp/bridge.zip" -d "$tmp"
mkdir -p "$(dirname "$dest")"
rm -rf "$dest.old"
[ -d "$dest" ] && mv "$dest" "$dest.old"  # an update keeps the previous copy until the new one is in place
mv "$tmp/local-ai-bridge-$version" "$dest"
rm -rf "$dest.old"
"$dest/install.sh" "$1"
echo
echo "Installed local-ai-bridge $version in $dest"
echo "Uninstall any time: $dest/uninstall.sh"
