#!/bin/sh
# jetsyn installer — curl -fsSL https://jetsyn.ai/install | sh
#
# Downloads the jetsyn CLI from https://jetsyn.ai/cli/jetsyn, installs it onto a
# directory already on your PATH (prefers /usr/local/bin, falls back to
# ~/.local/bin), makes it executable, and verifies it runs. POSIX sh, no
# bashisms. Fails loudly on any error.
set -eu

CLI_URL="https://jetsyn.ai/cli/jetsyn"
BIN_NAME="jetsyn"

# --- tiny output helpers (color only when stdout is a TTY) -------------------
if [ -t 1 ]; then
  C_OK="$(printf '\033[32m')"; C_ERR="$(printf '\033[31m')"
  C_DIM="$(printf '\033[2m')"; C_RST="$(printf '\033[0m')"; C_BOLD="$(printf '\033[1m')"
else
  C_OK=''; C_ERR=''; C_DIM=''; C_RST=''; C_BOLD=''
fi
say()  { printf '%s\n' "$*"; }
ok()   { printf '%s\xe2\x9c\x94%s %s\n' "$C_OK" "$C_RST" "$*"; }
info() { printf '%s%s%s\n' "$C_DIM" "$*" "$C_RST"; }
die()  { printf '%s\xe2\x9c\x96 %s%s\n' "$C_ERR" "$*" "$C_RST" >&2; exit 1; }

# --- preflight ---------------------------------------------------------------
command -v curl >/dev/null 2>&1 || die "curl is required but not found. Install curl and re-run."

# --- pick an install dir that is on PATH (writable) --------------------------
# Prefer /usr/local/bin, then ~/.local/bin. We only pick a dir that is actually
# on $PATH so the user can run jetsyn immediately without shell config.
on_path() {
  case ":${PATH}:" in
    *":$1:"*) return 0 ;;
    *) return 1 ;;
  esac
}
writable_dir() {
  if [ -d "$1" ]; then [ -w "$1" ]; return; fi
  ( mkdir -p "$1" >/dev/null 2>&1 ) && [ -w "$1" ]
}

INSTALL_DIR=''
SUDO=''
if on_path /usr/local/bin && writable_dir /usr/local/bin; then
  INSTALL_DIR=/usr/local/bin
elif on_path "$HOME/.local/bin" && writable_dir "$HOME/.local/bin"; then
  INSTALL_DIR="$HOME/.local/bin"
elif on_path /usr/local/bin && command -v sudo >/dev/null 2>&1; then
  INSTALL_DIR=/usr/local/bin
  SUDO=sudo
  info "Need elevated permissions to write to /usr/local/bin; sudo will prompt."
else
  INSTALL_DIR="$HOME/.local/bin"
  mkdir -p "$INSTALL_DIR" || die "Cannot create $INSTALL_DIR"
  if ! on_path "$INSTALL_DIR"; then
    NOT_ON_PATH=1
  fi
fi

DEST="$INSTALL_DIR/$BIN_NAME"

# --- download to a temp file, then atomically install ------------------------
TMP="$(mktemp 2>/dev/null || echo "${TMPDIR:-/tmp}/jetsyn.$$")"
# shellcheck disable=SC2064
trap 'rm -f "$TMP"' EXIT INT TERM

info "Downloading jetsyn from $CLI_URL ..."
curl -fsSL --max-time 60 "$CLI_URL" -o "$TMP" || die "download failed: $CLI_URL"

# Sanity-check what we got: must look like the bash script, not an HTML page.
if ! head -1 "$TMP" | grep -q 'bash'; then
  die "downloaded file is not the jetsyn script (got an unexpected response). Try again later."
fi
[ "$(wc -c < "$TMP")" -ge 500 ] || die "downloaded file is too small; aborting."

chmod 755 "$TMP" || die "could not mark the script executable"

if [ -n "$SUDO" ]; then
  $SUDO mv -f "$TMP" "$DEST" || die "could not install to $DEST"
  $SUDO chmod 755 "$DEST" || true
else
  mv -f "$TMP" "$DEST" || die "could not install to $DEST"
fi
trap - EXIT INT TERM
ok "Installed jetsyn to $DEST"

# --- verify it runs ----------------------------------------------------------
VER="$("$DEST" --version 2>/dev/null || true)"
case "$VER" in
  jetsyn\ *) ok "Verified: $VER" ;;
  *) die "installed jetsyn but '$DEST --version' did not run as expected." ;;
esac

# --- next steps --------------------------------------------------------------
echo
# The white JETSYN wordmark — the install "wow". Running `jetsyn` then throws up the
# PURPLE command-center banner; this is its white, pre-launch twin.
printf '%s\n' "${C_BOLD}     ██╗███████╗████████╗███████╗██╗   ██╗███╗   ██╗${C_RST}"
printf '%s\n' "${C_BOLD}     ██║██╔════╝╚══██╔══╝██╔════╝╚██╗ ██╔╝████╗  ██║${C_RST}"
printf '%s\n' "${C_BOLD}     ██║█████╗     ██║   ███████╗ ╚████╔╝ ██╔██╗ ██║${C_RST}"
printf '%s\n' "${C_BOLD}██   ██║██╔══╝     ██║   ╚════██║  ╚██╔╝  ██║╚██╗██║${C_RST}"
printf '%s\n' "${C_BOLD}╚█████╔╝███████╗   ██║   ███████║   ██║   ██║ ╚████║${C_RST}"
printf '%s\n' "${C_BOLD} ╚════╝ ╚══════╝   ╚═╝   ╚══════╝   ╚═╝   ╚═══╝     ${C_RST}"
echo
if [ "${NOT_ON_PATH:-0}" = "1" ]; then
  say "  First add it to your PATH (so new terminals find it):"
  say "       export PATH=\"$INSTALL_DIR:\$PATH\""
  echo
fi
printf '  %s\xe2\x96\xb6  Now run:%s   %sjetsyn%s\n' "$C_BOLD" "$C_RST" "$C_BOLD" "$C_RST"
echo
info "It signs you in in your browser, then opens Claude Code in your cloud cell."
