#!/bin/bash
set -euo pipefail

IMAGE_NAME="evalscope"
CONTAINER_NAME="evalscope"
SRC_DIR="evalscope.0.17.0"

echo "📍 当前目录: $(pwd)"

# ======== 1. 停止并删除旧容器 ========
if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
    echo "🛑 停止旧容器：$CONTAINER_NAME"
    docker stop "$CONTAINER_NAME" || true
    echo "🧹 删除旧容器：$CONTAINER_NAME"
    docker rm "$CONTAINER_NAME"
fi

# ======== 2. 删除旧镜像 ========
if docker images -a | grep -q "^${IMAGE_NAME}"; then
    echo "🗑️ 删除旧镜像：$IMAGE_NAME"
    docker rmi -f "$IMAGE_NAME"
fi

# ======== 3. Git 拉取最新代码（当前目录是 git 仓库） ========
if [ -d ".git" ]; then
    echo "🔄 拉取当前目录最新源码"
    git pull
else
    echo "❌ 错误：当前目录不是 git 仓库"
    exit 1
fi

# ======== 4. 构建镜像 ========
echo "🔧 开始构建镜像：$IMAGE_NAME"
docker build \
  --build-arg http_proxy=http://127.0.0.1:7890 \
  --build-arg https_proxy=http://127.0.0.1:7890 \
  --network=host \
  -t "$IMAGE_NAME" .

# ======== 5. 运行容器 ========
echo "🚀 启动容器：$CONTAINER_NAME"
docker run --rm \
  -p 7900:7900 -p 7901:7901 \
  -v "/data/evalscope/datasets:/root/.cache/modelscope/hub/datasets" \
  --name "$CONTAINER_NAME" \
  "$IMAGE_NAME"

