#!/bin/sh
# Install the silverbaths CLI.
#   curl -fsSL https://silverbaths.com/install.sh | sh
# Options (environment): SILVERBATHS_VERSION=v0.2.0  SILVERBATHS_DIR=~/.local/bin  SILVERBATHS_NO_ALIAS=1
set -eu

repo="sumukshashidhar/silverbaths"
dir="${SILVERBATHS_DIR:-$HOME/.local/bin}"
version="${SILVERBATHS_VERSION:-latest}"

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

os=$(uname -s)
arch=$(uname -m)
case "$os-$arch" in
  Darwin-arm64) target="aarch64-apple-darwin" ;;
  Darwin-x86_64) target="x86_64-apple-darwin" ;;
  Linux-x86_64) target="x86_64-unknown-linux-gnu" ;;
  Linux-aarch64 | Linux-arm64) target="aarch64-unknown-linux-gnu" ;;
  *) fail "no prebuilt binary for $os $arch; install with: cargo install silverbaths" ;;
esac

if [ "$version" = latest ]; then
  url="https://github.com/$repo/releases/latest/download/silverbaths-$target.tar.gz"
else
  url="https://github.com/$repo/releases/download/$version/silverbaths-$target.tar.gz"
fi

tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
say "downloading $url"
if command -v curl >/dev/null 2>&1; then
  curl -fsSL "$url" -o "$tmp/sb.tar.gz" || fail "download failed; install with: cargo install silverbaths"
else
  wget -qO "$tmp/sb.tar.gz" "$url" || fail "download failed; install with: cargo install silverbaths"
fi
tar -xzf "$tmp/sb.tar.gz" -C "$tmp"
mkdir -p "$dir"
install -m 755 "$tmp/silverbaths" "$dir/silverbaths"

# short alias, unless something else is already called sb
if [ -z "${SILVERBATHS_NO_ALIAS:-}" ] && ! command -v sb >/dev/null 2>&1; then
  ln -sf "$dir/silverbaths" "$dir/sb"
  alias_note=" (also available as sb)"
else
  alias_note=""
fi

say "installed $("$dir/silverbaths" --version) to $dir/silverbaths$alias_note"
case ":$PATH:" in
  *":$dir:"*) ;;
  *) say "add $dir to your PATH, e.g.: echo 'export PATH=\"$dir:\$PATH\"' >> ~/.zshrc" ;;
esac
command -v ffmpeg >/dev/null 2>&1 || say "for video, also install ffmpeg (macOS: brew install ffmpeg)"
say "next: silverbaths --help    agents: silverbaths skills install    MCP: silverbaths mcp"
