#!/bin/bash

ROOT_DIR="$(cd "$(dirname "$0")/../.." && pwd)"
SERVER_DIR="$ROOT_DIR/server"
LOG_DIR="$ROOT_DIR/logs"
LOG_FILE="$LOG_DIR/server.log"
SERVER_PORT="${SERVER_PORT:-9091}"

mkdir -p "$LOG_DIR"

pipe_to_log() {
  local source="${1:-SERVER}"
  local raw_log="${2:-}"
  local line
  while IFS= read -r line || [ -n "$line" ]; do
    if [ -n "$raw_log" ]; then
      echo "$line" >> "$raw_log"
    fi
    line=$(echo "[$source] $line" | sed 's/\x1b\[[0-9;]*[a-zA-Z]//g; s/\x1b\[[0-9;]*m//g')
    echo "$line"
  done
}

kill_old_server() {
  if command -v lsof &> /dev/null; then
    local pids
    pids=$(lsof -t -i tcp:"$SERVER_PORT" -sTCP:LISTEN 2>/dev/null || true)
    if [ -n "$pids" ]; then
      echo "正在关闭旧的 server 进程：$pids"
      kill -9 $pids 2>/dev/null || echo "关闭进程失败：$pids"
      sleep 1
    fi
  fi
}

echo "==================== Server Dev Run ===================="
echo "Server 目录：$SERVER_DIR"
echo "Server 端口：$SERVER_PORT"
echo "日志文件：$LOG_FILE"

kill_old_server

echo "启动 server 服务..."
cd "$SERVER_DIR"
NODE_ENV=development PORT="$SERVER_PORT" npx tsx ./src/index.ts 2>&1 | pipe_to_log "SERVER" "$LOG_FILE"
