#!/usr/bin/env bash
# PM2 / 手动启动入口:加载 manual_trading_hub/.env 后运行 hub.py
set -e
set -o pipefail

HUB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
REPO_ROOT="$(cd "${HUB_DIR}/.." && pwd)"
export PYTHONPATH="${REPO_ROOT}${PYTHONPATH:+:${PYTHONPATH}}"
cd "${HUB_DIR}"

# shellcheck source=lib_load_dotenv.sh
source "${HUB_DIR}/scripts/lib_load_dotenv.sh"

VENV_PY="${HUB_DIR}/.venv/bin/python"
if [[ ! -x "${VENV_PY}" ]]; then
  echo "未找到 ${VENV_PY},请先: python3 -m venv .venv && pip install -r requirements.txt" >&2
  exit 1
fi

if [[ -f "${HUB_DIR}/.env" ]]; then
  load_dotenv_file "${HUB_DIR}/.env" || {
    echo "错误: ${HUB_DIR}/.env 加载失败" >&2
    exit 1
  }
fi

echo "run_hub: python=${VENV_PY} cwd=${HUB_DIR} PYTHONPATH=${PYTHONPATH}" >&2
exec "${VENV_PY}" -u "${HUB_DIR}/hub.py"
