#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$ROOT"
[[ -f .venv/.kimu-runtime-ready && -x .venv/bin/gunicorn ]] || {
  echo "KIMU runtime is not installed completely. Run ./install.sh first." >&2
  exit 1
}
set -a
[[ -f .env ]] && source .env
set +a
HOST="${KIMU_HOST:-127.0.0.1}"
PORT="${KIMU_PORT:-19460}"
WORKERS="${KIMU_WORKERS:-1}"
THREADS="${KIMU_THREADS:-16}"
if [[ "$WORKERS" != "1" ]]; then
  echo "WARNING: Conversation/chat cancellation uses process-local session state; KIMU_WORKERS=1 is required until a shared runtime broker is configured." >&2
fi
mkdir -p instance
export PYTHONPATH="$ROOT/src"
exec .venv/bin/gunicorn --chdir "$ROOT" --bind "$HOST:$PORT" --workers "$WORKERS" --threads "$THREADS" --worker-class gthread --timeout 240 --access-logfile - --error-logfile - 'kimu.wsgi:app'
