@echo on
@setlocal

@set NO_SYS_SIGNING=
rem *** parse arguments
:loop1
@if /I "%1"=="no_sys_signing"     set NO_SYS_SIGNING=on
@shift
@if not "%1"=="" goto loop1

@rem *** path to INF dir where .inf files are located
@set INFDIR=.

@rem *** subdir for debug driver package
@set TGTDIR_DBG=debug

@rem *** subdir for release driver package
@set TGTDIR_REL=release

@rem prefixes for signed and unsigned packages
@set TGTDIR_PARENT_SIGNED=driver_package
@set TGTDIR_PARENT_UNSIGNED=driver_package_unsigned

@rem *** the Thesycon preprocessor
@set PP=.\tools\TPP.exe

@rem *** NOTE: We should always use the full list of OS.

@rem *** OS list for x86, see inf2cat.txt
set OSLIST_x86=2000,XP_X86,Server2003_X86,Vista_X86,7_X86,Server2008_X86,8_X86,6_3_X86
@rem *** OS list for x64, see inf2cat.txt
set OSLIST_x64=XP_X64,Server2003_X64,Vista_X64,7_X64,Server2008_X64,Server2008R2_X64,8_X64,Server8_X64,6_3_X64,Server6_3_X64

@rem
@rem OS list contains a combination of the following values:
@rem    2000  XP_X86   Server2003_X86   Vista_X86  7_X86  Server2008_X86                      8_X86        6_3_X86
@rem          XP_X64   Server2003_X64   Vista_X64  7_X64  Server2008_X64   Server2008R2_X64   8_X64        6_3_X64
@rem                   Server2003_IA64                    Server2008_IA64  Server2008R2_IA64  Server8_X64  Server6_3_X64
@rem                                                                                          8_ARM
@rem

@rem load the version setting
@call .\setver.cmd

@rem load the customer settings
@call .\setvars.cmd

@rem load the certificate settings
@call ..\signing\set_vendor_certificate.cmd 

@rem remove the quotes, if any
@set DRIVER_NAME_BASE=%DRIVER_NAME_BASE:"=%

@call ..\signing\set_sign_tools.cmd 

@set UNSIGNED=

@rem check if driver shall not be signed by intension
@rem ------------------------------------------------
@if "%NO_SYS_SIGNING%"=="on" (
   @set UNSIGNED=1
)

@rem check if SignTools package is available
@rem ---------------------------------------
@if "%SIGNTOOLS%"=="" (
	@echo.
	@echo ***************************************************************************
	@echo The environment variable SIGNTOOLS has not been set.
	@echo Please install the SignTools package from Thesycon. Ask Thesycon for a free
	@echo copy of the SignTools installer at the e-mail address 
	@echo info^(at^)thesycon^(dot^)de. 
	@echo If you don't install the SignTools package then this script will build a 
	@echo driver package which is not digitally signed. 
	@call :EchoWarning
	pause
	@set UNSIGNED=1
)

@rem check if Vendor Certificate is set
@rem ----------------------------------
@if "%VENDOR_CERTIFICATE%"=="" (
	@echo.
	@echo ***************************************************************************
	@echo The environment variable VENDOR_CERTIFICATE has not been set.
	@echo If you own a code signing certificate from VeriSign or another provider
	@echo then edit set_vendor_certificate.cmd or set VENDOR_CERTIFICATE in your 
	@echo machine's environment.
	@echo If you don't own a certificate then this script will build a driver package
	@echo which is not digitally signed. 
	@call :EchoWarning
	pause
	@set UNSIGNED=1
)

@rem set the target directories (differ signed/unsigned)
@rem ---------------------------------------------------
@if "%UNSIGNED%"=="1" ( 
    @rem unsigned package
    @set TGTDIR_DBG=%TGTDIR_PARENT_UNSIGNED%\%TGTDIR_DBG%
    @set TGTDIR_REL=%TGTDIR_PARENT_UNSIGNED%\%TGTDIR_REL%
    @rmdir /S /Q %TGTDIR_PARENT_UNSIGNED% >nul 2>nul
	@goto tgtdir_defined
)

@if "%UNSIGNED%"=="" ( 
    @rem signed package
    @set TGTDIR_DBG=%TGTDIR_PARENT_SIGNED%\%TGTDIR_DBG%
    @set TGTDIR_REL=%TGTDIR_PARENT_SIGNED%\%TGTDIR_REL%
    @rmdir /S /Q %TGTDIR_PARENT_SIGNED% >nul 2>nul
 	@goto tgtdir_defined
)

:tgtdir_defined

@echo on

