#!/bin/sh

# set -e

# 欢迎信息
echo "欢迎使用PS4破解脚本，本脚本利用运行OpenWRT的软路由闲置资源自动破解PS4，漏洞来自PPPwn，payload是GoldHEN 2.4b17.3，当前支持PS4固件9.00、9.60和11.00的越狱。by ntbowen"
echo "在开始前请确保以下几点："
echo "1. 路由器和PS4通过网线连接"
echo "2. 下载 https://pcat.qsim.top/PPPwn/goldhen.bin 保存到U盘根目录，U盘插入PS4（仅限首次越狱，以后U盘就不用了，除非GoldHEN有更新）"
echo "3. PS4的网络设置为有线连接、定制、pppoe（账户密码随意）、DNS自动、MTU自动、不使用Proxy服务器"
read -p "是否继续？(y/n): " continue_script
if [ "$continue_script" != "y" ] && [ "$continue_script" != "Y" ]; then
    echo "退出脚本。"
    exit 0
fi

# 检查是否已安装必需的软件包和文件
check_installation() {
    if opkg list-installed | grep -q 'unzip' && opkg list-installed | grep -q 'libpcap' &&
       [ -f /etc/PPPwn/pppwn_aarch64 ] && [ -f /etc/PPPwn/pppwn_x86_64 ] && [ -f /etc/PPPwn/pppwn_mipsel ] &&
       [ -f /etc/PPPwn/stage1_9.00.bin ] && [ -f /etc/PPPwn/stage1_9.60.bin ] && [ -f /etc/PPPwn/stage1_10.00.bin ] && [ -f /etc/PPPwn/stage1_10.01.bin ] && [ -f /etc/PPPwn/stage1_11.00.bin ] &&
       [ -f /etc/PPPwn/stage2_9.00.bin ] && [ -f /etc/PPPwn/stage2_9.60.bin ] && [ -f /etc/PPPwn/stage2_10.00.bin ] && [ -f /etc/PPPwn/stage2_10.01.bin ] && [ -f /etc/PPPwn/stage2_11.00.bin ]; then
        return 0
    else
        return 1
    fi
}

# 安装必需的软件包
install_packages() {
    echo "正在下载安装必要插件..."
    opkg update

    for package in unzip libpcap; do
        if ! opkg list-installed | grep -q "$package"; then
            opkg install "$package" || {
                echo "$package 安装失败。请检查软件源是否提供所需软件或者安装符合要求的OpenWRT。"
                read -p "是否重试？(y/n): " retry
                if [ "$retry" = "y" ] || [ "$retry" = "Y" ]; then
                    install_packages
                else
                    exit 1
                fi
            }
        fi
    done
}

# 下载并安装PS4越狱工具
install_jailbreak_tools() {
    echo "正在下载安装PS4越狱工具..."
    wget https://pcat.qsim.top/PPPwn/PPPwn.zip -O /tmp/PPPwn.zip && unzip -o /tmp/PPPwn.zip -d /etc/PPPwn || {
        echo "下载或解压失败。"
        read -p "是否重试？(y/n): " retry
        if [ "$retry" = "y" ] || [ "$retry" = "Y" ]; then
            install_jailbreak_tools
        else
            exit 1
        fi
    }
}

# 加载之前保存的设置（如果有）
load_saved_settings() {
    if [ -f /etc/PPPwn/settings.conf ]; then
        . /etc/PPPwn/settings.conf
        echo "检测到之前保存的设置："
        echo "网络接口: eth$interface_num"
        echo "PS4固件版本: $saved_fw_version"
        read -p "是否按照之前的设置直接越狱？(y/n): " use_saved_settings
        if [ "$use_saved_settings" = "y" ] || [ "$use_saved_settings" = "Y" ]; then
            interface="$saved_interface"
            fw_version="$saved_fw_version"
            return 0
        fi
    fi
    return 1
}

# 保存当前设置
save_settings() {
    echo "保存当前设置..."
    echo "saved_interface=$interface_num" > /etc/PPPwn/settings.conf
    echo "saved_fw_version=$fw_version" >> /etc/PPPwn/settings.conf
}

# 检查是否已经安装
if check_installation; then
    echo "你似乎安装过PS4越狱工具。"
    read -p "是否升级或重新安装PS4越狱工具？(y/n): " reinstall
    if [ "$reinstall" != "y" ] && [ "$reinstall" != "Y" ]; then
        read -p "接下来是否正式开始PS4越狱？(y/n): " start_jailbreak
        if [ "$start_jailbreak" != "y" ] && [ "$start_jailbreak" != "Y" ]; then
            echo "退出脚本。"
            exit 0
        fi
    else
        install_packages
        install_jailbreak_tools
    fi
else
    read -p "是否安装PS4越狱工具？(y/n): " install
    if [ "$install" != "y" ] && [ "$install" != "Y" ]; then
        echo "退出脚本。"
        exit 0
    else
        install_packages
        install_jailbreak_tools
    fi
fi

# 加载之前保存的设置（如果有）并且用户同意
if ! load_saved_settings; then
    # 选择网络接口
    read -p "请选择和PS4连接的网口（0-9）： " interface_num
    if ! echo "$interface_num" | grep -qE '^[0-9]$'; then
        echo "输入错误，请输入0到9之间的数字。"
        exit 1
    fi
    interface="eth$interface_num"

    # 选择PS4固件版本
    echo "请选择PS4的固件版本：1为9.00, 2为9.60, 3为10.00, 4为10.01, 5为11.00"
    read -p "请输入版本号（1、2、3、4或5）： " fw_version
    case $fw_version in
        1)
            fw_code="900"
            stage1_file="stage1_9.00.bin"
            stage2_file="stage2_9.00.bin"
            ;;
        2)
            fw_code="960"
            stage1_file="stage1_9.60.bin"
            stage2_file="stage2_9.60.bin"
            ;;
        3)
            fw_code="1000"
            stage1_file="stage1_10.00.bin"
            stage2_file="stage2_10.00.bin"
            ;;
        4)
            fw_code="1001"
            stage1_file="stage1_10.01.bin"
            stage2_file="stage2_10.01.bin"
            ;;
        5)
            fw_code="1100"
            stage1_file="stage1_11.00.bin"
            stage2_file="stage2_11.00.bin"
            ;;
        *)
            echo "无效的版本号。"
            exit 1
            ;;
    esac

    # 保存当前设置
    save_settings
fi

# 确定平台架构
arch=$(uname -m)
case $arch in
    x86_64)
        pppwn_executable="/etc/PPPwn/pppwn_x86_64"
        ;;
    aarch64)
        pppwn_executable="/etc/PPPwn/pppwn_aarch64"
        ;;
    mipsel)
        pppwn_executable="/etc/PPPwn/pppwn_mipsel"
        ;;
    *)
        echo "不支持的平台架构：$arch"
        exit 1
        ;;
esac

# 运行越狱工具
echo "正在运行PS4越狱工具..."
$pppwn_executable -i $interface --fw $fw_code --stage1 "/etc/PPPwn/$stage1_file" --stage2 "/etc/PPPwn/$stage2_file" -a
if [ $? -eq 0 ]; then
    echo "PS4越狱工具运行成功。"
else
    echo "PS4越狱工具运行失败，请检查配置。"
fi
