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

cd /home/dsyoon/workspace/tts

CONDA_BASE="/home/dsyoon/workspace/miniconda3"
source "${CONDA_BASE}/bin/activate" tts
export LD_LIBRARY_PATH="${CONDA_PREFIX}/lib:${LD_LIBRARY_PATH:-}"

PORT="${PORT:-8019}"

if lsof -ti tcp:"${PORT}" >/dev/null 2>&1; then
  echo "Stopping existing server on port ${PORT}..."
  lsof -ti tcp:"${PORT}" | xargs -r kill -9
  sleep 1
fi

PORT="${PORT}" nohup python -m uvicorn server.main:app --host 0.0.0.0 --port "${PORT}" > server.log 2>&1 &
echo "Server started (PID: $!). Logs: server.log"
