@echo off
REM In a batch file, the total length of a command line can't contain more than 8191 characters.
setlocal enabledelayedexpansion 
cd /d %~dp0

if exist %SystemRoot%\System32\pnputil.exe (
	echo System32\pnputil.exe
    set "SystemPath=%SystemRoot%\System32"
) else if exist %SystemRoot%\Sysnative\pnputil.exe (
	echo Sysnative\pnputil.exe
    set "SystemPath=%SystemRoot%\Sysnative"
) else (
    echo ERROR: Cannot find pnputil.exe to install the driver.
    echo/
    pause
    goto :EOF
)

%SystemPath%\pnputil.exe /delete-driver ".\SynaAPO\AudioService.inf" /uninstall >nul 2>nul
powershell "Get-PnpDevice -FriendlyName '*audio*'|Select-Object -Property HardwareID | ft -wrap">[senary_temp].txt

REM withoutUWP, with headset mic Senary APO and PortC Senary APO
REM type [senary_temp].txt|findstr /i ""
REM if !errorlevel!==0 goto Install-label1

rem with UWP and with Senary APO
type [senary_temp].txt|findstr /i "1FA80311 1FA8021F 1FA80313 1FA8021D 1FA80302 1FA80218 1FA80216 1FA80209 1FA80205 1FA80206 1FA80207 1FA80204 1FA80210 1FA80203 "
if !errorlevel!==0 goto Install-label3

rem with UWP and without Senary APO
type [senary_temp].txt|findstr /i "1FA80223 0526A006 1FA80212 2014801D 1025203C 14F10251 1FA81722 1FA81749 1FA81743 1FA80220 1FA80221 1FA8020D 14F10411 1FA8020B 1FA8020A 14F13206 1FA8021E 1FA81307 1FA8021C 1FA8021B 1FA8021A 1C062508 14F10408 1FA80219 1FA81721 14F13202 13FE0154 14F13203 1FA80217 14F13201 1FA8030F 1FA8030B 1FA80215 1FA80214 1FA80213 1FA80201 14F10406 1FA81736 1FA80208 1FA81734 1FA80211 1FA81725 1FA8171F 14F10101"
if !errorlevel!==0 goto Install-label2

rem only install base and oem ext infs
echo %SystemPath%\pnputil.exe /add-driver "cisstrtU-ext-OEM-common.inf" /install
%SystemPath%\pnputil.exe /add-driver "cisstrtU-ext-OEM-common.inf" /install
echo %SystemPath%\pnputil.exe /add-driver "cisstrtU-base.inf" /install
%SystemPath%\pnputil.exe /add-driver "cisstrtU-base.inf" /install
goto end

:Install-label1
echo %SystemPath%\pnputil.exe /add-driver "cisstrtU-ext-OEM-common.inf" /install
%SystemPath%\pnputil.exe /add-driver "cisstrtU-ext-OEM-common.inf" /install
echo %SystemPath%\pnputil.exe /add-driver "cisstrtU-base.inf" /install
%SystemPath%\pnputil.exe /add-driver "cisstrtU-base.inf" /install
echo %SystemPath%\pnputil.exe /add-driver SenaryAPO/"SenaryAPO.inf" /install
%SystemPath%\pnputil.exe /add-driver SenaryAPO/"SenaryAPO.inf" /install
goto end

:Install-label2
echo %SystemPath%\pnputil.exe /add-driver "cisstrtU-ext-OEM-common.inf" /install
%SystemPath%\pnputil.exe /add-driver "cisstrtU-ext-OEM-common.inf" /install
echo %SystemPath%\pnputil.exe /add-driver "cisstrtU-base.inf" /install
%SystemPath%\pnputil.exe /add-driver "cisstrtU-base.inf" /install
echo %SystemPath%\pnputil.exe /add-driver ".\SynaAPO\cisstrtU-component-OEM.inf" /install
%SystemPath%\pnputil.exe /add-driver ".\SynaAPO\cisstrtU-component-OEM.inf" /install
echo %SystemPath%\pnputil.exe /add-driver ".\SynaAPO\AudioService.inf" /install
%SystemPath%\pnputil.exe /add-driver ".\SynaAPO\AudioService.inf" /install
goto end

:Install-label3
echo %SystemPath%\pnputil.exe /add-driver "cisstrtU-ext-OEM-common.inf" /install
%SystemPath%\pnputil.exe /add-driver "cisstrtU-ext-OEM-common.inf" /install
echo %SystemPath%\pnputil.exe /add-driver "cisstrtU-base.inf" /install
%SystemPath%\pnputil.exe /add-driver "cisstrtU-base.inf" /install
echo %SystemPath%\pnputil.exe /add-driver ".\SynaAPO\cisstrtU-component-OEM.inf" /install
%SystemPath%\pnputil.exe /add-driver ".\SynaAPO\cisstrtU-component-OEM.inf" /install
echo %SystemPath%\pnputil.exe /add-driver ".\SynaAPO\AudioService.inf" /install
%SystemPath%\pnputil.exe /add-driver ".\SynaAPO\AudioService.inf" /install
echo %SystemPath%\pnputil.exe /add-driver SenaryAPO/"SenaryAPO.inf" /install
%SystemPath%\pnputil.exe /add-driver SenaryAPO/"SenaryAPO.inf" /install

goto end


:end
if exist [senary_temp].txt del [senary_temp].txt
REM restart two services
net.exe stop "audioendpointbuilder" /y
net.exe start "audioendpointbuilder"
net stop audiosrv /y
net start audiosrv

echo ===============================
REM echo Reboot now? [Y/N]
REM set /p var=
REM if /i %var% == y (shutdown /r /f /t 0) else pause
