#!/bin/bash

CLIENT_COUNT=${CLIENT_COUNT:-"1"}

# 创建日志目录
mkdir -p /app/logs

# 清空日志文件
> /app/logs/client.log

# 启动实时日志监控（前台运行）
tail -f /app/logs/client.log &
TAIL_PID=$!

for ((COUNT=1; COUNT<=CLIENT_COUNT; COUNT++)); do
    echo "start $COUNT client"
    ./nexus_client 2>&1 | while IFS= read -r line; do
        echo "$(date '+%Y-%m-%d %H:%M:%S') [Client-$COUNT] $line"
    done >> /app/logs/client.log &
    sleep 2
done

wait

echo "=== 所有客户端已完成，tail进程继续运行 ==="
