#!/bin/bash

# 删除空目录脚本
# 用法: ./delete_empty_dirs.sh [基础目录]
#
# 扫描 android、ios、windows 下的所有子目录，找出空目录并删除

# 显示用法信息
show_usage() {
    echo "用法: $0 [基础目录]"
    echo ""
    echo "扫描 android、ios、windows 下的所有子目录，找出空目录并删除"
    echo ""
    echo "参数:"
    echo "  基础目录 - 包含 android、ios、windows 的根目录，默认为当前目录"
    echo ""
    echo "示例:"
    echo "  $0              # 使用当前目录"
    echo "  $0 /path/to/dir # 指定基础目录"
    echo ""
}

# 检查帮助参数
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
    show_usage
    exit 0
fi

# 设置参数
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
BASE_DIR="${1:-.}"

# 检查基础目录是否存在
if [ ! -d "$BASE_DIR" ]; then
    echo "错误: 基础目录不存在: $BASE_DIR"
    exit 1
fi

# 转换为绝对路径
BASE_DIR="$(cd "$BASE_DIR" && pwd)"

echo "=========================================="
echo "空目录删除工具"
echo "=========================================="
echo "基础目录: $BASE_DIR"
echo ""

# 定义平台目录
PLATFORMS=("android" "ios" "windows")

# 存储空目录列表
declare -a EMPTY_DIRS=()

echo "正在扫描空目录..."
echo ""

# 遍历每个平台
for platform in "${PLATFORMS[@]}"; do
    platform_dir="$BASE_DIR/$platform"
    
    if [ ! -d "$platform_dir" ]; then
        echo "  [跳过] 平台目录不存在: $platform"
        continue
    fi
    
    # 遍历区域目录 (如 eu_hgh, zlong_qa 等)
    for region_dir in "$platform_dir"/*/; do
        if [ ! -d "$region_dir" ]; then
            continue
        fi
        
        region_name=$(basename "$region_dir")
        
        # 遍历版本目录 (如 1.0, 1.1 等)
        for version_dir in "$region_dir"*/; do
            if [ ! -d "$version_dir" ]; then
                continue
            fi
            
            version_name=$(basename "$version_dir")
            
            # 快速检查：只看目录下有没有任何内容（文件或子目录）
            # 使用 ls -A 检查，如果为空则没有输出
            if [ -z "$(ls -A "$version_dir" 2>/dev/null)" ]; then
                relative_path="$platform/$region_name/$version_name"
                EMPTY_DIRS+=("$relative_path")
                echo "  [空目录] $relative_path"
            fi
        done
        
        # 检查区域目录本身是否为空（没有版本子目录）
        if [ -z "$(ls -A "$region_dir" 2>/dev/null)" ]; then
            relative_path="$platform/$region_name"
            EMPTY_DIRS+=("$relative_path")
            echo "  [空目录] $relative_path (无版本子目录)"
        fi
    done
done

echo ""
echo "=========================================="
echo "扫描结果"
echo "=========================================="
echo ""

if [ ${#EMPTY_DIRS[@]} -eq 0 ]; then
    echo "没有找到空目录"
    exit 0
fi

echo "找到 ${#EMPTY_DIRS[@]} 个空目录:"
echo ""

# 显示空目录列表
printf "%-4s | %s\n" "序号" "目录路径"
echo "----------------------------------------"

for i in "${!EMPTY_DIRS[@]}"; do
    printf "%-4s | %s\n" "$((i+1))" "${EMPTY_DIRS[$i]}"
done

echo ""

# 二次确认
echo "=========================================="
echo "⚠️  警告: 删除操作不可恢复！"
echo "=========================================="
echo ""
read -p "确认删除以上 ${#EMPTY_DIRS[@]} 个空目录吗？请输入 'yes' 确认: " confirm

if [ "$confirm" != "yes" ]; then
    echo ""
    echo "操作已取消"
    exit 0
fi

echo ""
echo "开始删除..."
echo ""

# 执行删除
SUCCESS_COUNT=0
FAIL_COUNT=0

for i in "${!EMPTY_DIRS[@]}"; do
    dir="${EMPTY_DIRS[$i]}"
    full_path="$BASE_DIR/$dir"
    
    echo "  删除 [$((i+1))/${#EMPTY_DIRS[@]}]: $dir"
    
    # 执行删除
    if rm -rf "$full_path" 2>/dev/null; then
        echo "    ✓ 删除成功"
        SUCCESS_COUNT=$((SUCCESS_COUNT + 1))
    else
        echo "    ✗ 删除失败"
        FAIL_COUNT=$((FAIL_COUNT + 1))
    fi
done

echo ""
echo "=========================================="
echo "删除完成"
echo "=========================================="
echo ""
echo "成功删除: $SUCCESS_COUNT 个空目录"
echo "删除失败: $FAIL_COUNT 个空目录"
echo ""
