#!/system/bin/sh

RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
PURPLE='\033[1;35m'
NC='\033[0m'

echo -e "${YELLOW}================================================"
echo -e "${GREEN}                shell脚本加密工具"
echo -e "${GREEN}                    版本：2.5"
echo -e "${GREEN}            作者：哔哩哔哩@万级的麦块"
echo -e "${YELLOW}================================================${NC}"

normalize_path() {
    path=$(echo "$1" | sed 's|//*|/|g')
    case "$path" in
        /sdcard/*) echo "/storage/emulated/0/${path#/sdcard/}" ;;
        /storage/emulated/0/*|/*) echo "$path" ;;
        *) echo "$(pwd)/$path" ;;
    esac
}

check_readable() {
    [ -r "$1" ] || { echo -e "${RED}错误: 文件不可读或不存在: $1${NC}"; return 1; }
}

check_space() {
    input_size=$(du -k "$1" | cut -f1)
    available_space=$(df -k "$(dirname "$2")" | tail -1 | awk '{print $4}')
    [ "$available_space" -ge "$((input_size * 3))" ] || {
        echo -e "${RED}错误：磁盘空间不足，需要至少 $((input_size * 3))KB 可用空间${NC}"; 
        return 1; 
    }
}

octo_encrypt() {
    script_content=$(cat "$1") || return 1
    len=${#script_content}
    [ "$len" -ge 8 ] || { echo -e "${RED}错误: 脚本内容过短，至少需要8个字符${NC}"; return 1; }
    
    part_len=$(( (len + 7) / 8 ))
    
    {
        echo '#!/system/bin/sh'
        echo 'eval "$(echo "'$(echo -n "${script_content:0:$part_len}" | base64 -w 0)'" | base64 -d)\'
        echo '$(echo "'$(echo -n "${script_content:$part_len:$part_len}" | base64 -w 0)'" | base64 -d)\'
        echo '$(echo "'$(echo -n "${script_content:$((part_len * 2)):$part_len}" | base64 -w 0)'" | base64 -d)\'
        echo '$(echo "'$(echo -n "${script_content:$((part_len * 3)):$part_len}" | base64 -w 0)'" | base64 -d)\'
        echo '$(echo "'$(echo -n "${script_content:$((part_len * 4)):$part_len}" | base64 -w 0)'" | base64 -d)\'
        echo '$(echo "'$(echo -n "${script_content:$((part_len * 5)):$part_len}" | base64 -w 0)'" | base64 -d)\'
        echo '$(echo "'$(echo -n "${script_content:$((part_len * 6)):$part_len}" | base64 -w 0)'" | base64 -d)\'
        echo '$(echo "'$(echo -n "${script_content:$((part_len * 7))}" | base64 -w 0)'" | base64 -d)"'
    } > "$2"
}

encrypt_file() {
    case $encrypt_type in
        "base64")
            e1=$(base64 -w 0 "$1") || { echo -e "${RED}Base64编码失败${NC}"; return 1; }
            echo '#!/system/bin/sh
d_script=$(echo "'"$e1"'" | base64 -d)
eval "$d_script"' > "$2" ;;
            
        "octo_base64")
            octo_encrypt "$1" "$2" || return 1 ;;
            
        "binary")
            e1=$(xxd -p "$1" | tr -d '\n') || { echo -e "${RED}二进制编码失败${NC}"; return 1; }
            echo '#!/system/bin/sh
d_script=$(echo "'"$e1"'" | xxd -r -p)
eval "$d_script"' > "$2" ;;
            
        "gzip")
            e1=$(gzip -c "$1" | base64 -w 0) || { echo -e "${RED}Gzip压缩失败${NC}"; return 1; }
            echo '#!/system/bin/sh
d_script=$(echo "'"$e1"'" | base64 -d | gunzip)
eval "$d_script"' > "$2" ;;
            
        "mixed")
            e1=$(gzip -c "$1" | base64 -w 0 | xxd -p | tr -d '\n') || { echo -e "${RED}混合加密失败${NC}"; return 1; }
            echo '#!/system/bin/sh
d_script=$(echo "'"$e1"'" | xxd -r -p | base64 -d | gunzip)
eval "$d_script"' > "$2" ;;
    esac
    
    chmod 755 "$2" 2>/dev/null || echo -e "${YELLOW}警告: 无法设置执行权限${NC}"
    echo -e "${GREEN}加密完成: ${2##*/}${NC}"
}

