#!/bin/sh

# Welcome message
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 if required packages and files are already installed
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
}

# Function to install required packages
install_packages() {
    echo "正在下载安装必要插件..."
    opkg update

    # Check and install unzip if not installed
    if ! opkg list-installed | grep -q 'unzip'; then
        opkg install unzip
        if [ $? -ne 0 ]; then
            echo "unzip 安装失败。请检查软件源是否提供所需软件或者安装符合要求的OpenWRT。"
            read -p "是否重试？(y/n): " retry
            if [ "$retry" = "y" ] || [ "$retry" = "Y" ]; then
                install_packages
            else
                exit 1
            fi
        fi
    fi

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

# Function to download and install PS4 jailbreak tools
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
    if [ $? -ne 0 ]; then
        echo "下载或解压失败。"
        read -p "是否重试？(y/n): " retry
        if [ "$retry" = "y" ] || [ "$retry" = "Y" ]; then
            install_jailbreak_tools
        else
            exit 1
        fi
    fi
}

# Check if already installed
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

# Select network interface
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"

# Select PS4 firmware version
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 "输入错误，请输入1、2、3、4或5。"
        exit 1
        ;;
esac

# Determine platform architecture
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"
        ;;
    mips)
        pppwn_executable="/etc/PPPwn/pppwn_mips"
        ;;
    *)
        echo "PS4越狱工具不支持当前平台，请更换路由器或等待新版越狱工具。"
        exit 1
        ;;
esac

# Run the jailbreak tool
echo "正在运行PS4越狱工具..."
# cd /etc/PPPwn
$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
