#!/bin/bash
# vegas-ws-api online installer.
#
# Usage:
#   curl -sSL https://ws.api.littlebearz.com/install.sh | bash
#   curl -sSL https://ws.api.littlebearz.com/install.sh | bash -s -- --key wsak-...
#   curl -sSL https://ws.api.littlebearz.com/install.sh | VEGAS_WS_KEY=wsak-... bash
#
# What it does:
#   1. Downloads the vegas-ws client to ~/.local/bin (or $INSTALL_BIN)
#   2. Stores your API key at ~/.ws_api_key_vegas (mode 600)
#   3. Tests the connection
#
# Requirements: bash, curl, python3. No root needed.
set -euo pipefail

WS_HOST="${VEGAS_WS_HOST:-ws.api.littlebearz.com}"
BASE_URL="https://${WS_HOST}"
INSTALL_BIN="${INSTALL_BIN:-$HOME/.local/bin}"
KEY_FILE="${VEGAS_WS_KEY_FILE:-$HOME/.ws_api_key_vegas}"
API_KEY="${VEGAS_WS_KEY:-}"

# Parse --key flag
while [[ $# -gt 0 ]]; do
    case "$1" in
        --key) API_KEY="$2"; shift 2 ;;
        --key=*) API_KEY="${1#--key=}"; shift ;;
        --host) WS_HOST="$2"; BASE_URL="https://${WS_HOST}"; shift 2 ;;
        --host=*) WS_HOST="${1#--host=}"; BASE_URL="https://${WS_HOST}"; shift ;;
        --bin-dir) INSTALL_BIN="$2"; shift 2 ;;
        -h|--help)
            echo "Usage: install.sh [--key wsak-...] [--host ws.api.littlebearz.com] [--bin-dir DIR]"
            exit 0 ;;
        *) echo "Unknown option: $1" >&2; exit 2 ;;
    esac
done

echo "== vegas-ws-api installer =="
echo "Server: ${BASE_URL}"
echo "Install dir: ${INSTALL_BIN}"
echo

# 1. Check requirements
command -v curl >/dev/null || { echo "ERROR: curl not found" >&2; exit 1; }
command -v python3 >/dev/null || { echo "ERROR: python3 not found" >&2; exit 1; }
python3 -c "import ssl" 2>/dev/null || { echo "ERROR: python3 ssl module missing" >&2; exit 1; }

# 2. Download client
mkdir -p "${INSTALL_BIN}"
echo "[1/3] Downloading vegas-ws client..."
curl -fsSL "${BASE_URL}/vegas-ws" -o "${INSTALL_BIN}/vegas-ws"
chmod 755 "${INSTALL_BIN}/vegas-ws"
echo "      -> ${INSTALL_BIN}/vegas-ws"

# Also install the vegas wrapper if available
if curl -fsSL "${BASE_URL}/vegas" -o "${INSTALL_BIN}/vegas" 2>/dev/null; then
    chmod 755 "${INSTALL_BIN}/vegas"
    echo "      -> ${INSTALL_BIN}/vegas (wrapper with SSH fallback)"
fi

# 3. API key
if [[ -z "${API_KEY}" ]]; then
    if [[ -t 0 ]]; then
        echo
        echo "[2/3] API key setup"
        echo "      Your API key starts with 'wsak-'. Get it from the server admin."
        read -rsp "      Enter API key: " API_KEY
        echo
    else
        echo "ERROR: no API key provided. Re-run with:" >&2
        echo "  curl -sSL ${BASE_URL}/install.sh | VEGAS_WS_KEY=wsak-... bash" >&2
        echo "  or: bash -s -- --key wsak-..." >&2
        exit 1
    fi
fi
if [[ "${API_KEY}" != wsak-* ]]; then
    echo "WARNING: key does not start with 'wsak-' — continuing anyway" >&2
fi
printf '%s\n' "${API_KEY}" > "${KEY_FILE}"
chmod 600 "${KEY_FILE}"
echo "      -> key saved to ${KEY_FILE} (mode 600)"

# 4. Test
echo
echo "[3/3] Testing connection..."
if "${INSTALL_BIN}/vegas-ws" --key-file "${KEY_FILE}" "echo CONNECTION_OK" 2>&1 | grep -q CONNECTION_OK; then
    echo "      SUCCESS: WebSocket API is reachable"
else
    echo "      FAILED: could not connect. Check your key and network." >&2
    exit 1
fi

echo
echo "Done. Add to your PATH if needed:"
echo "  export PATH=\"${INSTALL_BIN}:\$PATH\""
echo
echo "Usage:"
echo "  vegas-ws \"uptime\"              # direct WebSocket client"
echo "  vegas \"df -h\"                  # wrapper (WebSocket, SSH fallback)"
