@echo off
setlocal enabledelayedexpansion

:: Help message
if "%~1"=="" (
    goto :showHelp
)
if /I "%~1"=="/?" (
    goto :showHelp
)
if /I "%~1"=="-h" (
    goto :showHelp
)
if /I "%~1"=="--help" (
    goto :showHelp
)

pnputil /add-driver %1 /install
if not %ERRORLEVEL%==0 if not %ERRORLEVEL%==3010 (
    ECHO Failed to install the driver.
    ECHO Please, make sure you have administrator privileges.
    goto :eof
)

if /I not "%~2"=="-R" (
    echo Optional flag -R not present. BIOS update is triggered on next reboot.
    goto :eof
)
if "%~3"=="" (
    goto :showHelp
)
:: Set the countdown time in seconds
set COUNTDOWN=%~3

echo.
echo Platform is rebooting in %COUNTDOWN% seconds...
echo.

:: Countdown loop
for /L %%i in (%COUNTDOWN%,-1,1) do (
    echo Rebooting in %%i...
    timeout /t 1 >nul
)

:: Reboot the system
shutdown /r /t 0
goto :eof

:showHelp
echo.
echo Automated capsule update script
echo ----------------------
echo.
echo arg0: BIOS Driver (.inf) file path to install as the new BIOS image.
echo         NOTE: ALL the other files available in the "Windows" folder should be 
echo               in the same path as well!!!
echo arg1: (optional) Set this to -R to automatically trigger REBOOT after installing the Driver
echo arg2: (only with "-R") Set this to a number: this is the number of seconds platform waits before rebooting
echo.
echo Examples:
echo.
echo   update.bat seco_AmstonLake_E89_bios_9AADA203_1-06-11_20260113_Evaluation-only.inf
echo     Install the driver, but do not trigger the BIOS update yet. It will be triggered on next reboot.
echo.
echo   update.bat seco_AmstonLake_E89_bios_9AADA203_1-06-11_20260113_Evaluation-only.inf -R 5
echo     Install the driver. The platform is rebooted after 5 seconds, triggering the BIOS update.
echo.
goto :eof