@echo OFF
echo ------------------------------------------------------------------------
echo -----------------------Installing Certificate---------------------------
::Driverquery | findstr /i "ftdibus\ftdiport\." >nul
::IF %ERRORLEVEL% == 0 (GOTO FX3) else (GOTO phy_install)

::phy_install
::echo -----Instaling FTDI Drivers -----------
::IF EXIST "%PROGRAMFILES(X86)%" (GOTO x64) ELSE (GOTO x86)

::x86
::pnputil.exe -i -a C:\GRL\GRL-USB_Drivers\Phy_Drivers\ftdiusbdriver\ftdibus.inf
::pnputil.exe -i -a C:\GRL\GRL-USB_Drivers\Phy_Drivers\ftdiusbdriver\ftdiport.inf
::pnputil.exe -i -a C:\GRL\GRL-USB_Drivers\Phy_Drivers\lscusbdriver\lwinusb.inf



:FX3
Driverquery | findstr /i "GRL USB\." > nul
::IF %ERRORLEVEL% == 0 goto Installed

::CertUtil -delstore -User -f "TrustedPublisher" "GraniteRiverLabs"
::CertUtil -delstore -User -f "MyCompanyCertStore" "GraniteRiverLabs"
::CertUtil -delstore -User -f "Trusted Root Certification Authorities" "GraniteRiverLabs"
::CertUtil -delstore "Root" "GraniteRiverLabs"

CertUtil -addstore -User -f "TrustedPublisher" C:\GRL\GRL-USB_Drivers\Certificate.cer
CertUtil -addstore -User -f "MyCompanyCertStore" C:\GRL\GRL-USB_Drivers\Certificate.cer
CertUtil -addstore -User -f "Trusted Root Certification Authorities" C:\GRL\GRL-USB_Drivers\Certificate.cer
CertUtil -addstore "Root" C:\GRL\GRL-USB_Drivers\Certificate.cer


ver | findstr /i "6\.0\." > nul
IF %ERRORLEVEL% == 0 goto ver_Vista

ver | findstr /i "6\.1\." > nul
IF %ERRORLEVEL% == 0 goto ver_Win7

ver | findstr /i "6\.2\." > nul
IF %ERRORLEVEL% == 0 goto ver_Win8

ver | findstr /i "6\.3\." > nul
IF %ERRORLEVEL% == 0 goto ver_Win81

ver | findstr /i "10\.0\." > nul
IF %ERRORLEVEL% == 0 goto ver_Win10
Goto ERR


:ver_Vista
echo --------------------	windows_vista	 ----------------------
IF EXIST "%PROGRAMFILES(X86)%" (GOTO x64) ELSE (GOTO x86)
GOTO ERRprocessor_vista
 
:x86
echo --------------------	windows_vista_x86 ---------------------
Pnputil -i -a C:\GRL\GRL-USB_Drivers\vista\x86\cyusb3.inf 
Driverquery | findstr /i "GRL USB\CYUSB3\GRl USB Bootloader\." > nul
IF %ERRORLEVEL% == 0 (goto END) else (GOTO driver_instv86)
:driver_instv86
Pnputil -a C:\GRL\GRL-USB_Drivers\vista\x86\cyusb3.inf 
find "CYUSB3" C:\Windows\System32\drivers
IF %ERRORLEVEL% == 0 (GOTO Hardware_connect) else (GOTO ERR)

:x64
echo --------------------	windows_vista _X64 ---------------------
%windir%\sysnative\pnputil.exe -i -a C:\GRL\GRL-USB_Drivers\vista\x64\cyusb3.inf
Driverquery | findstr /i "GRL USB\CYUSB3\GRl USB Bootloader\." > nul
IF %ERRORLEVEL% == 0 (goto END) else (GOTO driver_instv64)
:driver_instv64
%windir%\sysnative\pnputil.exe -a C:\GRL\GRL-USB_Drivers\vista\x64\cyusb3.inf
find "CYUSB3" C:\Windows\System32\drivers
IF %ERRORLEVEL% == 0 (GOTO Hardware_connect) else (GOTO ERR)

:ERRprocessor_vista
@echo Unsupported architecture "%processor_architecture%"!
GOTO END


:ver_Win7
echo --------------------	Windows_7 -----------------------
systeminfo | find "Windows Embedded"
IF %ERRORLEVEL% == 0 (GOTO win7em) else (GOTO win7)

