@ECHO OFF

cd /d %~dp0

set countcomp=0

:YPCSOLO
if exist "C:\Windows\System32\DriverStore\FileRepository\yamahaaedriver_component.inf_amd64_*" (
	@echo yamahaaedriver_component.inf_amd64_ Exists.
	goto :END_SUCCESS
)
@echo yamahaaedriver_component.inf_amd64_ Not Exists.
set /a countcomp+=1
if %countcomp% gtr 5 (
	goto :RETRYALL
)
@echo YamahaAEDriver Package Component
devcon update "./YamahaAEDriver Package Component/YamahaAEDriver_component.inf" SWC\YamahaAEDriver_ComponentID
timeout /t 5 > nul
goto :YPCSOLO

:RETRYALL

@ECHO ON

@echo Uninstall

@echo YamahaAEDriver Package Component
devcon remove "./YamahaAEDriver Package Component/YamahaAEDriver_component.inf" SWC\YamahaAEDriver_ComponentID

@echo YamahaAEDriver Package Extension
devcon remove "./YamahaAEDriver Package Extension/YamahaAEDriver_extension.inf" ROOT\YamahaAEDriver

@echo YamahaAEDriver Package Base
devcon remove "./YamahaAEDriver Package Base/YamahaAEDriver.inf" ROOT\YamahaAEDriver

@echo pnputil-d oem*.inf
RemoveOem -r
if %ERRORLEVEL% equ 0 (
	echo 削除に成功しました。
) else (
	echo 削除に失敗しました。
)

@echo Install
@echo YamahaAEDriver Package Base
devcon install "./YamahaAEDriver Package Base/YamahaAEDriver.inf" ROOT\YamahaAEDriver

timeout /t 5 > nul

@echo YamahaAEDriver Package Extension
devcon update "./YamahaAEDriver Package Extension/YamahaAEDriver_extension.inf" ROOT\YamahaAEDriver

timeout /t 5 > nul

@ECHO OFF
set count=1

:WAITBEFOREYPC
@echo Wait before installing Component
for /f "usebackq delims=" %%A in (`devcon /status root\yamahaaedriver ^| findstr "Driver is running."`) do set HOGE=%%A
if "%HOGE%"=="    Driver is running." (
	echo yamahaaedriver is running.
	goto :YPCINST
) else (
	echo yamahaaedriver is not running.
	set /a count+=1
	if %count% gtr 11 (
		goto :YPCINST
	)
	timeout /t 5 > nul
	goto :WAITBEFOREYPC
)

:YPCINST

@echo YamahaAEDriver Package Component

devcon update "./YamahaAEDriver Package Component/YamahaAEDriver_component.inf" SWC\YamahaAEDriver_ComponentID
timeout /t 5 > nul
if exist "C:\Windows\System32\DriverStore\FileRepository\yamahaaedriver_component.inf_amd64_*" (
	echo yamahaaedriver_component.inf_amd64_ Exists.
) else (
	echo yamahaaedriver_component.inf_amd64_ Not Exist.
)

:FINALCHECK

@echo Done

@ECHO OFF
if exist "C:\Windows\System32\DriverStore\FileRepository\yamahaaedriver_component.inf_amd64_*" (
	goto :END_SUCCESS
) else (
	goto :END_FAIL
)

:END_SUCCESS
exit /b 0

:END_FAIL
exit /b 2
