#!/bin/sh
# Kiantu CLI installer — https://kiantu.com/install.sh
#
#   curl -fsSL https://kiantu.com/install.sh | sh
#
# Downloads the signed + notarized `kiantu` binary and installs it to the
# first user-writable directory already on your PATH (e.g. /opt/homebrew/bin,
# ~/.local/bin), falling back to `sudo mv` into /usr/local/bin only when no
# such directory exists. Overrides: KIANTU_INSTALL_DIR=<dir> picks the
# destination; KIANTU_NO_SUDO=1 forbids the sudo fallback. macOS universal
# (Apple Silicon + Intel) today; Linux and Windows builds are on the way.
set -eu

BASE="https://www.kiantu.com/downloads/cli"
OS="$(uname -s)"
ARCH="$(uname -m)"

case "$OS" in
  Darwin)
    FILE="kiantu-macos-universal.tar.gz"
    ;;
  Linux)
    echo "Linux builds are on the way — email hello@kiantu.com and we'll bump them up the queue."
    echo "Meanwhile: https://www.kiantu.com/download/cli/"
    exit 1
    ;;
  *)
    echo "Unsupported platform: $OS $ARCH"
    echo "Windows and Linux builds are on the way: https://www.kiantu.com/download/cli/"
    exit 1
    ;;
esac

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

echo "Downloading kiantu ($FILE)..."
curl -fsSL "$BASE/$FILE" -o "$TMP/$FILE"
tar xzf "$TMP/$FILE" -C "$TMP"
chmod +x "$TMP/kiantu"

# Install destination, in order of preference:
#   1. KIANTU_INSTALL_DIR if you set it (no sudo ever).
#   2. A directory already on your PATH that you can write to WITHOUT sudo
#      (/opt/homebrew/bin on Apple Silicon Homebrew, /usr/local/bin if yours,
#      ~/.local/bin or ~/bin when they're on PATH).
#   3. /usr/local/bin via sudo — the only privileged step this script ever
#      takes is that single `mv` below (macOS ships /usr/local/bin root-owned;
#      nothing else runs as root). Set KIANTU_NO_SUDO=1 to forbid it.
#   4. ~/.local/bin without sudo (you may need to add it to PATH).
on_path() { case ":$PATH:" in *":$1:"*) return 0 ;; *) return 1 ;; esac }

DEST=""
if [ -n "${KIANTU_INSTALL_DIR:-}" ]; then
  DEST="$KIANTU_INSTALL_DIR"
  mkdir -p "$DEST"
else
  for CANDIDATE in /opt/homebrew/bin /usr/local/bin "$HOME/.local/bin" "$HOME/bin"; do
    if [ -d "$CANDIDATE" ] && [ -w "$CANDIDATE" ] && on_path "$CANDIDATE"; then
      DEST="$CANDIDATE"
      break
    fi
  done
fi

if [ -n "$DEST" ]; then
  mv "$TMP/kiantu" "$DEST/kiantu"
elif [ -z "${KIANTU_NO_SUDO:-}" ] && command -v sudo >/dev/null 2>&1; then
  DEST="/usr/local/bin"
  echo "Installing to $DEST, which is root-owned on macOS — sudo will prompt"
  echo "for your password to run ONE command: mv <tmpfile> $DEST/kiantu."
  echo "(Prefer no sudo? Ctrl-C and re-run with KIANTU_NO_SUDO=1, or see the"
  echo "manual steps at https://www.kiantu.com/download/cli/)"
  sudo mv "$TMP/kiantu" "$DEST/kiantu"
else
  DEST="$HOME/.local/bin"
  mkdir -p "$DEST"
  mv "$TMP/kiantu" "$DEST/kiantu"
  on_path "$DEST" || echo "Note: add $DEST to your PATH (export PATH=\"\$PATH:$DEST\")."
fi

echo ""
echo "✓ kiantu installed to $DEST/kiantu"
echo ""
echo "Next steps:"
echo "  1. kiantu login                # opens your browser — approve once"
echo "  2. kiantu start \"your first tracked session\""
echo ""
echo "(Agents/CI: skip login and export KIANTU_API_TOKEN=kt_... instead —"
echo " mint one at https://app.kiantu.com/settings, Security → API tokens.)"
echo ""
echo "Docs: https://www.kiantu.com/docs/integrations/terminal"
