#!/bin/bash

# =============================================
# 判官标准版 - 包名随机生成器
# 自动生成并替换APK包名
# =============================================

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
CYAN='\033[0;36m'
NC='\033[0m'

# 目录设置
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
OUTPUT_DIR="$SCRIPT_DIR/output"
TEMP_DIR="$SCRIPT_DIR/temp_package"

mkdir -p "$OUTPUT_DIR"

# 随机字符串生成
generate_random_string() {
    local length=$1
    cat /dev/urandom | tr -dc 'a-z' | fold -w $length | head -n 1
}

# 生成随机包名
generate_random_package() {
    local prefixes=("com" "org" "net" "io" "app" "cn" "me")
    local prefix=${prefixes[$RANDOM % ${#prefixes[@]}]}
    
    local mid1=$(generate_random_string 4)
    local mid2=$(generate_random_string 5)
    local suffix=$(generate_random_string 6)
    
    echo "${prefix}.${mid1}${mid2}.${suffix}"
}

# 清理
cleanup() {
    rm -rf "$TEMP_DIR" 2>/dev/null
}

# 替换包名
replace_package_name() {
    local input_apk=$1
    local new_package=$2
    
    if [ ! -f "$input_apk" ]; then
        echo -e "${RED}错误: APK不存在${NC}"
        return 1
    fi
    
    echo -e "${BLUE}========================================${NC}"
    echo -e "${BLUE}  判官标准版 - 包名随机生成器${NC}"
    echo -e "${BLUE}========================================${NC}"
    echo ""
    echo -e "输入文件: ${GREEN}$(basename $input_apk)${NC}"
    echo -e "新包名: ${GREEN}$new_package${NC}"
    echo ""
    
    cleanup
    mkdir -p "$TEMP_DIR"
    
    # 解包
    echo -e "${YELLOW}[1/4] 解包APK...${NC}"
    if command -v apktool &> /dev/null; then
        apktool d "$input_apk" -o "$TEMP_DIR/unpacked" -f 2>/dev/null
    elif [ -f "$SCRIPT_DIR/apktool.jar" ]; then
        java -jar "$SCRIPT_DIR/apktool.jar" d "$input_apk" -o "$TEMP_DIR/unpacked" -f 2>/dev/null
    else
        echo -e "${RED}错误: 未找到apktool${NC}"
        return 1
    fi
    
    if [ ! -d "$TEMP_DIR/unpacked" ]; then
        echo -e "${RED}解包失败${NC}"
        return 1
    fi
    
    # 获取原包名
    local manifest="$TEMP_DIR/unpacked/AndroidManifest.xml"
    local old_package=$(grep -oP 'package="\K[^"]+' "$manifest" | head -1)
    echo -e "  原包名: ${CYAN}$old_package${NC}"
    
    # 替换Manifest中的包名
    echo -e "${YELLOW}[2/4] 替换包名...${NC}"
    sed -i "s|package=\"$old_package\"|package=\"$new_package\"|g" "$manifest"
    
    # 替换smali中的包名路径
    local old_path=$(echo $old_package | tr '.' '/')
    local new_path=$(echo $new_package | tr '.' '/')
    
    find "$TEMP_DIR/unpacked/smali" -name "*.smali" -exec sed -i "s|$old_path|$new_path|g" {} \; 2>/dev/null
    
    # 重命名smali目录
    if [ -d "$TEMP_DIR/unpacked/smali/$old_path" ]; then
        mkdir -p "$TEMP_DIR/unpacked/smali/$(dirname $new_path)"
        mv "$TEMP_DIR/unpacked/smali/$old_path" "$TEMP_DIR/unpacked/smali/$new_path" 2>/dev/null
    fi
    
    echo -e "  ${GREEN}✓${NC} 包名替换完成"
    
    # 重打包
    echo -e "${YELLOW}[3/4] 重新打包...${NC}"
    local output_name=$(basename "$input_apk" .apk)
    local timestamp=$(date +%Y%m%d_%H%M%S)
    local output_apk="$OUTPUT_DIR/${output_name}_repackaged_${timestamp}.apk"
    
    if command -v apktool &> /dev/null; then
        apktool b "$TEMP_DIR/unpacked" -o "$output_apk" 2>/dev/null
    else
        java -jar "$SCRIPT_DIR/apktool.jar" b "$TEMP_DIR/unpacked" -o "$output_apk" 2>/dev/null
    fi
    
    if [ ! -f "$output_apk" ]; then
        echo -e "${RED}打包失败${NC}"
        return 1
    fi
    
    echo -e "  ${GREEN}✓${NC} 打包成功"
    
    # 完成
    echo -e "${YELLOW}[4/4] 处理完成!${NC}"
    echo ""
    echo -e "${BLUE}========================================${NC}"
    echo -e "${GREEN}✓ 包名替换成功!${NC}"
    echo -e "  原包名: ${CYAN}$old_package${NC}"
    echo -e "  新包名: ${CYAN}$new_package${NC}"
    echo -e "  输出: ${CYAN}$output_apk${NC}"
    echo -e "${BLUE}========================================${NC}"
    echo ""
    echo -e "${YELLOW}注意: 输出APK需要重新签名${NC}"
    
    cleanup
    return 0
}

# 主菜单
show_menu() {
    echo -e "${BLUE}========================================${NC}"
    echo -e "${BLUE}  判官标准版 - 包名随机生成器${NC}"
    echo -e "${BLUE}========================================${NC}"
    echo ""
    echo -e "  ${GREEN}1.${NC} 使用随机包名"
    echo -e "  ${GREEN}2.${NC} 使用自定义包名"
    echo -e "  ${GREEN}3.${NC} 只生成随机包名(不处理APK)"
    echo -e "  ${GREEN}0.${NC} 退出"
    echo ""
    echo -n "请选择 [0-3]: "
}

# 主函数
main() {
    while true; do
        show_menu
        read -r option
        case $option in
            1)
                echo -n "请输入APK路径: "
                read -r apk_path
                local random_pkg=$(generate_random_package)
                replace_package_name "$apk_path" "$random_pkg"
                echo -n "按回车继续..."
                read
                clear
                ;;
            2)
                echo -n "请输入APK路径: "
                read -r apk_path
                echo -n "请输入新包名: "
                read -r new_pkg
                replace_package_name "$apk_path" "$new_pkg"
                echo -n "按回车继续..."
                read
                clear
                ;;
            3)
                echo ""
                echo -e "生成5个随机包名:"
                for i in {1..5}; do
                    echo -e "  ${GREEN}$i.${NC} $(generate_random_package)"
                done
                echo ""
                echo -n "按回车继续..."
                read
                clear
                ;;
            0)
                cleanup
                echo -e "${GREEN}再见!${NC}"
                exit 0
                ;;
        esac
    done
}

main
