@ECHO ON
setlocal enabledelayedexpansion
echo SilentInstall_Script_INF

:MODIFY
  SET TEMPLOG="C:\Windows\INF\setupapi.dev.log"
  SET LOG="C:\SWWORK\IPGLOGS\ME_Intel.log"
  SET DriverVer="2407.6.1.0"
  SET SHWID1="PCI\VEN_8086&DEV_51E0"
  SET InstallComment="pnputil.exe -i -a heci.inf"
  SET DELAYTIME=30
REM  SET WHSTATUS=NO

REM :WSTATUS
REM IF "%WHSTATUS%"=="NO" ECHO NonWHQL > ".\DrNonWHQ.TAG"

:PROCESS
  CALL :DELLOG
  CALL :DriverInstall
  TIMEOUT /T %DELAYTIME% /NOBREAK >NUL
  GOTO DRIVER_STATUS

:DELLOG
  if not exist "C:\SWWORK\IPGLOGS\" (md "C:\SWWORK\IPGLOGS\")
  if exist %LOG% (del %LOG% /q /f)
  IF EXIST "D:\%MODTYPE%S\" (del "D:\%MODTYPE%S\SilentInstall*.bat" /s /q /f 2>nul)
  IF EXIST "D:\%MODTYPE%\" (del "D:\%MODTYPE%\SilentInstall*.bat" /s /q /f 2>nul)
  IF EXIST "C:\%MODTYPE%S\" (del "C:\%MODTYPE%S\SilentInstall*.bat" /s /q /f 2>nul)
  IF EXIST "C:\%MODTYPE%\" (del "C:\%MODTYPE%\SilentInstall*.bat" /s /q /f 2>nul)
  goto :eof

