@echo off
:: Complete ZTOPMICRO driver cleanup
:: Based on your working method
setlocal enabledelayedexpansion
:: Check if already admin
net session >nul 2>&1
if %errorlevel% equ 0 (
    echo Already running as administrator
    goto :main
)

:: Create VBScript to elevate
set "vbs=%temp%\getadmin.vbs"
echo Set UAC = CreateObject^("Shell.Application"^) > "%vbs%"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%vbs%"

:: Execute elevation
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs" >nul 2>&1
exit /b

:main
echo ========================================
echo ZTOPMICRO Complete Driver Cleanup
echo ========================================
echo.

:: Step 1: Remove from Device Manager
echo STEP 1: Removing ZTOPMICRO adapters from Device Manager...
powershell -Command "Get-PnpDevice | Where-Object {$_.FriendlyName -like 'USB 802.11AX Wireless Adapter'} | ForEach-Object { Write-Host 'Removing adapter:' $_.FriendlyName; pnputil /remove-device $_.InstanceId }" 2>nul
echo.
:: Step 2: Run uninstaller
echo STEP 2: Running uninstaller...
if exist "C:\Program Files (x86)\ZTOPMICRO\uninstall.exe" (
    echo Found uninstaller, running...
    "C:\Program Files (x86)\ZTOPMICRO\uninstall.exe"
    echo Uninstaller completed.
) else (
    echo Uninstaller not found, skipping to step 2...
)
:: Wait for completion
timeout /t 3 /nobreak >nul

echo Cleaning up directory...
if exist "C:\Program Files (x86)\ZTOPMICRO" (
    rmdir /S /Q "C:\Program Files (x86)\ZTOPMICRO"
    if exist "C:\Program Files (x86)\ZTOPMICRO" (
        echo Some files remain - may need restart
    ) else (
        echo Directory removed successfully!
    )
)
echo.

echo ========================================
echo ZTOPMICRO Auto Load Driver
echo ========================================
echo.

:step1
echo STEP 1: Finding ISO files...
for %%f in ("%~dp0*.iso") do set "iso=%%~ff" & goto :step2
echo No ISO found
pause
exit /b 1

:step2
echo.
echo STEP 2: Found ISO - !iso!
echo STEP 3: Mounting ISO...
::call :mount_iso "%iso%"
set "drive="
for /f %%d in ('powershell -Command "try { $m = Mount-DiskImage -ImagePath '!iso!' -PassThru; $v = Get-Volume -DiskImage $m; if($v){ Write-Host $v.DriveLetter; exit 0 } } catch { exit 1 }"') do (
    set "drive=%%d"
	echo Mounted to !drive!
	goto :step4
)
if not defined drive (
    echo Mount failed
    pause
    exit /b 1
)

:step4
echo.
echo STEP 4: Checking for auto_load.exe...
if exist "!drive!:\auto_load.exe" (
    echo Found auto_load.exe - executing...
    "!drive!:\auto_load.exe"
    echo auto_load.exe finished
) else (
    echo auto_load.exe not found
    dir /b "!drive!:\"
    echo Opening ISO folder...
    explorer "!drive!:\"
)
goto :step5

:step5
echo.
echo STEP 5: Ready to eject ISO
powershell -Command "Dismount-DiskImage -ImagePath '%iso%'"
echo ISO ejected successfully!

echo.
echo ========================================
echo completed!
echo ========================================
pause
exit /b 0