#!/bin/bash

#检查是否安装curl
if [ ! -e "/usr/bin/curl" ]; then
    echo "curl 未安装，请手动上传相关文件到当前目录..."
    echo "https://r2.endercat.eu.org/rootkit/rootfs.tar.xz"
    echo "https://r2.endercat.eu.org/rootkit/proot"
    echo "https://r2.endercat.eu.org/rootkit/proot-init.sh"
    echo "https://r2.endercat.eu.org/rootkit/xz"
    #询问是否继续
    read -p "是否继续？[y/n]" isContinue
    if [ "$isContinue" == "n" ]; then
        exit
    fi
fi

#检查文件是否已经存在
if [ ! -e "proot" ]; then
    echo "正在下载 proot..."
    curl "https://r2.endercat.eu.org/rootkit/proot" -o proot
fi

if [ ! -e "rootfs.tar.xz" ]; then
    echo "正在下载 rootfs.tar.xz..."
    curl "https://r2.endercat.eu.org/rootkit/rootfs.tar.xz" -o rootfs.tar.xz
fi
if [ ! -e "proot-init.sh" ]; then
    echo "正在下载 proot-init.sh..."
    curl "https://r2.endercat.eu.org/rootkit/proot-init.sh" -o proot-init.sh
fi

echo "正在解压..."

# 检查是否安装xz
if [ ! -e "/usr/bin/xz" ]; then
    echo "正在下载 xz..."
    curl "https://r2.endercat.eu.org/rootkit/xz" -o xz
    chmod +x xz
    # 先xz解压再tar解压
    ./xz -d rootfs.tar.xz
    tar -xvf rootfs.tar -C rootfs
else
    tar -xvf rootfs.tar.xz -C rootfs
fi

chmod 777 proot
cp proot-init.sh rootfs
echo "port=$port" > rootfs/config.conf
read -p "设置root密码：" password
echo "password=$password" >> rootfs/config.conf
echo "正在配置..."
./proot -S rootfs /bin/bash /proot-init.sh

echo "正在启动..."
echo "下次启动执行./proot -S rootfs /bin/bash即可"
echo "由于proot的限制，无法使用正常systemctl，请在启动后手动运行服务"
echo "使用dropbear -p 端口号 启动ssh"
./proot -S rootfs /bin/bash