:win7
IF EXIST "%PROGRAMFILES(X86)%" (GOTO x64) ELSE (GOTO x86)
GOTO ERRprocessor_7
 
:x86
echo --------------------	Windows_7 _x86 ---------------------
Pnputil -i -a C:\GRL\GRL-USB_Drivers\win7\x86\cyusb3.inf
Driverquery | findstr /i "GRL USB\CYUSB3\GRl USB Bootloader\." > nul
IF %ERRORLEVEL% == 0 (GOTO END) else (GOTO driver_inst786)

:driver_inst786
pnputil -a C:\GRL\GRL-USB_Drivers\win7\x86\cyusb3.inf
find "CYUSB3" C:\Windows\System32\drivers
IF %ERRORLEVEL% == 0 (GOTO Hardware_connect) else (GOTO ERR)

:x64
echo --------------------	Windows_7_x64 ---------------------
%windir%\sysnative\pnputil.exe -i -a C:\GRL\GRL-USB_Drivers\win7\x64\cyusb3.inf
Driverquery | findstr /i "GRL USB\CYUSB3\GRl USB Bootloader\." > nul
IF %ERRORLEVEL% == 0 (GOTO END) else (GOTO driver_inst764)

:driver_inst764
%windir%\sysnative\pnputil.exe -a C:\GRL\GRL-USB_Drivers\win7\x64\cyusb3.inf
find "CYUSB3" C:\Windows\System32\drivers
IF %ERRORLEVEL% == 0 (GOTO Hardware_connect) else (GOTO ERR)

:ERRprocessor_7
@echo Unsupported architecture "%processor_architecture%"!
GOTO END


:win7em
IF EXIST "%PROGRAMFILES(X86)%" (GOTO x64) ELSE (GOTO x86)
GOTO ERRprocessor_7em
 
:x86
echo --------------------windows_7em _x86 ---------------------
Pnputil -i -a C:\GRL\GRL-USB_Drivers\win7Embedded\x86\cyusb3.inf
Driverquery | findstr /i "GRL USB\CYUSB3\GRl USB Bootloader\." > nul
IF %ERRORLEVEL% == 0 (GOTO END) else (GOTO driver_inst7em86)

:driver_inst7em86
Pnputil -a C:\GRL\GRL-USB_Drivers\win7Embedded\x86\cyusb3.inf
find "CYUSB3" C:\Windows\System32\drivers
IF %ERRORLEVEL% == 0 (GOTO Hardware_connect) else (GOTO ERR)

:x64
echo --------------------windows_7em_x64 ---------------------
%windir%\sysnative\pnputil.exe -i -a C:\GRL\GRL-USB_Drivers\win7Embedded\x64\cyusb3.inf
Driverquery | findstr /i "GRL USB\CYUSB3\GRl USB Bootloader\." > nul
IF %ERRORLEVEL% == 0 (GOTO END) else (GOTO driver_inst7em64)

:driver_inst7em64
%windir%\sysnative\pnputil.exe -a C:\GRL\GRL-USB_Drivers\win7Embedded\x64\cyusb3.inf
find "CYUSB3" C:\Windows\System32\drivers
IF %ERRORLEVEL% == 0 (GOTO Hardware_connect) else (GOTO ERR)

:ERRprocessor_7em
@echo Unsupported architecture "%processor_architecture%"!
pause
GOTO END


:ver_Win8
echo --------------------windows_8 ---------------------
IF EXIST "%PROGRAMFILES(X86)%" (GOTO x64) ELSE (GOTO x86)
GOTO ERRprocessor_8

:x86
echo --------------------windows_8 _x86 ---------------------
Pnputil -i -a C:\GRL\GRL-USB_Drivers\win8\x86\cyusb3.inf
Driverquery | findstr /i "GRL USB\CYUSB3\GRl USB Bootloader\." > nul
IF %ERRORLEVEL% == 0 (GOTO END) else (GOTO driver_inst886)

:driver_inst886
Pnputil.exe -a C:\GRL\GRL-USB_Drivers\win8\x86\cyusb3.inf
find "CYUSB3" C:\Windows\System32\drivers
IF %ERRORLEVEL% == 0 (GOTO Hardware_connect) else (GOTO ERR)

