#!/bin/bash
# hzhub-portal-dealer 启动脚本

cd "$(dirname "$0")"

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

mkdir -p logs

if [ -f "$PID_FILE" ]; then
    PID=$(cat "$PID_FILE")
    if ps -p "$PID" > /dev/null 2>&1; then
        echo "Service already running (PID: $PID)"
        exit 1
    else
        rm -f "$PID_FILE"
    fi
fi

echo "Starting hzhub-portal-dealer..."
nohup pnpm dev > "$LOG_FILE" 2>&1 &
PID=$!

sleep 2
if ps -p "$PID" > /dev/null 2>&1; then
    echo "$PID" > "$PID_FILE"
    echo "hzhub-portal-dealer started (PID: $PID)"
    echo "Port: http://localhost:5138"
else
    echo "Failed to start. Check logs: $LOG_FILE"
    exit 1
fi
