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


TMPDIR0="$TMPDIR"
[[ ! -x /system/bin/dalvikvm ]] && abort "！/system/bin/dalvikvm命令不存在"

Extracting_files() {        
    log=$Data_Dir/Decompress_Magisk_versionCode.log
    MAGISKBIN=$Data_Dir
    APK=$MAGISKBIN/magisk.apk
    [[ -f $log ]] && user_version=`cat $log` || user_version=0
    [[ $KEEPVERITY = 1 ]] && export KEEPVERITY=true || export KEEPVERITY=false
    [[ $KEEPFORCEENCRYPT = 1 ]] && export KEEPFORCEENCRYPT=true || export KEEPFORCEENCRYPT=false
    [[ $RECOVERYMODE = 1 ]] && export RECOVERYMODE=true || export RECOVERYMODE=false
    
        if [[ $user_version -lt $versionCode || ! -f $APK ]]; then
            echo "- 开始解压zip文件"
            echo $versionCode >$log
            BBBIN=$TMPDIR/busybox
            INSTALLER=$TMPDIR/install
            COMMONDIR=$INSTALLER/assets
            CHROMEDIR=$INSTALLER/assets/chromeos
            
            rm -rf $TMPDIR
            mkdir -p $TMPDIR
            
            unzip -o "$Download_File" lib/x86/libbusybox.so lib/armeabi-v7a/libbusybox.so -d $TMPDIR &>/dev/null
            chmod -R 755 $TMPDIR/lib
            mv -f $TMPDIR/lib/x86/libbusybox.so $BBBIN
            $BBBIN >/dev/null 2>&1 || mv -f $TMPDIR/lib/armeabi-v7a/libbusybox.so $BBBIN
            rm -rf $TMPDIR/lib
            mkdir -p $INSTALLER
            unzip -o "$Download_File" "assets/*" "lib/*" "META-INF/com/google/*" -x "lib/*/libbusybox.so" -d $INSTALLER &>/dev/null
            
            if [ ! -f $COMMONDIR/util_functions.sh ]; then
                ls -l $INSTALLER
                abort "！读取文件异常，请私信我修复"
            fi
            
            sed -i -r '/^TMPDIR=\/dev\/tmp$/d' $COMMONDIR/util_functions.sh
            . $COMMONDIR/util_functions.sh
            api_level_arch_detect
            ui_print "- 设备架构：$ARCH"
            BINDIR=$INSTALLER/lib/$ARCH32
            [ ! -d "$BINDIR" ] && BINDIR=$INSTALLER/lib/armeabi-v7a
            cd $BINDIR
            for file in lib*.so; do mv -f "$file" "${file:3:${#file}-6}"; done
            chmod -R 755 $CHROMEDIR $BINDIR

            MAGISKBIN=$Data_Dir
            
            rm -rf $MAGISKBIN 2>/dev/null
            mkdir -p $MAGISKBIN 2>/dev/null
            cp -af $BINDIR/. $COMMONDIR/. $BBBIN $MAGISKBIN
            $IS64BIT && cp -f $MAGISKBIN/magisk64 $MAGISKBIN/magisk || cp -f $MAGISKBIN/magisk32 $MAGISKBIN/magisk
            cp -f "$Download_File" $APK
            chmod -R 755 $MAGISKBIN
            rm -rf "$TMPDIR"
        else
            echo "- 资源文件已存在不在重复解压"
        fi
            cd $MAGISKBIN
            . ./util_functions.sh
            MAGISKBIN=$Data_Dir
            TMPDIR="$TMPDIR0"
}

boot_verify() {
    if [[ ! -c "$1" ]]; then
        echo -n "- "
        eval $BOOTSIGNER -verify < "$1" 2>&1 && BOOTSIGNED=true
        $BOOTSIGNED && ui_print "- Boot image is signed with AVB 1.0"
    fi
}

boot_sign() {
    [[ "$1" = -sign ]] && BOOTSIGNED=true && shift
    echo "- Generate new boot image"
    flash_image "$1" "$2"
        case $? in
        1)
            abort "! 分区大小不足"
        ;;
        2)
            abort "! $1 是只读的"
        ;;
        esac
        
}

XiuBu_boot() {
    boot_verify "$1"
    SOURCEDMODE=true
    sh ./boot_patch.sh "$1" 2>/dev/null
    [[ $? -ne 0 ]] && abort -e "！不支持的boot.img镜像文件\n已被其它未知程序修补过boot，请找到原始boot.img镜像进行操作。"
    boot_sign ./new-boot.img "$2"
    ./magiskboot cleanup &>/dev/null
    rm -f ./*.img
}



    if [[ -z "$File" ]]; then
        abort "请选择文件路径不可为空哦﻿⊙∀⊙！"
    elif [[ ! -f "$File" ]]; then
        abort "\"${File}\"文件不存在无法使用本功能！"
    fi
        Extracting_files
        BOOTSIGNED=false
        
        case $1 in
        -patched)
            patched="$GJZS/magisk_patched.img"
            rm -rf $patched &>/dev/null
            XiuBu_boot "$File" "$patched"
            [[ -f "$patched" ]] && echo -e "- 完成\n\n文件输出路径：$patched"
        ;;
        
            
        *)
            abort "！未找到服务"
        ;;
        esac
true
