#!/bin/bash

# 路径配置测试脚本
# 用于验证部署目录结构和路径配置是否正确

set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

print_message() {
    local color=$1
    local message=$2
    echo -e "${color}${message}${NC}"
}

print_message $BLUE "QAUP 部署路径配置测试"
print_message $BLUE "======================"
echo ""

print_message $BLUE "当前目录: $SCRIPT_DIR"
print_message $BLUE "项目根目录: $PROJECT_ROOT"
echo ""

# 检查关键文件和目录
print_message $BLUE "检查关键文件和目录..."

# 检查项目根目录文件
files_to_check=(
    "$PROJECT_ROOT/pom.xml"
    "$PROJECT_ROOT/qaup-admin/pom.xml"
    "$PROJECT_ROOT/qaup-ui/package.json"
)

for file in "${files_to_check[@]}"; do
    if [ -f "$file" ]; then
        print_message $GREEN "✓ $file"
    else
        print_message $RED "✗ $file (缺失)"
    fi
done

# 检查部署目录文件
deploy_files_to_check=(
    "$SCRIPT_DIR/deploy.sh"
    "$SCRIPT_DIR/docker-compose.yml"
    "$SCRIPT_DIR/Dockerfile"
    "$SCRIPT_DIR/docker/config-manager.sh"
    "$SCRIPT_DIR/docker/nginx/Dockerfile"
)

for file in "${deploy_files_to_check[@]}"; do
    if [ -f "$file" ]; then
        print_message $GREEN "✓ $file"
    else
        print_message $RED "✗ $file (缺失)"
    fi
done

echo ""
print_message $BLUE "检查 Docker Compose 构建上下文..."

# 验证 Docker Compose 配置
if grep -q "context: \.\." "$SCRIPT_DIR/docker-compose.yml"; then
    print_message $GREEN "✓ Docker Compose 构建上下文配置正确"
else
    print_message $RED "✗ Docker Compose 构建上下文配置错误"
fi

if grep -q "dockerfile: deploy/Dockerfile" "$SCRIPT_DIR/docker-compose.yml"; then
    print_message $GREEN "✓ Dockerfile 路径配置正确"
else
    print_message $RED "✗ Dockerfile 路径配置错误"
fi

echo ""
print_message $BLUE "检查 Dockerfile 路径引用..."

# 检查 Dockerfile 中的路径
if grep -q "COPY deploy/docker/start.sh" "$SCRIPT_DIR/Dockerfile"; then
    print_message $GREEN "✓ Dockerfile 启动脚本路径正确"
else
    print_message $RED "✗ Dockerfile 启动脚本路径错误"
fi

echo ""
print_message $GREEN "路径配置检查完成！"
print_message $YELLOW "如果发现错误，请根据提示修复后重新测试"