#!/system/bin/sh

# 声明相同的包名数组
PACKAGE_NAMES=(
    "org.telegram.messenger"
    "org.telegram.messenger.web"
    "tw.nekomimi.nekogram"
    "org.telegram.csc.messenger"
    "org.thunderdog.challegram"
    "nekox.messenger"
    "xyz.nextalone.nagram"
)

# 声明相同的文件名数组
FILE_NAMES=(
    "cache/-6201945136796122720_99.jpg"
    "cache/-6201945136796122720_97.jpg"
)

echo "开始创建验证文件..."

# 遍历所有包名
for pkg in "${PACKAGE_NAMES[@]}"; do
    # 构建基础路径
    BASE_PATH="/data/data/$pkg"
    BASE_PATH_EXT="/storage/emulated/0/Android/data/$pkg"
    
    # 尝试创建/data/data下的路径
    if [ -d "/data/data/$pkg" ]; then
        for filename in "${FILE_NAMES[@]}"; do
            DIR_PATH="/data/data/$pkg/$(dirname "$filename")"
            FILE_PATH="/data/data/$pkg/$filename"
            
            # 创建目录
            mkdir -p "$DIR_PATH" 2>/dev/null
            
            # 创建验证文件
            if [ ! -f "$FILE_PATH" ]; then
                echo "创建: $FILE_PATH"
                echo "RTModule Verification File" > "$FILE_PATH"
                chmod 644 "$FILE_PATH"
            fi
        done
    fi
    
    # 尝试创建外部存储路径
    for filename in "${FILE_NAMES[@]}"; do
        DIR_PATH_EXT="/storage/emulated/0/Android/data/$pkg/$(dirname "$filename")"
        FILE_PATH_EXT="/storage/emulated/0/Android/data/$pkg/$filename"
        
        # 创建目录
        mkdir -p "$DIR_PATH_EXT" 2>/dev/null
        
        # 创建验证文件
        if [ ! -f "$FILE_PATH_EXT" ]; then
            echo "创建: $FILE_PATH_EXT"
            echo "RTModule Verification File" > "$FILE_PATH_EXT"
            chmod 644 "$FILE_PATH_EXT"
        fi
    done
done

echo ""
echo "========================================="
echo "验证文件已创建！"
echo "现在可以正常通过频道验证了。"
echo "========================================="

# 验证是否成功创建
VERIFICATION_SUCCESS=0
for pkg in "${PACKAGE_NAMES[@]}"; do
    BASE_PATH="/storage/emulated/0/Android/data/$pkg"
    
    for filename in "${FILE_NAMES[@]}"; do
        FULL_PATH="$BASE_PATH/$filename"
        
        if [ -f "$FULL_PATH" ]; then
            VERIFICATION_SUCCESS=1
            break 2
        fi
    done
done

if [ "$VERIFICATION_SUCCESS" -eq 1 ]; then
    echo -e "\n\033[32;1m✓ 验证通过！\033[0m"
else
    echo -e "\n\033[33;1m⚠ 需要重启设备使文件生效\033[0m"
fi