#!/bin/bash

cd "$(dirname "$0")"

mkdir -p data uploads logs

if [ -f server.pid ]; then
    PID=$(cat server.pid)
    if ps -p $PID > /dev/null 2>&1; then
        echo "停止已运行的进程 (PID: $PID)..."
        kill $PID 2>/dev/null
        sleep 1
    fi
    rm -f server.pid
fi

echo "启动服务器..."
nohup node server.js > logs/server.log 2>&1 &
echo $! > server.pid

sleep 1

HOST=$(hostname -I 2>/dev/null | awk '{print $1}' || echo "localhost")

echo ""
echo "========================================="
echo "  服务器已启动"
echo "========================================="
echo ""
echo "访问地址:"
echo "  首页:   http://${HOST}:80"
echo "  后台:   http://${HOST}:16888"
echo ""
echo "默认账号: admin / admin123"
echo ""
echo "查看日志: tail -f logs/server.log"
echo "停止服务: ./stop.sh"
echo "========================================="
