#!/bin/bash
# ============================================================
# 脚本名称：change_author_to_pangu.sh
# 功能说明：将项目中所有文件的作者统一改为pangu
# 作 者：pangu
# 创建时间：2026-01-31
# 使用方法：./change_author_to_pangu.sh
# ============================================================
set -e

PROJECT_ROOT="/Users/felix/hbxhWorkSpace/pangu-user-platform"
LOG_FILE="$PROJECT_ROOT/scripts/change_author_log.txt"

echo "======================================================"
echo "  批量修改作者为pangu"
echo "======================================================"
echo ""
echo "项目根目录: $PROJECT_ROOT"
echo "日志文件: $LOG_FILE"
echo ""

# 清空日志文件
> "$LOG_FILE"

# 统计变量
total_files=0
changed_files=0

# 定义需要替换的作者模式（支持多种格式）
declare -a OLD_AUTHORS=(
    "湖北新华业务中台研发团队"
    "admin"
    "ruoyi"
    "若依"
    "RuoYi"
    "pangu-dev"
)

# 替换函数
replace_author() {
    local file=$1
    local changed=0
    
    # 备份原文件
    cp "$file" "$file.bak.author"
    
    # 替换 @author
    if grep -q "@author" "$file" 2>/dev/null; then
        for old_author in "${OLD_AUTHORS[@]}"; do
            if grep -q "@author.*$old_author" "$file" 2>/dev/null; then
                sed -i '' "s/@author.*$old_author.*/@author pangu/g" "$file"
                changed=1
                echo "  - 替换 @author $old_author -> pangu" | tee -a "$LOG_FILE"
            fi
        done
        # 处理其他 @author 格式
        sed -i '' 's/@author \([^p][^ ]*\|[^ ]\+[^ ]\)/@author pangu/g' "$file"
    fi
    
    # 替换 作者：
    if grep -q "作者：\|作 者：" "$file" 2>/dev/null; then
        for old_author in "${OLD_AUTHORS[@]}"; do
            if grep -q "作者：.*$old_author\|作 者：.*$old_author" "$file" 2>/dev/null; then
                sed -i '' "s/作者：.*$old_author.*/作者：pangu/g" "$file"
                sed -i '' "s/作 者：.*$old_author.*/作 者：pangu/g" "$file"
                changed=1
                echo "  - 替换 作者：$old_author -> pangu" | tee -a "$LOG_FILE"
            fi
        done
    fi
    
    # 替换 Author:
    if grep -q "Author:" "$file" 2>/dev/null; then
        for old_author in "${OLD_AUTHORS[@]}"; do
            if grep -q "Author:.*$old_author" "$file" 2>/dev/null; then
                sed -i '' "s/Author:.*$old_author.*/Author: pangu/g" "$file"
                changed=1
                echo "  - 替换 Author: $old_author -> pangu" | tee -a "$LOG_FILE"
            fi
        done
    fi
    
    # 如果有更改，删除备份；否则恢复
    if [ $changed -eq 1 ]; then
        rm -f "$file.bak.author"
        return 0
    else
        mv "$file.bak.author" "$file"
        return 1
    fi
}

echo "🔍 正在扫描需要修改的文件..."
echo ""

# 扫描 Java 文件
echo "📁 处理 Java 文件..."
while IFS= read -r -d '' file; do
    ((total_files++))
    if replace_author "$file"; then
        ((changed_files++))
        echo "✅ $file" | tee -a "$LOG_FILE"
    fi
done < <(find "$PROJECT_ROOT/pangu-system" "$PROJECT_ROOT/pangu-admin" "$PROJECT_ROOT/pangu-common" "$PROJECT_ROOT/pangu-framework" -type f -name "*.java" -print0 2>/dev/null)

# 扫描 Vue 文件
echo ""
echo "📁 处理 Vue 文件..."
while IFS= read -r -d '' file; do
    ((total_files++))
    if replace_author "$file"; then
        ((changed_files++))
        echo "✅ $file" | tee -a "$LOG_FILE"
    fi
done < <(find "$PROJECT_ROOT/pangu-ui/src" -type f -name "*.vue" -print0 2>/dev/null)

# 扫描 JS 文件
echo ""
echo "📁 处理 JS 文件..."
while IFS= read -r -d '' file; do
    ((total_files++))
    if replace_author "$file"; then
        ((changed_files++))
        echo "✅ $file" | tee -a "$LOG_FILE"
    fi
done < <(find "$PROJECT_ROOT/pangu-ui/src" -type f -name "*.js" -print0 2>/dev/null)

# 扫描 SQL 文件
echo ""
echo "📁 处理 SQL 文件..."
while IFS= read -r -d '' file; do
    # 跳过备份文件
    if [[ "$file" == *.bak* ]] || [[ "$file" == *.backup* ]]; then
        continue
    fi
    ((total_files++))
    if replace_author "$file"; then
        ((changed_files++))
        echo "✅ $file" | tee -a "$LOG_FILE"
    fi
done < <(find "$PROJECT_ROOT/sql" -type f \( -name "*.sql" -o -name "*.sh" \) -print0 2>/dev/null)

# 扫描 Markdown 文档
echo ""
echo "📁 处理 Markdown 文档..."
while IFS= read -r -d '' file; do
    ((total_files++))
    if replace_author "$file"; then
        ((changed_files++))
        echo "✅ $file" | tee -a "$LOG_FILE"
    fi
done < <(find "$PROJECT_ROOT/docs" -type f -name "*.md" -print0 2>/dev/null)

# 处理 README
if [ -f "$PROJECT_ROOT/README.md" ]; then
    ((total_files++))
    if replace_author "$PROJECT_ROOT/README.md"; then
        ((changed_files++))
        echo "✅ $PROJECT_ROOT/README.md" | tee -a "$LOG_FILE"
    fi
fi

echo ""
echo "======================================================"
echo "  ✅ 作者修改完成"
echo "======================================================"
echo ""
echo "📊 统计信息："
echo "  - 扫描文件总数: $total_files"
echo "  - 修改文件数量: $changed_files"
echo "  - 未修改文件数: $((total_files - changed_files))"
echo ""
echo "📄 详细日志: $LOG_FILE"
echo ""
echo "提示：所有修改已完成，请使用 git diff 检查变更"
echo ""
