@echo off
setlocal
rem ***************************************************************************
rem * Use this cmd file to get the entire WAN Links project from Sourcesafe   *
rem * (vss) and build it.  Assumes a clean "get" of the entire project from   *
rem * vss (i.e. from $/WanLinks/Latest on down).  This cmd file gets and      *
rem * builds the autoincrement component, executes it to increment the build  *
rem * number for all components, and checks in the new build number header.   *
rem * Then, does a complete "get" on the project and calls make.cmd to build  *
rem * the project.                                                            *
rem *                                                                         *
rem * Must be called from an NTDDK checked or free build window.  Builds all  *
rem * components checked or free (debug or release) based on the build env.   *
rem *                                                                         *
rem * Parameters:                                                             *
rem *                                                                         *
rem *    %1  <VSS path>    // SourceSafe path. (no longer optional)           *
rem *                                                                         *
rem *    %2  <tag>         // Optional SourceSafe Tag.  Must be a quoted      *
rem *                      // string if spaces are present.                   *
rem *                                                                         *
rem *                                                                         *
rem * Associated files: buildall.cmd.                                         *
rem *                                                                         *
rem * buildall.cmd                                                            *
rem * This cmd file performs the actual build of all components.  Release     *
rem * files are placed in the .\bin\<processor>\<buildenv> directory.         *
rem *                                                                         *
rem ***************************************************************************

echo.
echo Digi Wan Links build utility.  Builds the entire Wan Links project.

rem Use DebugScript Env var to *echo* commands to console instead of executing them.
if     defined DebugScript set DebugEcho=echo
if not defined DebugScript set DebugEcho=


rem Process commandline parameters
if "%1"=="" goto ExitDisplaySyntax
if NOT "%3"=="" goto ExitDisplaySyntax

set VssRoot=%1
set VssLabel=%2

echo.
echo VssRoot = %VssRoot%
echo VssLabel= %VssLabel%

rem ***************************************************************************
rem * Verify and remember the build environment.  IDE-based projects need     *
rem * this info passed on the nmake command line to build correctly.          *
rem ***************************************************************************

:GetBuildEnvironment
echo.
echo Verifying environment...
set DIGIBUILDENV=
if "%DDKBUILDENV%"==""          goto ExitNotBuildWindow
if "%DDKBUILDENV%"=="checked" set DIGIBUILDENV=Debug
if "%DDKBUILDENV%"=="free"    set DIGIBUILDENV=Release
if "%DIGIBUILDENV%"==""         goto ExitCorruptedBuildWindow

if "%DDK_INC_PATH%"=="" (set NTOSVER=NT4) else (set NTOSVER=NT5)

:GetPlatform
if "%Cpu%"==""         goto ExitNotBuildWindow
if /I "%Cpu%"=="i386"  goto GetPlatformComplete
if /I "%Cpu%"=="alpha" goto GetPlatformComplete
if /I "%Cpu%"=="ia64"  goto GetPlatformComplete
goto ExitCorruptCpu
:GetPlatformComplete
echo Verified, found:  
echo    Cpu         = %Cpu%
echo    DdkBuildEnv = %DDKBUILDENV%
echo    NtOsVer     = %NTOSVER%

rem ***************************************************************************
rem * AutoIncrement the Build Number                                          *
rem *                                                                         *
rem * All components in the Wan Links build share a single build number.      *
rem * Defined in a single header file it is included in all version rc files. *
rem * When doing a full build (using this script), we auto increment the      *
rem * common buildnumber.                                                     *
rem ***************************************************************************

if defined SkipAutoInc goto AutoIncComplete
echo.
echo Build the AutoIncrementor
if not exist .\autoinc md autoinc
pushd autoinc
%DebugEcho% ss get -I- %VssRoot%/autoinc/autoinc.exe
if ERRORLEVEL 1 goto ExitToolsBuildError
popd
if ERRORLEVEL 1 goto ExitToolsBuildError

echo.
echo Auto-increment the buildnumber
if not exist .\inc md inc
pushd inc
%DebugEcho% ss checkout %VssRoot%/Inc/dxbbuildnumber.h
%DebugEcho% ..\autoinc\autoinc.exe dxbbuildnumber.h
if ERRORLEVEL 1 goto ExitAutoIncFailed

if (%VssLabel%)==() (set VssComment="AutoIncremented by %USERNAME% on %COMPUTERNAME%.") else (set VssComment=AutoIncrement_ReleaseLabel:%VssLabel%)
%DebugEcho% ss checkin -C%VssComment% -I- %VssRoot%/Inc/dxbbuildnumber.h
if ERRORLEVEL 1 goto ExitToolsBuildError
popd
:AutoIncComplete

