#!/bin/bash
# 离线构建诊断脚本
# Usage: ./diagnose.sh [cache-dir]

set -e

CACHE_DIR="${1:-./build-cache}"

echo "======================================"
echo "离线构建环境诊断"
echo "======================================"
echo ""

# 1. 检查 Docker
echo "1. Docker 版本:"
docker version --format '{{.Server.Version}}' 2>/dev/null || echo "  无法获取版本"
echo ""

# 2. 检查 BuildKit
echo "2. BuildKit 状态:"
if docker buildx version > /dev/null 2>&1; then
    docker buildx version
    echo ""
    echo "可用的构建器:"
    docker buildx ls
else
    echo "  BuildKit 不可用"
fi
echo ""

# 3. 检查缓存目录
echo "3. 缓存目录检查 ($CACHE_DIR):"
if [ -d "$CACHE_DIR" ]; then
    echo "  ✓ 缓存目录存在"
    
    # 检查子目录
    for subdir in buildkit images resources; do
        if [ -d "$CACHE_DIR/$subdir" ]; then
            echo "  ✓ $subdir/ 存在"
            count=$(find "$CACHE_DIR/$subdir" -type d | wc -l)
            echo "    子目录数量: $count"
        else
            echo "  ✗ $subdir/ 不存在"
        fi
    done
else
    echo "  ✗ 缓存目录不存在"
fi
echo ""

# 4. 检查基础镜像
echo "4. 基础镜像检查:"
required_images=(
    "maven:3-eclipse-temurin-21"
    "maven:3-eclipse-temurin-8"
    "eclipse-temurin:21-jdk"
    "mysql:8"
    "node:20-alpine"
    "nginx:1.29"
    "ghcr.nju.edu.cn/astral-sh/uv:python3.11-bookworm"
    "python:3.12-slim"
    "gcr.io/distroless/nodejs20-debian12"
)

missing_images=()
for img in "${required_images[@]}"; do
    if docker inspect "$img" > /dev/null 2>&1; then
        size=$(docker images --format "{{.Size}}" "$img" | head -1)
        echo "  ✓ $img ($size)"
    else
        echo "  ✗ $img (缺失)"
        missing_images+=("$img")
    fi
done
echo ""

# 5. 检查 BuildKit 缓存
echo "5. BuildKit 缓存检查:"
if [ -d "$CACHE_DIR/buildkit" ]; then
    for cache_dir in "$CACHE_DIR/buildkit"/*-cache; do
        if [ -d "$cache_dir" ]; then
            name=$(basename "$cache_dir")
            size=$(du -sh "$cache_dir" 2>/dev/null | cut -f1)
            echo "  ✓ $name ($size)"
        fi
    done
else
    echo "  ✗ 缓存目录不存在"
fi
echo ""

# 6. 检查资源文件
echo "6. 外部资源检查:"
if [ -d "$CACHE_DIR/resources" ]; then
    if [ -f "$CACHE_DIR/resources/models/ch_ppocr_mobile_v2.0_cls_infer.tar" ]; then
        size=$(du -sh "$CACHE_DIR/resources/models/ch_ppocr_mobile_v2.0_cls_infer.tar" | cut -f1)
        echo "  ✓ PaddleOCR 模型 ($size)"
    else
        echo "  ✗ PaddleOCR 模型缺失"
    fi
    
    if [ -f "$CACHE_DIR/resources/models/zh_core_web_sm-3.8.0-py3-none-any.whl" ]; then
        size=$(du -sh "$CACHE_DIR/resources/models/zh_core_web_sm-3.8.0-py3-none-any.whl" | cut -f1)
        echo "  ✓ spaCy 模型 ($size)"
    else
        echo "  ✗ spaCy 模型缺失"
    fi
    
    if [ -d "$CACHE_DIR/resources/DataX" ]; then
        echo "  ✓ DataX 源码"
    else
        echo "  ✗ DataX 源码缺失"
    fi
    
    if [ -d "$CACHE_DIR/resources/deer-flow" ]; then
        echo "  ✓ deer-flow 源码"
    else
        echo "  ✗ deer-flow 源码缺失"
    fi
else
    echo "  ✗ 资源目录不存在"
fi
echo ""

# 7. 网络检查
echo "7. 网络检查:"
if ping -c 1 8.8.8.8 > /dev/null 2>&1; then
    echo "  ⚠ 网络可用（离线构建环境通常不需要）"
else
    echo "  ✓ 网络不可达（符合离线环境）"
fi
echo ""

# 8. 总结
echo "======================================"
echo "诊断总结"
echo "======================================"

if [ ${#missing_images[@]} -eq 0 ]; then
    echo "✓ 所有基础镜像已就绪"
else
    echo "✗ 缺少 ${#missing_images[@]} 个基础镜像:"
    printf '  - %s\n' "${missing_images[@]}"
    echo ""
    echo "修复方法:"
    if [ -f "$CACHE_DIR/images/base-images.tar" ]; then
        echo "  docker load -i $CACHE_DIR/images/base-images.tar"
    else
        echo "  请确保有网环境导出时包含所有基础镜像"
    fi
fi
