@echo off
set projectDir=gtDriverVs2019
set drvDir=drv
@echo projectDir=%projectDir%
echo drvDir=%drvDir%
setlocal 
@echo Sign Driver files and create cat file
rem echo Verify signtool in path
@signtool /? > out.txt 2> error.txt
@if %ERRORLEVEL% == 0 goto findInf2cat
@echo Signtool.exe not in current path. Searching in other locations.
rem if not exist "C:\Program Files (x86)\Windows Kits\10\bin\10.0.17763.0\x86\signtool.exe" echo Adding signtool.exe to path && set path=%path%;C:\Program Files (x86)\Windows Kits\10\bin\10.0.17763.0\x86 
call findfile.bat signtool.exe
@signtool /? > out.txt 2> error.txt
@if %ERRORLEVEL% == 0 echo Added signtool.exe to path && goto findInf2cat
echo Error Signtool.exe not found 
goto last

:findInf2cat
rem echo Verify Inf2Cat in path
inf2cat.exe > out.txt 2> error.txt
if %ERRORLEVEL% == 0 goto copy_files
echo inf2cat not found in path. Searching for file in other locactions. 
call findfile inf2cat.exe
rem if exist "C:\Program Files (x86)\Windows Kits\10\bin\10.0.17763.0\x86\inf2cat.exe" echo Adding inf2cat.exe to path && set path=%path%;C:\Program Files (x86)\Windows Kits\10\bin\10.0.17763.0\x86
inf2cat.exe > out.txt 2> error.txt
@if %ERRORLEVEL% == 0 goto copy_files
echo ERROR inf2cat not founnd
goto last

rem set path=%path;C:\Program Files (x86)\Windows Kits\10\App Certification Kit;C:\Program Files (x86)\Windows Kits\10\bin\x86

:copy_files
@if exist error.txt del error.txt
rem @echo copy drive files
@if not exist %drvDir% echo mkdir %drvDir% && mkdir %drvDir%
@if not exist %drvDir%\amd64 echo mkdir %drvDir%\amd64 && mkdir %drvDir%\amd64
@if not exist %drvDir%\i386 echo mkdir %drvDir%\i386 && mkdir %drvDir%\i386
@if not exist %drvDir%\gtdriver.inf echo copy inf to %drvDir% && copy gtdriver.inf %drvDir% > out.txt
fc gtdriver.inf %drvDir%\gtdriver.inf > fc.txt
if not %ERRORLEVEL% == 0  echo Error gtdriver.inf file does not match verion in %drvDir% folder && type fc.txt && goto last
if exist fc.txt del fc.txt

set GT64Dir=%projectDir%\x64\Release\gtdriver
if not exist %GT64Dir%\gtdriver.sys echo "file %GT64Dir%\gtdriver.sys not found" && goto last
@echo copy %GT64Dir%\gtdriver.sys to %drvDir%\amd64 
copy /y %GT64Dir%\gtdriver.sys %drvDir%\amd64\gtdriver.sys > out.txt

if not exist %drvDir%\amd64\gtdriver.sys echo Missing driver file %drvDir%/amd64/gtdriver.sys && goto last

set GT32Dir=%projectDir%\Release\gtdriver
@if exist %GT32Dir%\gtdriver.sys echo copy %GT32Dir%\gtdriver.sys to %drvDir%\i386 && copy /y %GT32Dir%\gtdriver.sys %drvDir%\i386\gtdriverx86.sys > out.txt
if not exist %drvDir%\i386\gtdriverx86.sys echo Missing driver file %drvDir%/i386/gtdriverx86.sys && goto last

if not exist %drvDir%\gtdriver.inf echo missing inf file %drvDir%\gtdriver.inf && goto last

@if exist %GT64Dir%\..\gtdriver.pdb echo copy %GT64Dir%\..\gtdriver.pdb to %drvDir%\amd64 && copy /y %GT64Dir%\..\gtdriver.pdb %drvDir%\amd64\gtdriver.pdb > out.txt
@if exist %GT32Dir%\..\gtdriver.pdb echo copy %GT32Dir%\..\gtdriver.pdb to %drvDir%\i386  && copy /y %GT32Dir%\..\gtdriver.pdb %drvDir%\i386\gtdriverx86.pdb > out.txt

@if exist %projectDir%\gtdriver.DVL.XML echo copy %projectDir%\gtdriver.DVL.XML to %drvDir% && copy /y %projectDir%\gtdriver.DVL.XML %drvDir% > out.txt
if not exist %drvDir%\gtdriver.DVL.XML echo Missing gtdriver DVL Xml file && goto last

call osver.bat

@echo sign driver files 
rem /v          Print verbose success and status messages. This may also provide slightly more information on error.
rem /ac <file>  Add an additional certificate, from <file>, to the signature block
rem /tr <URL>   Specifies the RFC 3161 timestamp server's URL.
rem /td <alg>   Used with the /tr or /tseal switch to request a digest algorithm used by the RFC 3161 timestamp server.
rem /fd         Specifies the file digest algorithm to use for creating file signatures. (Default is SHA1)
rem /sha1 <h>   Specify the SHA1 thumbprint of the signing cert.

rem set Thumprint=/sha1 ea3f362df36026d404747bff76cec1d5f4501013
rem set Thumprint=/sha1 8541f2337480a78bec2254ae4f269b783a637b11
set Thumprint=/sha1 bfbd7cf6438fb797a6ae08fc2af8644de3a2a8b5
echo Thumprint=%Thumprint% 

set hash=sha256
echo hash=%hash%

@echo signtool.exe sign /v  /tr http://timestamp.digicert.com /td %hash% /fd %hash%  %Thumprint% /a %drvDir%\amd64\gtdriver.sys
signtool.exe sign /v  /tr http://timestamp.digicert.com /td %hash% /fd %hash% %Thumprint% /a %drvDir%\amd64\gtdriver.sys
if not %ERRORLEVEL% == 0 goto signerror

@echo signtool.exe sign /v /tr http://timestamp.digicert.com /td %hash% /fd %hash% %Thumprint% %drvDir%\i386\gtdriverx86.sys
signtool.exe sign /v /tr http://timestamp.digicert.com /td %hash% /fd %hash% %Thumprint% %drvDir%\i386\gtdriverx86.sys

@echo Create cat file
@if exist %drvDir%\gtdriver.cat del %drvDir%\gtdriver.cat
rem @echo inf2cat /driver:drv /os:10_X64,10_x86,Server10_X64 /V
rem inf2cat /driver:drv /os:10_X64,10_x86,Server10_X64 /V
@echo inf2cat /driver:%drvDir% /os:%OS% /V
inf2cat /driver:%drvDir% /os:%OS% /V
if not %ERRORLEVEL% == 0 goto error

@echo Sign cat file
signtool.exe sign /v /tr http://timestamp.digicert.com /td %hash% /fd %hash%  %Thumprint% %drvDir%\gtdriver.cat
if not %ERRORLEVEL% == 0 goto signerror
goto end

:signtool
@echo signtoll.exe not found.
goto last

:signerror
@echo error signing file
goto last

:error
@echo error creating cat file
goto last

:end
echo Succesfully signed code.
goto last

:last