rem ***************************************************************************
rem * Get all components.                                                     *
rem *                                                                         *
rem * Get all source from vss.  Use optional command line path and tag, if    *
rem * specified.                                                              *
rem ***************************************************************************

if defined SkipVssGet goto GetComplete
echo.
echo Get all files from SourceSafe.
if (%VssLabel%)==() goto NoLabel
%DebugEcho% ss label %VssRoot% -I- -L%VssLabel% -C-
if ERRORLEVEL 1 goto ExitLabelAlreadyUsed
%DebugEcho% ss get -I- -R -VL%VssLabel% %VssRoot%
goto GetComplete
:NoLabel
%DebugEcho% ss get -I- -R %VssRoot%
:GetComplete


rem ***************************************************************************
rem * Build all components.                                                   *
rem *                                                                         *
rem * Start with ddk environment builds, i.e. using build utility.  Then walk *
rem * through all exported projects building one at a time.                   *
rem ***************************************************************************

if defined SkipBuild goto BuildComplete
echo.
echo Build all components
call buildall.cmd /c 
if ERRORLEVEL 1 goto ExitBuildError
:BuildComplete

echo.
echo Done!
goto Exit

:ExitCorruptCpu
call :PrintErrorBanner
echo Corrupted Cpu in the build environment.  Open a new checked or
echo free build window and try again!
goto Exit

:ExitNoVssPath
call :PrintErrorBanner
echo %0 must be called with a VSS path specified
echo (e.g. %0 $/WANLinks/Latest NT5).
goto Exit

:ExitNoOSVer
call :PrintErrorBanner
echo %0 must be called with a NT OS Version specified 
echo (e.g. %0 $/WANLinks/Latest NT5).
goto Exit

:ExitNotBuildWindow
call :PrintErrorBanner
echo %0 must be called from an NTDDK Checked or Free build window.
goto Exit

:ExitCorruptedBuildWindow
call :PrintErrorBanner
echo Corrupted DDKBUILDENV in the build environment.  Open a new checked
echo or free build window and try again!
goto Exit

:ExitToolsBuildError
call :PrintErrorBanner
echo Error building tools, stopping build.
goto Exit

:ExitLabelAlreadyUsed
popd
call :PrintErrorBanner
echo Label already used, stopping build.
goto Exit

:ExitAutoIncFailed
%DebugEcho% ss uncheckout %VssRoot%/Inc/dxbbuildnumber.h -i-
popd
call :PrintErrorBanner
echo AutoInc returned error, stopping build.
goto Exit

:ExitBuildError
call :PrintErrorBanner
echo Build returned errors, aborting.
goto Exit

:ExitDisplaySyntax
echo.
echo   ***************************************************************************
echo   * Use this cmd file to get the entire WAN Links project from Sourcesafe   *
echo   * (vss) and build it.  Assumes a clean "get" of the entire project from   *
echo   * vss (i.e. from $/WanLinks/Latest on down).  This cmd file gets and      *
echo   * builds the autoincrement component, executes it to increment the build  *
echo   * number for all components, and checks in the new build number header.   *
echo   * Then, does a complete "get" on the project and calls make.cmd to build  *
echo   * the project.                                                            *
echo   *                                                                         *
echo   * Must be called from an NTDDK checked or free build window.  Builds all  *
echo   * components checked or free (debug or release) based on the build env.   *
echo   *                                                                         *
echo   * Parameters:                                                             *
echo   *                                                                         *
echo   *                                                                         *
echo   *    1  ^<VSS path^>    // SourceSafe path. (no longer optional)            *
echo   *                                                                         *
echo   *    2  ^<tag^>         // Optional SourceSafe Tag.  Must be a quoted       *
echo   *                     // string if present.                               *
echo   *                                                                         *
echo   *                                                                         *
echo   * Associated files: buildall.cmd.                                         *
echo   *                                                                         *
echo   * buildall.cmd                                                            *
echo   * This cmd file performs the actual build of all components.  Release     *
echo   * files are placed in the .\bin\^<processor^>\^<buildenv^> directory.         *
echo   *                                                                         *
echo   ***************************************************************************
echo.
goto Exit

:Exit
endlocal
goto :EOF


rem ***************************************************************************
rem * SubRoutines                                                             *
rem ***************************************************************************

:PrintErrorBanner
rem Parameters
rem   None
echo.
echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
echo.
goto :EOF