@rem create target dirs as defined before
@rem ------------------------------------
@if EXIST .\bin_signed\debug (
  mkdir %TGTDIR_DBG% >nul 2>nul
  @if %errorlevel% NEQ 0 pause
)
mkdir %TGTDIR_REL% >nul 2>nul
@if %errorlevel% NEQ 0 pause

@rem copy .inf files
@rem ---------------
@if EXIST .\bin_signed\debug (
	call :DoPreProcessing %INFDIR%\cdcacmpo.inf.src %TGTDIR_DBG%\%DRIVER_NAME_BASE%.inf
)
call :DoPreProcessing %INFDIR%\cdcacmpo.inf.src %TGTDIR_REL%\%DRIVER_NAME_BASE%.inf

@rem copy the appropriate .sys files to the package
@rem ----------------------------------------------
@if "%UNSIGNED%"=="1" ( 
    @rem copy unsigned .sys files to package
    @if EXIST .\bin\debug (
	  @call :CopySys %TGTDIR_DBG% .\bin\debug
    )
    @call :CopySys %TGTDIR_REL% .\bin\release
    @rem create .cat files
    @if not "%SIGNTOOLS%"=="" call :CreateCat
	@goto end
)
@if "%UNSIGNED%"=="" ( 
    @rem copy signed .sys files to package
    @if EXIST .\bin_signed\debug (
	    @call :CopySys %TGTDIR_DBG% .\bin_signed\debug
    )
    @call :CopySys %TGTDIR_REL% .\bin_signed\release
) 

@rem    @echo on
@rem    @rem .sys files have already been signed within DoPreProcessing,
@rem    @rem      so there is no additional signing needed
@rem    @rem sign the .sys files
@rem    @set FILE_LIST=%TGTDIR_REL%\*.sys
@rem    @if EXIST .\bin_signed\debug set FILE_LIST=%FILE_LIST% %TGTDIR_DBG%\*.sys
@rem    @if not "%NO_SYS_SIGNING%"=="on" (
@rem      cmd /c ..\signing\vendor_sign_files.cmd %FILE_LIST%
@rem    )

@rem create the .cat files
@call :CreateCat

@rem sign the .cat files
@rem  this will avoid security message 'Provider of SW cannot be checked'
@set FILE_LIST=%TGTDIR_REL%\*.cat
@if EXIST .\bin_signed\debug set FILE_LIST=%FILE_LIST% %TGTDIR_DBG%\*.cat
cmd /c ..\signing\vendor_sign_files.cmd %FILE_LIST%

@echo off
@goto end
rem *************** end of 'main'



rem *************** Procedures begin here ****************************

rem *** DoPreProcessing  %1=In-File [%2=Out-File]
:DoPreProcessing
@rem if no Out-File is given, use In-File without .src extension
@set OUTF=%2
@if "%OUTF%" == "" set OUTF=%~dpn1
@attrib -r %OUTF% >nul 2>nul
@echo on
%PP% %1 -o %OUTF% -f  %TPP_VERDEF_ARGS% %TPP_DEFS%
@if %errorlevel% NEQ 0 pause
@echo off
@attrib +r %OUTF% >nul 2>nul
@exit /b 0


rem *** CopySys  %1=TargetDir %2=source
:CopySys
@echo on
copy %2\cdcacmpo.sys      %1\%DRIVER_NAME_BASE%.sys
@if %errorlevel% NEQ 0 pause
copy %2\cdcacmpo_x64.sys  %1\%DRIVER_NAME_BASE%_x64.sys
@if %errorlevel% NEQ 0 pause
@echo off
@exit /b 0
rem ***

:CreateCat
@if EXIST .\bin_signed\debug (
	%SIGNTOOLS%\bin\inf2cat.exe /driver:%TGTDIR_DBG%  /os:%OSLIST_x86%,%OSLIST_x64%
	@if %errorlevel% NEQ 0 pause
)
%SIGNTOOLS%\bin\inf2cat.exe /driver:%TGTDIR_REL%  /os:%OSLIST_x86%,%OSLIST_x64%
@if %errorlevel% NEQ 0 pause
@exit /b 0

:EchoWarning
@echo. 
@echo If this package is installed you will need to confirm warning messages 
@echo raised by the system during driver installation. The driver will install
@echo and run anyway because .sys files are signed with Thesycon's certificate
@echo in this case. On Windows 8 x64 the unsigned driver cannot be installed.
@echo See reference manual for more details. 
@echo.
@echo Press any key to continue creating an unsigned driver package.
@echo ***************************************************************************
@echo.
@exit /b 0

rem *************** Procedures end here ****************************


:end
endlocal