:DriverInstall
  for /f tokens^=1^ delims^=^"^  %%i in ('echo %InstallComment%') do (set EXE=%%i)
  echo %InstallComment%|find "pnputil" 1>nul 2>nul
  if !errorlevel!==0 (set DriverInstall=start /min %InstallComment:"=%&goto Install) else (echo %InstallComment%|find "devcon" 1>nul 2>nul)
  if !errorlevel!==0 (set DriverInstall=start /min %InstallComment:"=%&goto Install) else (echo %InstallComment%|find "dpinst" 1>nul 2>nul)
  if !errorlevel!==0 (set DriverInstall=start /min %InstallComment:"=%&goto Install) else (set DriverInstall=start /min %InstallComment:"=%&goto Install)
  :Install
REM  IF EXIST C:\SWWORK\CHS IF EXIST  ".\DrNonWHQ.TAG" (START /MIN CMD.EXE /C ".\ClickInstallNonWHQL-SC.vbs") 1>nul 2>nul
REM  IF NOT EXIST C:\SWWORK\CHS  IF EXIST  ".\DrNonWHQ.TAG" (START /MIN CMD.EXE /C ".\ClickInstallNonWHQL-EN.vbs") 1>nul 2>nul
  PUSHD "%~DP0MEI"
  %DriverInstall%
  TIMEOUT /T 10 /NOBREAK >NUL
  POPD

  PUSHD "%~DP0ICLS"  
  pnputil.exe -i -a iclsClient.inf
  TIMEOUT /T 10 /NOBREAK >NUL
  POPD
  
  PUSHD "%~DP0JHI"  
  pnputil.exe -i -a DAL.inf
  TIMEOUT /T 10 /NOBREAK >NUL
  POPD

  PUSHD "%~DP0WMIProvider"  
  pnputil.exe -i -a MEWMIProv.inf
  TIMEOUT /T 10 /NOBREAK >NUL
  
  POPD
  goto :eof

:DRIVER_STATUS
  SET CYCLE=0
  SET NO=1
  IF NOT DEFINED SHWID1 (GOTO CHECKLOG)
  IF /I %PROCESSOR_ARCHITECTURE% EQU x86 SET DEVCON=C:\SWWORK\devcon32.exe
  IF /I %PROCESSOR_ARCHITECTURE% EQU AMD64 SET DEVCON=C:\SWWORK\devcon64.exe
  :JUDGE
  IF %CYCLE%==4 (SET CYCLE=0 & GOTO CHECKLOG)  &::DRIVER_STATUS limit time is 2 minutes(30s*4cycle)
  SET SHWID=!SHWID%NO%!
  IF NOT DEFINED SHWID (TIMEOUT /T 30 /NOBREAK >NUL & SET /A CYCLE=%CYCLE%+1 & SET "NO=1" & GOTO JUDGE)
  for /f "delims=" %%i in ('%DEVCON% drivernodes *%SHWID%*^|find /i %DriverVer%') do (set I=%%i)
  IF DEFINED I (SET drivernodes=0) ELSE (SET drivernodes=1)
  %DEVCON% status *%SHWID%*|find /i "Microsoft"
  if !errorlevel!==1 (SET status1=0) ELSE (SET status1=1)
  %DEVCON% status *%SHWID%*|find /i "running"
  if !errorlevel!==0 (SET status2=0) ELSE (SET status2=1)
  SET /A DRIVERSTATUS=%drivernodes%+%status1%+%status2%
  IF %DRIVERSTATUS%==0 (SET CYCLE=0 & GOTO CHECKLOG) ELSE (SET /A NO=%NO%+1 & GOTO JUDGE)
  goto :eof  

:CHECKLOG
  IF NOT EXIST %LOG% (echo ResultCode=0 >>%LOG% & GOTO TASKKILL)
  :LOGVERIFY
  for /f "tokens=1* delims=\" %%i in ('echo %TEMPLOG:"=%') do (
  if "%%j"=="" (goto modifiedtime) else (set LOGPATH=%%i&set LOGNAME=%%j&goto loop1)
  )
  :loop1
  for /f "tokens=1* delims=\" %%m in ("!LOGNAME!") do (
  if "%%n"=="" (goto modifiedtime) else (set LOGPATH=!LOGPATH!\%%m&set LOGNAME=%%n&goto loop1)
  )
  :modifiedtime
  IF %CYCLE%==6 (SET CYCLE=0 & GOTO CREATELOG)  &::CHECKLOG limit time is 3 minutes(30s*6cycle)
  type %TEMPLOG%>nul  &::Refresh the log file properties
  for /f "delims=" %%i in ('forfiles /p %LOGPATH% /s /m %LOGNAME% /C "cmd /c echo @fdate @ftime"') do (
  if not defined filetime (set filetime="%%i"&TIMEOUT /T 30 /NOBREAK >NUL&goto modifiedtime) else (if "%%i" NEQ !filetime! (set filetime="%%i"&TIMEOUT /T 30 /NOBREAK >NUL&SET /A CYCLE=%CYCLE%+1&goto modifiedtime))
  )
  :CREATELOG
  cmd.exe /a /c type %TEMPLOG%>>%TEMPLOG:~0,-5%_temp.log"
  cmd.exe /a /c echo. >>%TEMPLOG:~0,-5%_temp.log"
  cmd.exe /a /c echo EncodingTest >>%TEMPLOG:~0,-5%_temp.log"
  FIND /I "EncodingTest" %TEMPLOG:~0,-5%_temp.log"
  if !errorlevel!==0 (set code=a) else (set code=u)
  del %TEMPLOG:~0,-5%_temp.log" /q /f
  cmd.exe /a /c type %TEMPLOG%>>%TEMPLOG:~0,-5%_temp.log"
  cmd.exe /!code! /c echo. >>%TEMPLOG:~0,-5%_temp.log"
  FIND /I "ResultCode=0" %TEMPLOG:~0,-5%_temp.log"
  if !errorlevel! NEQ 0 (cmd.exe /!code! /c echo ResultCode=0 >>%TEMPLOG:~0,-5%_temp.log")
  cmd.exe /!code! /c echo ResultCode check successfully. >>%TEMPLOG:~0,-5%_temp.log"
  CALL :Replacelog
  GOTO TASKKILL

:ReplaceLOG
  IF %CYCLE%==4 (SET CYCLE=0 & GOTO :EOF)  &::ReplaceLOG limit time is 2 minutes(30s*4cycle)
  move /Y %TEMPLOG:~0,-5%_temp.log" %LOG%
  if !errorlevel!==1 (SET /A CYCLE=%CYCLE%+1 & TIMEOUT /T 30 /NOBREAK >NUL & GOTO ReplaceLOG)
  goto :eof

:TASKKILL
  tasklist|find /i "%EXE%"
  if !errorlevel!==0 (taskkill /IM "%EXE%" /F)
  :MSIEXEC_CHECK
  IF %CYCLE%==2 (SET CYCLE=0 & taskkill /IM "msiexec.exe" /F & GOTO END)  &::MSIEXEC_CHECK limit time is 1 minutes(30s*2cycle)
  tasklist|find /i "msiexec.exe"
  if !errorlevel!==0 (SET /A CYCLE=%CYCLE%+1 & TIMEOUT /T 30 /NOBREAK >NUL & GOTO MSIEXEC_CHECK)

:END
REM IF EXIST "D:\%MODTYPE%S\" (del "D:\%MODTYPE%S\ClickInstallNonWHQL*.vbs" /s /q /f 2>nul)
REM IF EXIST "D:\%MODTYPE%\" (del "D:\%MODTYPE%\ClickInstallNonWHQL*.vbs" /s /q /f 2>nul)
REM IF EXIST "C:\%MODTYPE%S\" (del "C:\%MODTYPE%S\ClickInstallNonWHQL*.vbs" /s /q /f 2>nul)
REM IF EXIST "C:\%MODTYPE%\" (del "C:\%MODTYPE%\ClickInstallNonWHQL*.vbs" /s /q /f 2>nul)
  SET TEMPLOG=
  SET LOG=
  SET DriverVer=
  SET InstallComment=
  SET DELAYTIME=
  SET EXE=
  SET DEVCON=
  SET CYCLE=
  SET NO=
  SET SHWID=
  SET SHWID1=
  SET SHWID2=
  SET SHWID3=
  SET SHWID4=
  SET SHWID5=
  SET SHWID6=
  SET SHWID7=
  SET SHWID8=
  SET SHWID9=
  SET I=
  SET drivernodes=
  SET status1=
  SET status2=
  SET DRIVERSTATUS=
  SET LOGPATH=
  SET LOGNAME=
  SET filetime=
  SET code=
  SET WHSTATUS=
  setlocal disabledelayedexpansion