#!/bin/bash
set -euo pipefail

# ---------------------------------------------------------------------------
# start-hybrid-server.sh
# Start the docling-fast hybrid FastAPI server (port 5002) in the background.
# ---------------------------------------------------------------------------

PORT=5002
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SERVER_SCRIPT="${SCRIPT_DIR}/hybrid_server.py"
PID_FILE="${SCRIPT_DIR}/hybrid-server.pid"
LOG_DIR="/home/jay/workspace/logs"
LOG_FILE="${LOG_DIR}/hybrid-server.log"
JAVA_HOME="/home/jay/.local/jvm/jdk-11.0.2"
PYTHONPATH_EXTRA="/home/jay/.local/lib/python3.12/site-packages"

# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
info()  { echo "[INFO]  $*"; }
warn()  { echo "[WARN]  $*" >&2; }
error() { echo "[ERROR] $*" >&2; }

# ---------------------------------------------------------------------------
# 1. Ensure log directory exists
# ---------------------------------------------------------------------------
if [[ ! -d "${LOG_DIR}" ]]; then
    info "Creating log directory: ${LOG_DIR}"
    mkdir -p "${LOG_DIR}"
fi

# ---------------------------------------------------------------------------
# 2. Check port availability
# ---------------------------------------------------------------------------
if ss -tlnp 2>/dev/null | grep -q ":${PORT} " || \
   lsof -iTCP:"${PORT}" -sTCP:LISTEN -t 2>/dev/null | grep -q .; then
    warn "Port ${PORT} is already in use."
    warn "Stop the occupying process before starting the hybrid server."
    exit 1
fi

# ---------------------------------------------------------------------------
# 3. GPU detection
# ---------------------------------------------------------------------------
if command -v nvidia-smi &>/dev/null; then
    info "GPU detected (nvidia-smi found). Running in GPU mode."
else
    info "nvidia-smi not found. Running in CPU mode."
fi

# ---------------------------------------------------------------------------
# 4. Export environment
# ---------------------------------------------------------------------------
export JAVA_HOME="${JAVA_HOME}"
export PATH="${JAVA_HOME}/bin:${PATH}"
export PYTHONPATH="${PYTHONPATH_EXTRA}${PYTHONPATH:+:${PYTHONPATH}}"

info "JAVA_HOME : ${JAVA_HOME}"
info "PYTHONPATH: ${PYTHONPATH}"

# ---------------------------------------------------------------------------
# 5. Launch server with nohup
# ---------------------------------------------------------------------------
info "Starting hybrid server on port ${PORT} …"
info "Log file : ${LOG_FILE}"
info "PID file : ${PID_FILE}"

# Rotate: keep previous log as .1 backup
if [[ -f "${LOG_FILE}" ]]; then
    mv "${LOG_FILE}" "${LOG_FILE}.1"
fi

# cd to script dir so uvicorn can locate the hybrid_server module
cd "${SCRIPT_DIR}"
nohup /home/jay/.local/bin/uvicorn \
    hybrid_server:app \
    --host 0.0.0.0 \
    --port "${PORT}" \
    --log-level info \
    > "${LOG_FILE}" 2>&1 &

SERVER_PID=$!
echo "${SERVER_PID}" > "${PID_FILE}"

info "Server started with PID ${SERVER_PID}."
info "Waiting up to 15 seconds for the server to become ready …"
READY=0
for _i in $(seq 1 15); do
    sleep 1
    if curl -sf "http://127.0.0.1:${PORT}/health" >/dev/null 2>&1; then
        READY=1
        info "Server became ready after ${_i}s."
        break
    fi
done
unset _i

# ---------------------------------------------------------------------------
# 6. Health check
# ---------------------------------------------------------------------------
if [[ "${READY}" -eq 1 ]]; then
    info "Health check passed: http://127.0.0.1:${PORT}/health"
    curl -s "http://127.0.0.1:${PORT}/health"
    echo
else
    warn "Health check failed after 15 s. The server may still be initialising."
    warn "Check the log for details: ${LOG_FILE}"
    warn "Last 20 lines of log:"
    tail -n 20 "${LOG_FILE}" >&2 || true
    warn "Server process (PID ${SERVER_PID}) is still running. Retry: curl http://127.0.0.1:${PORT}/health"
    # Do not exit 1 – the process is alive; model loading may take longer
fi
