# 本脚本由　by Han | 情非得已c，编写
# 应用于Han.GJZS-Windows版上


abort() {
    echo "$@"
    sleep 3
    exit 1
}

rw() {
    local f=$GJZS/rw
    touch $f && rm -f $f && touch $f
    [[ ! -f $f ]] && abort "/data 是只读文件系统"
}

install_bb() {
    local JCe BusyBox2 userVC
    JCe=$GJZS/log/was_busybox.log
    BusyBox2=$Data_Dir/busybox

    [[ -f $JCe ]] && userVC=`cat $JCe` || userVC=0
    
    if [[ $userVC -lt $bbVC || ! -L $ELF_Path/unzip ]]; then
        echo
        echo "- 开始安装busybox环境……"
        [[ -e $ELF_Path ]] && rm -rf $ELF_Path
        mkdir -p $ELF_Path
        if "$BusyBox2" --install -s "$ELF_Path"; then
            echo "- 安装busybox成功。"
            echo "$versionCode" >$JCe
        else
            rm -rf $ELF_Path
            abort "！安装busybox失败"
        fi
    fi
}


#Han
versionCode=$1
bbVC=$2
GJZS=/data/local/tmp/Han.GJZS
dir=$GJZS/files
TMPDIR=$GJZS/tmp
File="$GJZS/boot.img"
Download_File="$dir/Magisk.zip"
Data_Dir=$GJZS/Magisk
ELF_Path=$GJZS/busybox
export PATH="$ELF_Path:$PATH"

[[ ! -f $GJZS/rw ]] && rw
[[ ! -d "$Data_Dir" ]] && mkdir $Data_Dir
[[ -f $dir/busybox ]] && mv -f $dir/busybox $Data_Dir
install_bb

[[ ! -f "$Download_File" ]] && abort "！接收Magisk.zip文件失败"
[[ ! -f "$File" ]] && abort "！接收boot.img文件失败"
. $dir/MultiFunction.sh -patched
