#!/bin/sh
# spinyield installer — https://spinyield.com/install.sh
#
#   curl -fsSL https://spinyield.com/install.sh | sh
#
# Installs the `spinyield` CLI globally with npm (falling back to a user-local prefix if
# the global one isn't writable — e.g. NixOS, where npm's prefix is the read-only
# /nix/store). spinyield renders a sponsored line in your Claude Code status line and pays
# you a 70% share for clean impressions. It's a plain JS package that runs on the Node you
# already have (Claude Code runs on Node too), so there's no native binary and nothing to code-sign.
set -eu

# Package to install. Overridable for local/staging testing (e.g. a packed tarball).
PKG="${SPINYIELD_NPM_PKG:-spinyield}"

info() { printf '\033[36m›\033[0m %s\n' "$1"; }
err() { printf '\033[31m✗ %s\033[0m\n' "$1" >&2; }

if ! command -v node >/dev/null 2>&1; then
  err "Node.js is required but was not found."
  echo "  spinyield runs on Node (so does Claude Code). Install Node ≥ 20 from" >&2
  echo "  https://nodejs.org and re-run this installer." >&2
  exit 1
fi

if ! command -v npm >/dev/null 2>&1; then
  err "npm is required but was not found (it ships with Node.js)."
  echo "  Install Node ≥ 20 from https://nodejs.org and re-run." >&2
  exit 1
fi

info "Installing spinyield with npm…"
# INSTALL_PREFIX stays empty when the default global prefix works; it's set below when we
# fall back to a user-writable prefix.
INSTALL_PREFIX=""
if npm install -g "$PKG"; then
  :
else
  # The default global prefix wasn't writable. This is normal on NixOS (npm's prefix lives
  # in the read-only /nix/store) and on some system/Homebrew Node setups. Rather than force
  # sudo, retry into a user-writable prefix — no root, nothing system-wide to break.
  USER_PREFIX="${SPINYIELD_PREFIX:-$HOME/.spinyield}"
  info "Global prefix isn't writable — installing into $USER_PREFIX instead (no sudo)…"
  mkdir -p "$USER_PREFIX"
  if npm install -g "$PKG" --prefix "$USER_PREFIX"; then
    INSTALL_PREFIX="$USER_PREFIX"
  else
    err "Install failed even into a user-writable prefix (see the npm log above)."
    echo "    Last resort: sudo npm install -g $PKG" >&2
    exit 1
  fi
fi

echo
# The npm bin dir may not be on PATH in this shell (fresh install, or the custom prefix
# from the fallback above), so resolve the installed binary directly.
if [ -n "$INSTALL_PREFIX" ]; then
  BIN_DIR="$INSTALL_PREFIX/bin"
else
  BIN_DIR="$(npm prefix -g)/bin"
fi
SPINYIELD_BIN="$BIN_DIR/spinyield"
if [ ! -x "$SPINYIELD_BIN" ]; then
  SPINYIELD_BIN="spinyield"
fi

# If we installed into a custom prefix, its bin dir won't be on PATH. Wire it into the
# user's shell rc files (for future shells) and into this run, so `spinyield` just works.
if [ -n "$INSTALL_PREFIX" ]; then
  case ":${PATH:-}:" in
    *":$BIN_DIR:"*) ;;
    *)
      export PATH="$BIN_DIR:$PATH"
      _rc_added=""
      for _rc in "$HOME/.zshrc" "$HOME/.bashrc" "$HOME/.profile"; do
        if [ -f "$_rc" ] && ! grep -qs "$BIN_DIR" "$_rc"; then
          if printf '\n# Added by the spinyield installer\nexport PATH="%s:$PATH"\n' "$BIN_DIR" >> "$_rc" 2>/dev/null; then
            _rc_added="$_rc_added $_rc"
          fi
        fi
      done
      if [ -n "$_rc_added" ]; then
        info "Added $BIN_DIR to PATH in:$_rc_added — restart your shell later to pick it up."
      else
        info "Add $BIN_DIR to your PATH to run 'spinyield' in new shells."
      fi
      ;;
  esac
fi

if [ -t 0 ]; then
  info "Starting setup…"
  "$SPINYIELD_BIN" setup || true
elif [ -e /dev/tty ]; then
  # curl | sh: stdin is the pipe, so hand the wizard the real terminal.
  info "Starting setup…"
  "$SPINYIELD_BIN" setup </dev/tty || true
else
  info "Installed. Next:"
  echo "    spinyield setup      # guided setup (sign in, wire status line, migrate from kickbacks)"
fi
