#!/bin/bash

# 路径检查脚本
# 用于验证所有必要的构建目录是否存在

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TEST_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"
cd "$TEST_DIR"

echo "=========================================="
echo "  路径检查脚本"
echo "=========================================="
echo ""
echo "当前脚本目录: $SCRIPT_DIR"
echo "当前工作目录: $(pwd)"
echo ""

# 检查配置文件
echo "检查配置文件..."
if [ -f "$TEST_DIR/docker-compose.yml" ]; then
    echo "  ✓ docker-compose.yml 存在"
else
    echo "  ✗ docker-compose.yml 不存在"
fi

if [ -f "$TEST_DIR/.env" ]; then
    echo "  ✓ .env 存在"
elif [ -f "$TEST_DIR/env.example" ]; then
    echo "  ⚠ .env 不存在，但 env.example 存在"
else
    echo "  ✗ .env 和 env.example 都不存在"
fi
echo ""

# 检查构建目录
echo "检查构建目录..."
BUILD_DIRS=(
    "../ftp/build"
    "../prometheus/build"
    "../grafana/build"
)

all_exist=true
for dir in "${BUILD_DIRS[@]}"; do
    full_path="$SCRIPT_DIR/$dir"
    if [ -d "$full_path" ]; then
        echo "  ✓ $dir"
        echo "    完整路径: $full_path"
    else
        echo "  ✗ $dir 不存在"
        echo "    查找路径: $full_path"
        all_exist=false
    fi
done
echo ""

# 检查 Dockerfile
echo "检查 Dockerfile..."
DOCKERFILES=(
    "../ftp/build/Dockerfile"
    "../prometheus/build/Dockerfile"
    "../grafana/build/Dockerfile"
)

for dockerfile in "${DOCKERFILES[@]}"; do
    full_path="$SCRIPT_DIR/$dockerfile"
    if [ -f "$full_path" ]; then
        echo "  ✓ $dockerfile"
    else
        echo "  ✗ $dockerfile 不存在"
        echo "    查找路径: $full_path"
        all_exist=false
    fi
done
echo ""

# 检查数据目录（可选）
if [ -f "$SCRIPT_DIR/.env" ]; then
    source "$SCRIPT_DIR/.env"
    DATA_ROOT=${DATA_ROOT:-./data}
    
    echo "检查数据目录..."
    echo "  数据根目录: $DATA_ROOT"
    
    if [ -d "$SCRIPT_DIR/$DATA_ROOT" ]; then
        echo "  ✓ 数据目录存在"
        ls -la "$SCRIPT_DIR/$DATA_ROOT" | head -10
    else
        echo "  ⚠ 数据目录不存在（首次运行时会自动创建）"
    fi
    echo ""
fi

# 总结
echo "=========================================="
if $all_exist; then
    echo "  ✓ 所有必要的文件和目录都存在"
    echo "  可以运行 ./start-all.sh 启动服务"
else
    echo "  ✗ 部分文件或目录缺失"
    echo "  请检查项目结构是否完整"
fi
echo "=========================================="
echo ""