process_single() {
    [ -z "$1" ] && {
        echo -e "${YELLOW}请输入要加密的脚本路径:"
        echo -e "${BLUE}示例: /sdcard/scripts/myscript.sh${NC}"
        while read -r p; do
            p=$(normalize_path "$p")
            check_readable "$p" && { script_path="$p"; break; }
            echo -e "${RED}错误: 文件无效或不可读"
            echo -e "${YELLOW}请重新输入:${NC}"
        done
    } || {
        script_path=$(normalize_path "$1")
        check_readable "$script_path" || exit 1
    }

    echo -e "${YELLOW}输入保存目录 (留空使用原脚本所在目录): ${NC}"
    read -r dir
    [ -z "$dir" ] && dir=$(dirname "$script_path") || dir=$(normalize_path "$dir")

    mkdir -p "$dir" || { echo -e "${RED}错误: 无法创建目录${NC}"; exit 1; }
    [ -w "$dir" ] || { echo -e "${RED}错误: 无写入权限${NC}"; exit 1; }

    default_name="${script_path##*/}"
    default_name="${default_name%.*}[加密].sh"
    
    echo -e "${YELLOW}输入文件名 (留空使用原文件名): ${NC}"
    read -r fname
    [ -z "$fname" ] && fname="$default_name"
    [ "${fname##*.}" != "sh" ] && fname="${fname%.*}.sh"

    output="${dir%/}/$fname"
    check_space "$script_path" "$output" || exit 1

    encrypt_file "$script_path" "$output" || exit 1
}

process_batch() {
    echo -e "${YELLOW}请输入文件夹路径: ${NC}"
    read -r folder_path
    folder_path=$(normalize_path "$folder_path")
    
    [ -d "$folder_path" ] || { echo -e "${RED}错误: 该路径不是文件夹${NC}"; exit 1; }
    [ -r "$folder_path" ] || { echo -e "${RED}错误: 无读取权限${NC}"; exit 1; }
    
    success=0
    fail=0
    
    echo -e "${YELLOW}正在扫描文件夹中的脚本文件...${NC}"
    while read -r file; do
        output="${file%.*}[加密].sh"
        
        if check_readable "$file" && check_space "$file" "$output"; then
            if encrypt_file "$file" "$output"; then
                success=$((success + 1))
            else
                fail=$((fail + 1))
            fi
        else
            fail=$((fail + 1))
        fi
    done < <(find "$folder_path" -maxdepth 1 -type f \( -name "*.sh" -o -name "*.bash" \))
    
    echo -e "${GREEN}批量加密完成: 成功 ${success} 个, 失败 ${fail} 个${NC}"
}

echo -e "${RED}请选择加密方式:"
echo -e "${BLUE}1. 普通Base64加密"
echo -e "${BLUE}2. 变异Base64加密"
echo -e "${BLUE}3. 二进制加密"
echo -e "${BLUE}4. Gzip加密"
echo -e "${BLUE}5. 混合加密${NC}"
echo -e "${YELLOW}请输入序号: ${NC}"

read -r choice
case $choice in
    1) encrypt_type="base64";;
    2) encrypt_type="octo_base64";;
    3) encrypt_type="binary";;
    4) encrypt_type="gzip";;
    5) encrypt_type="mixed";;
    *) echo -e "${RED}无效选择${NC}"; exit 1;;
esac

echo -e "${YELLOW}是否启用批量加密？(y/n): ${NC}"
read -r batch_mode

case "$batch_mode" in
    y|Y) process_batch;;
    *) process_single "$1";;
esac

echo -e "${GREEN}操作完成${NC}"
exit 0
