#!/usr/bin/env bash
# 容器运行时检测脚本
# 在执行strict模式测试前检测Docker/Podman是否可用
# 如果不可用，给出明确的修复指令

set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
CHECK_DOCKER="${1:-false}"
CHECK_PODMAN="${2:-false}"

echo "===== 容器运行时检测 ====="
echo ""

# 如果未指定检测选项，默认检测两者
if [[ "${CHECK_DOCKER}" == "false" && "${CHECK_PODMAN}" == "false" ]]; then
    CHECK_DOCKER="true"
    CHECK_PODMAN="true"
fi

DOCKER_AVAILABLE="false"
PODMAN_AVAILABLE="false"

# 检测Docker
if [[ "${CHECK_DOCKER}" == "true" ]]; then
    echo "检测 Docker..."
    if command -v docker >/dev/null 2>&1; then
        if docker info >/dev/null 2>&1; then
            echo "  ✓ Docker 已安装且运行中"
            DOCKER_AVAILABLE="true"
        else
            echo "  ✗ Docker 已安装但无法连接（可能需要启动Docker daemon）"
            echo "    解决方案: sudo systemctl start docker"
        fi
    else
        echo "  ✗ Docker 未安装"
    fi
fi

# 检测Podman
if [[ "${CHECK_PODMAN}" == "true" ]]; then
    echo ""
    echo "检测 Podman..."
    if command -v podman >/dev/null 2>&1; then
        if podman info >/dev/null 2>&1; then
            echo "  ✓ Podman 已安装且运行中"
            PODMAN_AVAILABLE="true"
        else
            echo "  ✗ Podman 已安装但无法连接"
            echo "    解决方案: podman system start"
        fi
    else
        echo "  ✗ Podman 未安装"
    fi
fi

# 检测Docker Socket
echo ""
echo "检测 Docker Socket..."
DOCKER_SOCK="/var/run/docker.sock"
if [[ -S "${DOCKER_SOCK}" ]]; then
    echo "  ✓ ${DOCKER_SOCK} 存在"
else
    echo "  ✗ ${DOCKER_SOCK} 不存在或不是socket"
fi

# 检测Podman Socket
echo ""
echo "检测 Podman Socket..."
for uid in $(id -u) 0 1000; do
    PODMAN_SOCK="/run/user/${uid}/podman/podman.sock"
    if [[ -S "${PODMAN_SOCK}" ]]; then
        echo "  ✓ ${PODMAN_SOCK} 存在"
        PODMAN_AVAILABLE="true"
        break
    fi
done
if [[ "${PODMAN_AVAILABLE}" != "true" ]]; then
    echo "  ✗ 未找到可用的Podman socket"
fi

# 最终结论
echo ""
echo "===== 检测结果 ====="
if [[ "${DOCKER_AVAILABLE}" == "true" || "${PODMAN_AVAILABLE}" == "true" ]]; then
    echo "✓ 容器运行时可用"
    if [[ "${DOCKER_AVAILABLE}" == "true" ]]; then
        echo "  - Docker: 可用"
        export DOCKER_HOST="unix://${DOCKER_SOCK}"
    fi
    if [[ "${PODMAN_AVAILABLE}" == "true" ]]; then
        echo "  - Podman: 可用"
        export DOCKER_HOST="unix:///run/user/$(id -u)/podman/podman.sock"
    fi
    echo ""
    echo "可以执行 strict 模式测试"
    exit 0
else
    echo "✗ 无可用的容器运行时"
    echo ""
    echo "===== 修复指令 ====="
    echo "严格模式测试需要Docker或Podman来启动PostgreSQL容器。"
    echo ""
    echo "方案1: 使用Docker"
    echo "  1. 安装Docker: sudo apt install docker.io"
    echo "  2. 启动Docker daemon: sudo systemctl start docker"
    echo "  3. 将当前用户加入docker组: sudo usermod -aG docker \$(whoami)"
    echo "  4. 重新登录或执行: newgrp docker"
    echo ""
    echo "方案2: 使用Podman（推荐，无需root）"
    echo "  1. 安装Podman: sudo apt install podman"
    echo "  2. 启动Podman服务: podman system start"
    echo ""
    echo "方案3: 跳过strict模式测试"
    echo "  使用普通Maven测试命令，不带 -Dmigration.test.strict=true"
    echo "  注意：这将导致关键安全测试被跳过，不推荐用于生产环境"
    echo ""
    exit 1
fi