@echo off
REM ============================================================
REM  [WL-20261018-08] Bilong7 on-site quick diagnosis
REM  Purpose: run when the exe reports a decompression error.
REM  Rule: pure ASCII body + CRLF. zh-CN cmd parses non-ASCII
REM        as GBK and breaks, so keep English here.
REM ============================================================
setlocal EnableDelayedExpansion
chcp 65001 >nul2>&1
title Bilong7 Quick Diagnosis

set "FAIL=0"

echo ================================================
echo   Bilong7 Quick Diagnosis
echo ================================================
echo.

REM ---------- [1] System ----------
echo [1/6] System ...
for /f "tokens=2 delims==" %%V in ('wmic os get Caption /value 2^>nul') do set "OS=%%V"
for /f "tokens=2 delims==" %%V in ('wmic os get TotalVisibleMemorySize /value 2^>nul') do set "MEMKB=%%V"
for /f "tokens=2 delims==" %%V in ('wmic os get FreePhysicalMemory /value 2^>nul') do set "FREEMEMKB=%%V"
echo   OS        : %OS%
set /a MEMGB=%MEMKB% / 1024
set /a FREEMEMGB=%FREEMEMKB% / 1024
echo   RAM       : %MEMGB% GB total / %FREEMEMGB% GB free
if %FREEMEMGB% LSS 2 (
    echo   [WARN] Less than 2 GB free RAM
)
echo.

REM ---------- [2] Disk space on C: ----------
REM  THIS IS THE PRIME SUSPECT FOR "return code -3"
REM  Install needs roughly: 72 MB extract + 1.5 GB ollama + 1 GB model
echo [2/6] Disk space on C:  ^<-- prime suspect for "code -3" ^<--
set "NEEDGB=10"
for /f "tokens=2 delims==" %%V in ('wmic logicaldisk where "DeviceID='C:'" get FreeSpace /value 2^>nul') do set "FREEB=%%V"
if not defined FREEB (
    echo   [FAIL] Cannot read C: free space
    set FAIL=1
) else (
    set /a FREEGB=%FREEB% / 1073741824
    echo   C: free   : %FREEGB% GB
    echo   Required : !NEEDGB! GB or more
    if !FREEGB! LSS !NEEDGB! (
        echo.
        echo   [FAIL] DISK SPACE TOO LOW ^(this causes decompression errors^)
        echo          Free at least !NEEDGB! GB, then download the setup again.
        echo.
        set FAIL=1
    ) else (
        echo   [OK] Disk space is sufficient
    )
)
echo.

REM ---------- [3] Temp folder writable ----------
REM  PyInstaller extracts to %%TEMP%%\_MEIxxxxxx. If TEMP is broken or
REM  redirected to a network path, extraction fails with data errors.
echo [3/6] Temp folder ...
echo   TEMP      : %TEMP%
if not exist "%TEMP%" (
    echo   [FAIL] TEMP folder does not exist
    set FAIL=1
) else (
    echo test > "%TEMP%\_bq_test.tmp" 2>nul
    if exist "%TEMP%\_bq_test.tmp" (
        del "%TEMP%\_bq_test.tmp" >nul 2>&1
        echo   [OK] TEMP is writable
    ) else (
        echo   [FAIL] TEMP is NOT writable - this causes decompression errors
        set FAIL=1
    )
)
echo.

REM ---------- [4] Controlled folder / antivirus ----------
echo [4/6] Security software that may block extraction ...
set "SEC=0"
tasklist /FI "IMAGENAME eq MsMpEng.exe" 2>nul | find /I "MsMpEng.exe" >nul && (
    echo   - Windows Defender running
    set "SEC=1"
)
tasklist /FI "IMAGENAME eq 360tray.exe" 2>nul | find /I "360tray.exe" >nul && (
    echo   - 360 Security running
    set "SEC=1"
)
tasklist /FI "IMAGENAME eq Huorong.exe" 2>nul | find /I "Huorong.exe" >nul && (
    echo   - Huorong running
    set "SEC=1"
)
if %SEC% EQU 0 (echo   [OK] No obvious security tool detected) else (
    echo   [WARN] Security software may block extraction.
    echo          If disk and TEMP are fine, temporarily exit it and retry.
)
echo.

REM ---------- [5] Find the downloaded setup ----------
REM  'where /r' scans the profile recursively, so it works no matter
REM  whether the Documents folder is localized or not.
echo [5/6] Locating the downloaded setup file ...
set "FOUND="
for %%F in ('where /r "%USERPROFILE%" cilong-bilong7-setup.exe 2^>nul') do (
    if not defined FOUND set "FOUND=%%F"
)
if defined FOUND (
    echo   [OK] Found: !FOUND!
    for %%F in ("!FOUND!") do echo   Size : %%~zF bytes
    echo.
    echo   >>> Double-click that file now. Then watch for the window.
) else (
    echo   [WARN] Setup file not found under your user profile
    echo          Press Win+E, then search for:  cilong
)
echo.

REM ---------- [6] What to do next ----------
echo [6/6] What to do next ...
echo.
if %FAIL% EQU 1 (
    echo   [RESULT] PROBLEM FOUND ^- fix the [FAIL] items above first.
) else (
    echo   [RESULT] No blocking problem found on this machine.
    echo            If the error dialog still appears, it is likely the
    echo            security software. Add the whole folder of the setup
    echo            file to the whitelist, then double-click it again.
)
echo.
echo ================================================
echo   Done. Keep this window and report the [FAIL] lines.
echo ================================================
echo.
pause
endlocal