#!/bin/bash
# 修复Synology Docker自动挂载冲突问题的方案

# 1. 先启动sanguo_vnpy:v2，不挂载任何卷，只启动bash
echo "[1/6] 启动sanguo_vnpy:v2临时容器..."
docker run -d --name temp_fix_container sanguo_vnpy:v2 /bin/bash -c "sleep 600"
sleep 3

# 2. 进入容器，删除冲突的文件（如果存在）
echo "[2/6] 删除容器内冲突文件..."
docker exec temp_fix_container rm -f /usr/share/zoneinfo/Etc/UTC 2>/dev/null || true
docker exec temp_fix_container rm -f /app/requirements.txt 2>/dev/null || true
echo "[2/6] ✅ 冲突文件已删除"

# 3. 复制修复代码到容器
echo "[3/6] 复制修复代码到容器..."
docker cp /volume1/homes/cfdaily/sanguo_vnpy_scripts/final_rpc_correct.py temp_fix_container:/app/scripts/
docker cp /volume1/homes/cfdaily/sanguo_vnpy_scripts/api_for_fixed_rpc.py temp_fix_container:/app/scripts/
docker cp /volume1/homes/cfdaily/sanguo_vnpy_scripts/start_fixed_services.sh temp_fix_container:/app/scripts/
chmod +x temp_fix_container:/app/scripts/start_fixed_services.sh
echo "[3/6] ✅ 修复代码已复制"

# 4. Commit成新镜像
echo "[4/6] Commit新镜像..."
docker commit temp_fix_container sanguo_vnpy:fixed_no_conflict
echo "[4/6] ✅ 新镜像已创建: sanguo_vnpy:fixed_no_conflict"

# 5. 停止并删除临时容器
echo "[5/6] 清理临时容器..."
docker stop temp_fix_container
docker rm temp_fix_container
echo "[5/6] ✅ 临时容器已清理"

# 6. 启动新容器
echo "[6/6] 启动新容器..."
docker run -d \
  --name sanguo_vnpy \
  -p 2222:22 \
  -p 8000:8000 \
  -p 8080:8080 \
  -p 8088:8088 \
  -p 8888:8888 \
  --restart always \
  sanguo_vnpy:fixed_no_conflict \
  /bin/bash -c "cd /app/scripts && ./start_fixed_services.sh"

echo "[6/6] ✅ 新容器已启动！"
echo ""
echo "✅ 修复完成！API地址: http://192.168.2.154:8088/api/backtest/run"
