@echo off
set REG_KEY_NAME=HKLM\SOFTWARE\Sony Corporation\Installed Software\Unattend\Drivers
set REG_VALUE_NAME=LastInstallationStatus
reg delete "%REG_KEY_NAME%" /v "%REG_VALUE_NAME%" /f

set DPINST_EXE=C:\Windows\InstDrvs\DPInst.exe
if not exist "%DPINST_EXE%" call :WRITE_REG_AND_EXIT 2

xcopy "%DPINST_EXE%" "%~dp0" /cky
if %ERRORLEVEL% neq 0 call :WRITE_REG_AND_EXIT 3

start "%~dp0DPInst.exe" /wait "%~dp0DPInst.exe" /sw /se /sa /f
set RETURN=%ERRORLEVEL%
if %RETURN% equ -2147483648 set RETURN=0x80000000
set /a ZZ="(%RETURN% & 0x000000ff)"
set /a YY="(%RETURN% & 0x0000ff00) >> 8"
set /a XX="(%RETURN% & 0x00ff0000) >> 16"
set /a WW="(%RETURN% & 0xff000000) >> 24"
if %WW% lss 0 set /a WW+=256

if %ZZ% gtr 0 call :WRITE_REG_AND_EXIT 0
if %YY% gtr 0 call :WRITE_REG_AND_EXIT 0
call :WRITE_REG_AND_EXIT 1

:WRITE_REG_AND_EXIT
reg add "%REG_KEY_NAME%" /v "%REG_VALUE_NAME%" /t REG_DWORD /d %1 /f
exit %1
