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

PORT="${PORT:-8550}"

# Ensure we're running from the project root.
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$ROOT_DIR"

echo "Starting atlas2-mcp on port ${PORT}..."

mkdir -p logs

LOG_FILE="logs/server.log"
PID_FILE="logs/server.pid"

# Detach (background) and persist PID + logs.
nohup uvicorn app.main:app \
  --host 0.0.0.0 \
  --port "${PORT}" \
  --env-file .env \
  >"${LOG_FILE}" 2>&1 &

echo $! >"${PID_FILE}"

echo "atlas2-mcp started (pid $(cat "${PID_FILE}")), logging to ${LOG_FILE}."
