set TMPDIR1=/data/local/tmp/Han.GJZS
set dir=%TMPDIR1%/files
set versionCode=13
set maskVC=1
:: Number of files + 1
set nof=7
set wu=files\was_unzip.log
set BBBIN=files\lib\armeabi-v7a\libbusybox.so
set BBBIN_x86=files\lib\x86\libbusybox.so
set log1=%TMPDIR1%/log/was_installed.log
set log2=%TMPDIR1%/log/busybox_installed.log
set cs=adb shell sh %dir%/test.sh >nul 2>nul

%cs%
if not "%errorlevel%"=="0" (
    echo - 该功能会向手机目录%TMPDIR1%推送一些配置文件，是否愿意？
    echo.
    echo - 0.回到主菜单     回车和任意内容继续
    echo.
    echo.
    set gn=
    set /p gn=%t2%
    if "%gn%"=="0" ( main )
    adb shell rm -rf %TMPDIR1% >nul 2>nul
    adb shell mkdir -p %TMPDIR1%/log %dir%
)

if not exist %wu% (
    echo - 开始从Magisk.apk中提取出busybox
    goto bb
) else (
    set userVC=
    for /f "tokens=1 delims=" %%i in (%wu%) do ( set userVC=%%i & goto next )


:next
    if not "%userVC%"=="%maskVC% " (
        echo - 开始提取busybox
        goto bb
    )
)
goto check


:bb
    busybox echo %maskVC% >%wu%
    busybox unzip -o "%magisk_file%" lib/x86/libbusybox.so lib/armeabi-v7a/libbusybox.so -d files >nul
    for %%i in (%BBBIN% %BBBIN_x86%) do (
        if not exist %%i ( abort 提取busybox命令失败 )
    )


:check
    set num=
    for /f "delims=" %%i in ('adb shell ls %dir% 2^>nul ^| busybox wc -l') do set num=%%i

    if "%num%" LSS "%nof%" (
        echo - 未检测到数据文件
        goto send_files
    ) else (
        adb shell sh %dir%/ctf.sh %log1%
        if "%errorlevel%"=="0" (
            set num=
            for /f "delims=" %%i in ('adb shell cat %log1%') do set num=%%i
                if !num! GEQ !versionCode! (
                    echo - 已发送不再重复发送，浪费设备存储读写
                    goto sfbb
                ) else (
                    echo - 更新资源文件中……
                    goto send_files
                )
        )
    )
    exit /B 0


:send_files
    echo - 开始发送文件……
    adb push files\push\. %dir%
    %cs%
    if "%errorlevel%"=="0" (
        echo - 推送资源文件成功。
        adb shell echo %versionCode% ^>%log1%
    ) else (
        abort 推送资源文件失败
    )


:sfbb
        adb shell sh %dir%/ctf.sh %log2%
        if "%errorlevel%"=="0" (
            set num=
            for /f "delims=" %%i in ('adb shell cat %log2%') do set num=%%i
                if "!num!" GEQ "!maskVC!" (
                    echo - busybox和Magisk.zip已发送不再重复发送，浪费设备存储读写
                    goto end
                )
        )

        echo - 发送busybox和Magisk.zip
        adb push %magisk_file% %dir%/Magisk.zip
        adb push %BBBIN% %dir%/busybox
        adb push %BBBIN_x86% %dir%/busybox_x86
        adb shell chmod 700 %dir%/busybox*
    
        echo - 测试busybox是否可用
        adb shell %dir%/busybox_x86 >nul 2>nul && adb shell mv -f %dir%/busybox_x86 %dir%/busybox || adb shell rm -rf %dir%/busybox_x86
        adb shell %dir%/busybox >nul || adb shell rm -rf %dir%/busybox
        echo - 查看推送结果：
        adb shell ls -l %dir%
        echo.
        echo.
        adb shell sh %dir%/ctf.sh %dir%/busybox
        if not "%errorlevel%"=="0" ( abort busybox推送失败 )
        adb shell echo %maskVC% ^>%log2%
        exit /B 0


:end
    exit /B 0
