@echo off
title NEWTECH SHOP - TEST HAO PIN ^& DO DONG AN NGUON (15-30 PHUT)
color 0B

"%SystemRoot%\System32\fltmc.exe" >nul 2>&1
if %errorLevel% equ 0 goto :ADMIN_OK
"%SystemRoot%\System32\net.exe" session >nul 2>&1
if %errorLevel% equ 0 goto :ADMIN_OK

echo [*] Dang yeu cau quyen Administrator (UAC)...
"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -Command "try { Start-Process -FilePath '%SystemRoot%\System32\cmd.exe' -ArgumentList '/k \"\"%~f0\"\"' -Verb RunAs -ErrorAction Stop; exit 0 } catch { exit 1 }" >nul 2>&1
if %errorLevel% equ 0 exit /b

if exist "%~dp0Core_Engine\--elevate.ps1" (
    "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -File "%~dp0Core_Engine\--elevate.ps1" "%~f0"
    exit /b
)

:ADMIN_OK
set "USB_ROOT=%~dp0"
if "%USB_ROOT:~-1%"=="\" set "USB_ROOT=%USB_ROOT:~0,-1%"
set "PS_EXE=%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe"
if not exist "%PS_EXE%" set "PS_EXE=powershell.exe"
set "LOCAL_PS=%TEMP%\NewTech_Step15_BatteryDrainTest.ps1"

cd /d "%TEMP%"
cls

if exist "%USB_ROOT%\Core_Engine\Step15-BatteryDrainTest.ps1" (
    copy /Y "%USB_ROOT%\Core_Engine\Step15-BatteryDrainTest.ps1" "%LOCAL_PS%" >nul
) else (
    echo [*] Dang tai module Test Hao Pin moi nhat tu Server NewTech...
    curl.exe -fL --retry 2 --connect-timeout 15 -o "%LOCAL_PS%" "https://hotro.newtechshop.vn/downloads/super_usb/Step15-BatteryDrainTest.ps1" >nul 2>&1
    if not exist "%LOCAL_PS%" (
        "%PS_EXE%" -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (New-Object System.Net.WebClient).DownloadFile('https://hotro.newtechshop.vn/downloads/super_usb/Step15-BatteryDrainTest.ps1', '%LOCAL_PS%')"
    )
)

if not exist "%LOCAL_PS%" (
    echo [!] Khong tim thay hoac khong tai duoc Step15-BatteryDrainTest.ps1!
    echo [!] Vui long kiem tra ket noi mang hoac thu lai.
    pause
    exit /b 1
)

"%PS_EXE%" -NoProfile -ExecutionPolicy Bypass -File "%LOCAL_PS%" -UsbRootPath "%USB_ROOT%"
pause
