@echo off
set scriptDebug=off
setlocal ENABLEEXTENSIONS

@echo %scriptDebug%


rem allow for win2k ddk concurrent with nt4.0 ddk - use W2KBASE as a clue
rem only look at W2KBASE if %1 == -W2K otherwise just use BASEDIR

if /I %1 NEQ -W2K goto NoW2kBase

shift

if "%W2KBASE%"=="" goto NoW2kBase

set BASEDIR=%W2KBASE%

:NoW2kBase
 
if "%BASEDIR%"=="" goto ErrNoBASEDIR

set path=%path%;%BASEDIR%\bin

set mode=
for %%f in (free FREE checked CHECKED) do if %%f == %1 set mode=%1
if %mode%=="" goto ErrBadMode

if %2 == "" goto ErrNoDir

if not exist %2 goto ErrNoDir

pushd .
call %BASEDIR%\bin\setenv.bat %BASEDIR% %mode% "%MSDEV%"
popd

@echo %scriptDebug%

set mpFlag=-M

if "%BUILD_ALT_DIR%"=="" goto NT4

rem win2k sets this!
set W2kEXT=%BUILD_ALT_DIR%

set mpFlag=-MI

:NT4

if "%NUMBER_OF_PROCESSORS%"=="" set mpFlag=
if "%NUMBER_OF_PROCESSORS%"=="1" set mpFlag=

@echo build in directory %2 with arguments %3 (basedir %BASEDIR%)

cd %2
set bflags=-e
set bscFlags=""

if "%3" == "" goto done

if "%3" == "/a" goto rebuildall

set bscFlags=/n

set bflags=%3 -e

goto done

:rebuildall

set bscFlags=/n
set bflags=-cfe

:done

if EXIST build%W2kEXT%.err	erase build%W2kEXT%.err
if EXIST build%W2kEXT%.wrn  erase build%W2kEXT%.wrn
if EXIST build%W2kEXT%.log	erase build%W2kEXT%.log


@echo run build %bflags% %mpFlag% for %mode% version in %2
pushd .
build  %bflags% %mpFlag%
popd

@echo %scriptDebug%

rem assume that the onscreen errors are complete!

@echo =============== build warnings ======================
if exist build%W2kEXT%.log findstr "warning.*[CLU][0-9]*" build%W2kEXT%.log

@echo. 
@echo. 
@echo build complete

@echo building browse information files

if EXIST buildbrowse.cmd goto doBrowsescript

set sbrlist=sbrList.txt

if not EXIST sbrList%CPU%.txt goto sbrDefault

set sbrlist=sbrList%CPU%.txt

:sbrDefault

if not EXIST %sbrlist% goto end

if %bscFlags% == "" goto noBscFlags

bscmake %bscFlags% @%sbrlist%

goto end

:noBscFlags

bscmake @%sbrlist%

goto end

:doBrowsescript

call buildBrowse %mode%

goto end

:ErrBadMode
@echo error: first param must be "checked" or "free"
goto usage

:
ErrNoBASEDIR
@echo error: BASEDIR environment variable not set, reinstall DDK!
goto usage

:ErrnoDir
@echo Error: second parameter must be a valid directory

:usage
@echo usage: ddkbuild [-W2K] "checked | free" "directory-to-build" [flags] 
@echo        -W2K       indicates development system uses W2KBASE environment variable
@echo                   to locate the win2000 ddk, otherwise BASEDIR is used (optional)
@echo         checked   indicates a checked build
@echo         free      indicates a free build (must choose one or the other)
@echo         directory path to build directory, try . (cwd)
@echo         flags     any random flags you think should be passed to build (try /a for clean)
@echo
@echo         ex: ddkbuild checked . 
@echo

rem goto end

:end

@echo ddkbuild complete
