@echo off

echo.
echo Preparing Intel(R) Optane(TM) Memory and Storage Management Installation...
echo.

pnputil /add-driver "%~dp0\*.inf" /subdirs /install > nul

if errorlevel 3010 goto reboot
if errorlevel 259 goto nothing
if errorlevel 5 goto elevated
if errorlevel 1 goto error

:success
echo Preparation by installing necessary RST extension drivers done. 
goto HSA

:reboot
echo Preparation by installing necessary RST extension drivers done. 
echo Please reboot the system and run _DISM_INSTALL_Intel_RST.bat again.
goto end

:nothing
echo RST extension drivers already installed.

:HSA
echo.
echo Installing Intel(R) Optane(TM) Memory and Storage Management now...

setlocal EnableDelayedExpansion

FOR /R %~dp0 %%# in (*.appxbundle) DO (
    SET APPXBUNDLE=%%~nx#
)

FOR /R %~dp0 %%# in (*.xml) DO (
    SET License1XML=%%~nx#
)

dism /online /Add-ProvisionedAppxPackage /PackagePath:"%~dp0%APPXBUNDLE%" /LicensePath:"%~dp0%License1XML%" /DependencyPackagePath:"%~dp0Dependency\Microsoft.VCLibs.140.00.UWPDesktop_14.0.33728.0_x64__8wekyb3d8bbwe.appx" /region=all

echo.
echo Please reboot the system.
goto end

:elevated
echo RST extension drivers installation failed. Please run this installer from an elevated command prompt.
goto end

:error
echo Errorlevel: %errorlevel%
echo Please check driver installation in Windows Device Manager.

:end
echo.
pause