:x64
echo --------------------windows_8 _x64 ---------------------
%windir%\sysnative\pnputil.exe -i -a C:\GRL\GRL-USB_Drivers\win8\x64\cyusb3.inf
Driverquery | findstr /i "GRL USB\CYUSB3\GRl USB Bootloader\." > nul
IF %ERRORLEVEL% == 0 (GOTO END) else (GOTO driver_inst864)

:driver_inst864
%windir%\sysnative\pnputil.exe -a C:\GRL\GRL-USB_Drivers\win8\x64\cyusb3.inf
find "CYUSB3" C:\Windows\System32\drivers
IF %ERRORLEVEL% == 0 (GOTO Hardware_connect) else (GOTO ERR)

:ERRprocessor_8
@echo Unsupported architecture "%processor_architecture%"!
pause
GOTO END


:ver_Win81
echo --------------------windows_8.1 ---------------------
IF EXIST "%PROGRAMFILES(X86)%" (GOTO x64) ELSE (GOTO x86)
GOTO ERRprocessor_81
 
:x86
echo --------------------windows_8.1 _x86 ---------------------
Pnputil -i -a C:\GRL\GRL-USB_Drivers\win81\x86\cyusb3.inf
Driverquery | findstr /i "GRL USB\CYUSB3\GRl USB Bootloader\." > nul
IF %ERRORLEVEL% == 0 (GOTO END) else (GOTO driver_inst8186)

:driver_inst8186
Pnputil -a C:\GRL\GRL-USB_Drivers\win81\x86\cyusb3.inf
find "CYUSB3" C:\Windows\System32\drivers
IF %ERRORLEVEL% == 0 (GOTO Hardware_connect) else (GOTO ERR)

:X64
echo --------------------windows_8.1 _x64 ---------------------
%windir%\sysnative\pnputil.exe -i -a C:\GRL\GRL-USB_Drivers\win81\x64\cyusb3.inf
Driverquery | findstr /i "GRL USB\CYUSB3\GRl USB Bootloader\." > nul
IF %ERRORLEVEL% == 0 (GOTO END) else (GOTO driver_inst8164)

:driver_inst8164
%windir%\sysnative\pnputil.exe -a C:\GRL\GRL-USB_Drivers\win81\x64\cyusb3.inf
find "CYUSB3" C:\Windows\System32\drivers
IF %ERRORLEVEL% == 0 (GOTO Hardware_connect) else (GOTO ERR)

:ERRprocessor_81
@echo Unsupported architecture "%processor_architecture%"!
pause
GOTO END

:ver_Win10
echo --------------------windows 10 ---------------------
IF EXIST "%PROGRAMFILES(X86)%" (GOTO x64) ELSE (GOTO x86)
GOTO ERRprocessor_81
 
:x86
echo --------------------windows 10_x86 ---------------------
Pnputil -i -a C:\GRL\GRL-USB_Drivers\Win10\x86\cyusb3.inf
Driverquery | findstr /i "GRL USB\CYUSB3\GRl USB Bootloader\." > nul
IF %ERRORLEVEL% == 0 (GOTO END) else (GOTO driver_inst1086)

:driver_inst1086
Pnputil -a C:\GRL\GRL-USB_Drivers\win10\x86\cyusb3.inf
find "CYUSB3" C:\Windows\System32\drivers
IF %ERRORLEVEL% == 0 (GOTO Hardware_connect) else (GOTO ERR)

:X64
echo --------------------windows_10 _x64 ---------------------

pnputil.exe -i -a C:\GRL\GRL-USB_Drivers\win10\x64\cyusb3.inf

Driverquery | findstr /i "GRL USB\CYUSB3\GRl USB Bootloader\." > nul
IF %ERRORLEVEL% == 0 (GOTO END) else (GOTO driver_inst1064)

:driver_inst1064
pnputil.exe -a C:\GRL\GRL-USB_Drivers\Win10\x64\cyusb3.inf
find "CYUSB3" C:\Windows\System32\drivers
IF %ERRORLEVEL% == 0 (GOTO Hardware_connect) else (GOTO ERR)

:ERRprocessor_81
@echo Unsupported architecture "%processor_architecture%"!
pause
GOTO END

:Installed
echo ---------------------Driver Already Installed --------------
goto END

:Hardware_connect
echo --Driver files are copied successfully. Driver will get installed upon connecting the hardware. 
pause
GOTO END


:ERR
echo ----------Driver Not Installed Properlly ---------------------

goto END

:END
echo ---------------programming completed------------------------------
pause
exit