#!/bin/sh
set -eu

cd /app

echo "[backend-wrapper] starting taskpool server"
echo "[backend-wrapper] cwd=$(pwd)"
echo "[backend-wrapper] BH_SERVER_HOST=${BH_SERVER_HOST:-}"
echo "[backend-wrapper] BH_SERVER_PORT=${BH_SERVER_PORT:-}"
echo "[backend-wrapper] BH_DB_TYPE=${BH_DB_TYPE:-}"
echo "[backend-wrapper] BH_DB_HOST=${BH_DB_HOST:-}"
echo "[backend-wrapper] BH_DB_PORT=${BH_DB_PORT:-}"
echo "[backend-wrapper] BH_DB_USER=${BH_DB_USER:-}"
echo "[backend-wrapper] BH_DB_NAME=${BH_DB_NAME:-}"
echo "[backend-wrapper] BH_DB_PATH=${BH_DB_PATH:-}"
echo "[backend-wrapper] config.ini exists=$([ -f /app/configs/config.ini ] && echo yes || echo no)"

# 基础目录
mkdir -p /app/data /app/configs /app/envs /app/logs /app/data/scripts /app/envs/mise

# Mise 环境（挂载 ./envs 后仍可工作）
export MISE_DATA_DIR="${MISE_DATA_DIR:-/app/envs/mise}"
export MISE_CONFIG_DIR="${MISE_CONFIG_DIR:-/app/envs/mise}"
export MISE_HIDE_UPDATE_WARNING=1
export PATH="/app/envs/mise/shims:/app/envs/mise/bin:/usr/local/bin:$PATH"

if command -v mise >/dev/null 2>&1; then
  echo "[backend-wrapper] mise version: $(mise --version 2>/dev/null | head -n 1)"
else
  echo "[backend-wrapper] WARNING: mise not found in PATH"
fi

# 直接前台运行，日志打到容器 stdout/stderr
exec /app/taskpool server
