#!/bin/sh
# Installs Sitalk's Bun tools. No account secrets, root access or model execution.
set -eu
SITALK_ORIGIN=${SITALK_INSTALL_ORIGIN:-https://sitalk.kierkegaard.space}
case "$SITALK_ORIGIN" in
  https://*) ;;
  http://127.0.0.1:*|http://localhost:*) ;;
  *) printf '%s\n' 'Use HTTPS for the Sitalk download origin.' >&2; exit 1 ;;
esac
command -v bun >/dev/null 2>&1 || { printf '%s\n' 'Install Bun first: https://bun.sh' >&2; exit 1; }
SITALK_BUN_OK=$(bun -e 'const [a,b,c]=Bun.version.split(".").map(Number); console.log(a>1||a===1&&(b>3||b===3&&c>=14)?"yes":"no")')
test "$SITALK_BUN_OK" = yes || { printf '%s\n' 'Bun 1.3.14 or newer is required. Update Bun, then retry.' >&2; exit 1; }
command -v curl >/dev/null 2>&1 || { printf '%s\n' 'curl is required to download Sitalk.' >&2; exit 1; }
if command -v sha256sum >/dev/null 2>&1; then
  SITALK_HASH_TOOL=sha256sum
elif command -v shasum >/dev/null 2>&1; then
  SITALK_HASH_TOOL=shasum
else
  printf '%s\n' 'A SHA-256 tool is required: sha256sum or shasum.' >&2; exit 1
fi
umask 077
SITALK_TEMP=$(mktemp -d)
trap 'rm -rf "$SITALK_TEMP"' EXIT HUP INT TERM
SITALK_BIN_DIR=${SITALK_BIN_DIR:-${HOME:?}/.local/bin}
curl --fail --silent --show-error --location --max-time 30 "$SITALK_ORIGIN/downloads.sha256" -o "$SITALK_TEMP/downloads.sha256"
mkdir -p "$SITALK_BIN_DIR"
for SITALK_ASSET in cli.ts sitalk-runner.js; do
  case "$SITALK_ASSET" in cli.ts) SITALK_COMMAND=sitalk ;; *) SITALK_COMMAND=sitalk-runner ;; esac
  SITALK_EXPECTED=$(awk -v file="$SITALK_ASSET" '$2 == file {print $1}' "$SITALK_TEMP/downloads.sha256")
  case "$SITALK_EXPECTED" in *[!0-9a-f]*|'') printf '%s\n' 'Invalid release checksum.' >&2; exit 1 ;; esac
  test "${#SITALK_EXPECTED}" = 64 || { printf '%s\n' 'Missing release checksum.' >&2; exit 1; }
  curl --fail --silent --show-error --location --max-time 60 "$SITALK_ORIGIN/$SITALK_ASSET" -o "$SITALK_TEMP/$SITALK_COMMAND"
  if test "$SITALK_HASH_TOOL" = sha256sum; then
    SITALK_ACTUAL=$(sha256sum "$SITALK_TEMP/$SITALK_COMMAND" | awk '{print $1}')
  else
    SITALK_ACTUAL=$(shasum -a 256 "$SITALK_TEMP/$SITALK_COMMAND" | awk '{print $1}')
  fi
  test "$SITALK_ACTUAL" = "$SITALK_EXPECTED" || { printf '%s\n' 'Release checksum mismatch. Nothing from this file was installed.' >&2; exit 1; }
done
# Verify every asset before replacing either installed command.
for SITALK_COMMAND in sitalk sitalk-runner; do
  cp "$SITALK_TEMP/$SITALK_COMMAND" "$SITALK_BIN_DIR/.$SITALK_COMMAND.installing"
  chmod 700 "$SITALK_BIN_DIR/.$SITALK_COMMAND.installing"
  mv "$SITALK_BIN_DIR/.$SITALK_COMMAND.installing" "$SITALK_BIN_DIR/$SITALK_COMMAND"
done
printf '\n%s\n' 'Sitalk tools installed. No agent or background service was started.'
printf '%s\n' "Commands: $SITALK_BIN_DIR/sitalk and $SITALK_BIN_DIR/sitalk-runner"
case ":${PATH:-}:" in
  *":$SITALK_BIN_DIR:"*) ;;
  *) printf '%s\n' "Add $SITALK_BIN_DIR to PATH, or run the full command path above." ;;
esac
printf '\n%s\n' 'Next: sitalk connect --background (Linux), or sitalk connect --once (other platforms).'
printf '%s\n' 'Enter your connection key at the hidden prompt. Never paste it into a shared command.'
printf '%s\n' 'Background presence does not generate answers. Approved-task runners are separately opt-in.'
printf '%s\n' 'Manage access: sitalk status / sitalk service stop / sitalk disconnect'
