#!/bin/sh
# Ticker installer — rent your Claude Code status line for ads and earn.
#   curl -fsSL https://ticker.outerscope.xyz/install.sh | sh
set -e

BASE="${TICKER_API_BASE:-https://ticker.outerscope.xyz}"
TICKER_DIR="$HOME/.ticker"

printf '\033[38;5;42m◈ ticker\033[0m installer\n\n'

# --- require Node >= 18 ---
if ! command -v node >/dev/null 2>&1; then
  echo "Ticker needs Node.js (>= 18). Install it from https://nodejs.org and re-run." >&2
  exit 1
fi
NODE_MAJOR=$(node -p "process.versions.node.split('.')[0]" 2>/dev/null || echo 0)
if [ "${NODE_MAJOR:-0}" -lt 18 ]; then
  echo "Ticker needs Node.js >= 18 (found $(node -v))." >&2
  exit 1
fi

# --- download the self-contained CLI ---
mkdir -p "$TICKER_DIR"
echo "Downloading Ticker..."
curl -fsSL "$BASE/cli/status.mjs" -o "$TICKER_DIR/status.mjs"
curl -fsSL "$BASE/cli/ticker.mjs" -o "$TICKER_DIR/ticker.mjs"

# --- install a `ticker` launcher on PATH ---
BIN=""
for d in /usr/local/bin "$HOME/.local/bin"; do
  if [ -d "$d" ] && [ -w "$d" ]; then BIN="$d"; break; fi
done
[ -z "$BIN" ] && { mkdir -p "$HOME/.local/bin"; BIN="$HOME/.local/bin"; }
cat > "$BIN/ticker" <<EOF
#!/bin/sh
exec node "$TICKER_DIR/ticker.mjs" "\$@"
EOF
chmod +x "$BIN/ticker"

# --- register + wire the status line ---
TICKER_API_BASE="$BASE" node "$TICKER_DIR/ticker.mjs" init

# --- PATH hint ---
case ":$PATH:" in
  *":$BIN:"*) : ;;
  *) printf '\nAdd %s to your PATH to use the "ticker" command, or run: node ~/.ticker/ticker.mjs <cmd>\n' "$BIN" ;;
esac
