#!/usr/bin/env bash
set -euo pipefail

ROOT="$(cd "$(dirname "$0")" && pwd)"
VENV="${ROOT}/.venv"
HOST="${OCR_HOST:-0.0.0.0}"
PORT="${OCR_PORT:-23567}"

export OCR_USE_GPU="${OCR_USE_GPU:-true}"
export OCR_HOST="${HOST}"
export OCR_PORT="${PORT}"

if [[ ! -d "${VENV}" ]]; then
  echo "未找到虚拟环境，请先在本目录运行: bash install.sh"
  exit 1
fi

# shellcheck disable=SC1091
source "${VENV}/bin/activate"
cd "${ROOT}"

echo "==> 启动 OCR Worker: http://${HOST}:${PORT}  (GPU=${OCR_USE_GPU})"
echo "    按 Ctrl+C 停止。后台运行请用: sudo bash install-service.sh"
echo ""

exec uvicorn app:app --host "${HOST}" --port "${PORT}" --log-level info
