@echo off
call toolkitENV.cmd

rem If "A%1A" == "AA" echo You must specify a name for your PE build.
If "A%1A" == "AA" set BuildName=winPDK
If not "A%1A" == "AA" set BuildName=%1

set WIN32KIT=%PDK%\win32
set NEWPE=%BUILD%\WinPE-%PDK_VERSION%\%BuildName%
set BUILD_STAGE=%BUILD%\WinPE-%PDK_VERSION%\%BuildName%_stage
set MEDIA_DIR=%BUILD%\WinPE-%PDK_VERSION%\%BuildName%_media
set NEWSYS32=%NEWPE%\i386\system32
set STARTNET=%NEWSYS32%\startnet.cmd
set EXTRAS=%PKGS%\WinPE\extras

echo Clean up before new build of WinPE.
If EXIST "%BUILD_STAGE%" rmdir %BUILD_STAGE% /s /q
mkdir %BUILD_STAGE%
echo Make a copy of WinPE to modify in %BUILD_STAGE% ....
xcopy %WINPE_CD_MOUNT%\* %BUILD_STAGE% /y /s/v/e/q

echo Add driver registry entries for Intel e1000 and other devices
copy %PDK%\Demos\Windows\WinPE\INTEL.WINPESYS.INF %BUILD_STAGE%\WinPE\WINPESYS.INF
echo Add our own preferred USER attributes to registry, specifically CMD shell properties.
copy %EXTRAS%\WINPEDEF.INF %BUILD_STAGE%\WinPE\WINPEDEF.INF

If EXIST "%NEWPE%"  rmdir %NEWPE% /s /q
mkdir %NEWPE%

pushd %BUILD_STAGE%\WinPE
echo Create new WinPE image tree %NEWPE% from %BUILD_STAGE% .....
echo WARNING: The mking.cmd will only succeed if DestinationDir (NEWPE)is on a local drive.
call mkimg.cmd %W2K3SP1_CD_MOUNT% %NEWPE% /PNP /WMI   > nul
rem call BUILDOPTIONALCOMPONENTS.VBS /s:%W2K3SP1_CD_MOUNT% /d:%NEWPE% /Q /WSH /HTA
cscript BUILDOPTIONALCOMPONENTS.VBS /s:%W2K3SP1_CD_MOUNT% /d:%NEWPE% /Q /WSH /HTA

echo Configuring Intel drivers within WinPE image tree ...
copy %PDK%\win32\drivers\amt\*.* %PDK%\win32\drivers
drvinst.exe /q /inf:%PDK%\win32\drivers  /oscd:%W2K3SP1_CD_MOUNT% %NEWPE%
copy /Y %PDK%\win32\drivers\*.SYS %NEWSYS32%\drivers
copy /Y %PDK%\win32\drivers\*.INF %NEWSYS32%\..\INF
popd
REM don't need BUILD_STAGE after mkimg
REM pause

echo To help speed image creation, remove unnecessary files from WinPE staging image.
echo We don' need all of these ODBC DLLs's
del /Q %NEWPE%\i386\SYSTEM32\ODBC*.*
echo But we do need these to enable VBScript, WSH and HTA
copy %BUILD_STAGE%\i386\SYSTEM32\ODBC32.dll %NEWPE%\i386\SYSTEM32
copy %BUILD_STAGE%\i386\SYSTEM32\ODBCint.dll %NEWPE%\i386\SYSTEM32
echo We can get rid of these unnecessary FONTS
del /Q %NEWPE%\i386\FONTS\*.*
echo But we do need these FONTS...
copy %BUILD_STAGE%\i386\FONTS\VGA*.FON     %NEWPE%\i386\FONTS
copy %BUILD_STAGE%\i386\FONTS\MARLETT.TTF  %NEWPE%\i386\FONTS
copy %BUILD_STAGE%\i386\FONTS\MICROSS.TTF  %NEWPE%\i386\FONTS
copy %BUILD_STAGE%\i386\FONTS\TAHOMA.TTF   %NEWPE%\i386\FONTS
copy %BUILD_STAGE%\i386\FONTS\TAHOMABD.TTF %NEWPE%\i386\FONTS

REM %BUILDROOT_PATH%\PythonPath > setPythonPath.cmd
REm call setPythonPath.cmd
echo using Python at %PythonInstall%
%PythonInstall%\python tree2uppercase.py %NEWPE%

echo Enable WinPE for WMI ....
for %%d in (repository autorecover logs mof) do mkdir %NEWSYS32%\wbem\%%d
xcopy %WINPE_CD_MOUNT%\i386\system32\wbem\repository\* %NEWSYS32%\wbem\repository /i /s /e /y /q

echo Add additional Gills Creek Toolkit files from %PDK%...
REM rmdir
mkdir %NEWPE%\deploy
xcopy %PDK%\Demos\Windows\WinPE\Python23 %NEWPE%\deploy\Python23 /i /s /e /y /q
echo Remove unnecessary Python files.
rmdir /S /Q %NEWPE%\deploy\Python23\Lib\bsddb
rmdir /S /Q %NEWPE%\deploy\Python23\Lib\email
REM rmdir /S /Q %NEWPE%\deploy\Python23\Lib\lib-tk

xcopy /i /y /s /q   "%PDK%\*" "%NEWPE%\deploy"
xcopy /S /-Y "%FACTORYPDK%\*" "%NEWPE%\deploy"
If EXIST "%BUILDROOT_PATH%\extras_forWinPE.cmd" call "%BUILDROOT_PATH%\extras_forWinPE.cmd"
xcopy /i /y /s /q  %EXTRAS%\*.*        %NEWPE%\i386\SYSTEM32
move %NEWPE%\i386\SYSTEM32\setupdd.sys %NEWPE%\i386\SYSTEM32\drivers

REM NOTE: BIOS & FW update files are located in the 'updates' folders of the boot device
REM they are copied into the %MEDIA_DIR%
REM xcopy %UPDATES%\* %NEWPE%\deploy\doc\ofu /i /y /s /q
REM pushd %NEWPE%\deploy\doc\ofu\S5000
REM "%UNZIP%" x -o%NEWPE%\deploy\doc\ofu\S5000 telco-wd_060807.zip -r
REM popd
REM pushd %NEWPE%\deploy\doc\ofu\S3000
REM "%UNZIP%" x -o%NEWPE%\deploy\doc\ofu\S3000 S3000AH_BIOS_R44.zip -r
REM popd

echo Add custom initialization to STARTNET.CMD ...
echo REM                                                             >> %STARTNET%
echo REM Following is Gills Creek custom initialization...           >> %STARTNET%
echo REM                                                             >> %STARTNET%
echo echo Setting up deployment environment, please wait ...         >> %STARTNET%
echo REM                                                             >> %STARTNET%
echo REM     Add Gills Creek Tools to PATH                           >> %STARTNET%
REM echo REM     Load driver to enable EEUPDATEW32.exe                   >> %STARTNET%
REM echo call %%SystemDrive%%\deploy\win32\reloadw32.exe                 >> %STARTNET%
rem echo REM     Map a network drive for additional tools            >> %STARTNET%
rem echo ipconfig                                                    >> %STARTNET%
rem echo REM net use i: \\10.243.42.206\images /USER:fred guest      >> %STARTNET%
echo start cmd                                                       >> %STARTNET%
echo start taskmgr                                                   >> %STARTNET%
echo net start dhcp                                                  >> %STARTNET%
echo REM Call OC.BAT to register and enable WSH/HTA scripting        >> %STARTNET%
echo call OC.BAT                                                     >> %STARTNET%
echo If EXIST "C:\AMR.usr"  call mapDrives.cmd                       >> %STARTNET%
echo REM     Start automatic deployment                              >> %STARTNET%
echo set RAMDrive=X:>> %STARTNET%
echo call %%SystemDrive%%\deploy\win32\deploy.cmd                    >> %STARTNET%
rem type %STARTNET%

echo Enable WinPE for Multi-Processor support.
del  %NEWSYS32%\ntoskrnl.exe
move %NEWSYS32%\ntkrnlmp.exe %NEWSYS32%\ntoskrnl.exe

echo Modifying %NEWPE%\i386\txtsetup.sif ....
type %WINPE_CD_MOUNT%\I386\TXTSETUP.SIF | %CYGWIN%\sed.exe 's/mps_mp[[:space:]]*= halapic.dll/mps_mp   = halmps.dll/' | %CYGWIN%\sed.exe 's/acpiapic_mp[[:space:]]*= halaacpi.dll/acpiapic_mp   = halmacpi.dll/' > %NEWSYS32%\..\TXTSETUP.SIF
"%PKGS%\WinPE\cygwin\diff" %WINPE_CD_MOUNT%\I386\TXTSETUP.SIF  %NEWSYS32%\..\TXTSETUP.SIF

echo Disable boot from CD prompt ...
REM move %NEWSYS32%\..\bootfix.bin %NEWSYS32%\..\bootfix.bin.SKIP
del %NEWSYS32%\..\bootfix.bin
rmdir %MEDIA_DIR% /s /q
mkdir %MEDIA_DIR%

echo copy %W2K3SP1_CD_MOUNT%\i386\NTDETECT.COM  %MEDIA_DIR%\
copy %W2K3SP1_CD_MOUNT%\i386\NTDETECT.COM  %MEDIA_DIR%\
REM PE2USB's MCR will only look for NTLDR when booting from USB
echo copy %W2K3SP1_CD_MOUNT%\i386\SETUPLDR.BIN  %MEDIA_DIR%\NTLDR
copy %W2K3SP1_CD_MOUNT%\i386\SETUPLDR.BIN  %MEDIA_DIR%\NTLDR

mkdir %MEDIA_DIR%\i386
echo NTDETECT.COM and SETUPLDR.BIN need to be in the i386 folder to boot from a CD-ROM.  > "%MEDIA_DIR%\i386\These files are needed for booting from CD-ROM.txt"
echo copy %W2K3SP1_CD_MOUNT%\i386\NTDETECT.COM  %MEDIA_DIR%\i386
copy %W2K3SP1_CD_MOUNT%\i386\NTDETECT.COM  %MEDIA_DIR%\i386
echo copy %W2K3SP1_CD_MOUNT%\i386\SETUPLDR.BIN  %MEDIA_DIR%\i386
copy %W2K3SP1_CD_MOUNT%\i386\SETUPLDR.BIN  %MEDIA_DIR%\i386

echo Create WINNT.SIF for bootUSB ...
set SIF=%MEDIA_DIR%\WINNT.SIF
echo [SetupData]                       > %SIF%
echo BootDevice = "ramdisk(0)"         >> %SIF%
echo BootPath = "\I386\System32\"      >> %SIF%
echo OsLoadOptions = "/noguiboot /fastdetect /minint /rdimageoffset=36352 /rdpath=%BuildName%.SDI"   >> %SIF%
echo Architecture = "I386"         >> %SIF%
echo ... WINNT.SIF file created.

echo Now, you must do the following on your SDI manually.
echo Start SDI Loader to add the %MEDIA_DIR%\%BuildName%.SDI disk.
echo After adding the new SDI image within the sdiloader, it must be initialized
echo Use disk manager or diskpart.exe to partition, format and assign drive letter R:
echo                      (make sure to use NTFS and enable Compression)
echo Finally:
echo          "%PKGS%\WinPE\Windows Resource Kits\Tools\robocopy.exe" /MIR %NEWPE% R:\ /R:3 /W:4
echo Note:
echo       Robocopy with retry switches is required because
echo       NTFS compression is not as fast as the file copy.
echo       Robocopy can be found in "Windows Server 2003 Resource Kit Tools"
echo       "http://www.microsoft.com/downloads/details.aspx?FamilyID=9D467A69-57FF-4AE7-96EE-B18C4790CFFD&displaylang=en"
echo create the CD ISO...
echo %WINPE_CD_MOUNT%\WinPE\OSCDIMG.EXE -h -n -l%BuildName% -b%WINPE_CD_MOUNT%\WinPE\ETFSBOOT.COM %NEWPE%\bootCD %NEWPE%\..\%BuildName%_CD.iso

echo Create RAMDisk SDI image: %MEDIA_DIR%\%BuildName%.SDI
echo SDIMgr.wsf and sdiloader.exe can be found in "Windows Embedded Trial"
echo http://www.microsoft.com/windows/embedded/eval/trial.mspx
cscript "%PKGS%\WinPE\Windows Embedded Trial\utilities\SDIMgr.wsf" %MEDIA_DIR%\%BuildName%.SDI /new
cscript "%PKGS%\WinPE\Windows Embedded Trial\utilities\SDIMgr.wsf" %MEDIA_DIR%\%BuildName%.SDI /createdisk:240000000
REM  echo %WINPE_CD_MOUNT%\WinPE\OSCDIMG.EXE -d -n -h -l%BuildName% %NEWPE% %MEDIA_DIR%\%BuildName%.iso
REM  %WINPE_CD_MOUNT%\WinPE\OSCDIMG.EXE -j1 -h -l%BuildName% %NEWPE% %MEDIA_DIR%\%BuildName%.iso

REM start  "SDI Loader" /B "%PKGS%\WinPE\Windows Embedded Trial\bin\sdiloader.exe"
echo "%PKGS%\WinPE\Windows Embedded Trial\bin\sdimount"  "%MEDIA_DIR%\%BuildName%.SDI"
"%PKGS%\WinPE\Windows Embedded Trial\bin\sdimount"  "%MEDIA_DIR%\%BuildName%.SDI"

%CYGWIN%\sleep 15
echo Press ENTER when ready.....
REM pause

echo off
echo  list disk     > dp.tmp
echo  list volume  >> dp.tmp

:waitForDisk
diskpart /s dp.tmp  > dp.out
type dp.out
findstr /C:"Disk " dp.out | findstr /C:"Disk " | %CYGWIN%\tail.exe -1 | %CYGWIN%\awk.exe '{ print $2 }' > num
set /p SDIdisk= < num
findstr "Volume" dp.out | findstr /V /C:"Volume #" | find  /c "Volume"  > num
set /p SDIvol= < num

rem %CYGWIN%\sleep 5

if "A%%SDIdisk%%A"=="AA" goto waitForDisk
if "A%%SDIvol%%A"=="AA" goto waitForDisk

echo -------------------------------------------------------------------
set SDIdisk
set SDIvol
del dp.tmp dp.out num
echo -------------------------------------------------------------------
echo Your SDI image will now be paritioned and formatted with NTFS w/Compression enbaled.
echo WARNING:  Your SDI Disk image is Disk %SDIdisk%.
echo           It's volume number will be %SDIvol%.
echo           Drive letter R: must be FREE to be assigned.
echo           If this is not the case, CNTRL-C NOW!
echo Press ENTER to begin..
REM pause
echo  select disk %SDIdisk%      > diskpart.sdi
echo  create partition primary  >> diskpart.sdi
echo  select volume %SDIvol%    >> diskpart.sdi
echo  assign letter=r           >> diskpart.sdi
echo  list disk                 >> diskpart.sdi
echo  list volume               >> diskpart.sdi
diskpart /s diskpart.sdi
del diskpart.sdi
format R: /Y /Q /V:winPDK  /FS:NTFS /C

echo ROBOCOPY is now populating the SDI disk image
echo To check on the status of the copy run:
echo      "%PKGS%\WinPE\cygwin\tail" -f %BUILDROOT_PATH%\..\SDIcopy.log
echo from another console prompt.
echo This may take ~20 minutes......

"%ROBOCOPY%" /MIR %NEWPE% R:\ /R:3 /W:4 > %BUILDROOT_PATH%\..\SDIcopy.log
echo  select volume %SDIvol%     > diskpart.sdi
echo  remove                    >> diskpart.sdi
diskpart /s diskpart.sdi
del diskpart.sdi

If NOT EXIST "%MEDIA_DIR%\updates" mkdir "%MEDIA_DIR%\updates"
xcopy  "%UPDATES%\*"    %MEDIA_DIR%\updates    /E /V /Y

set MKISO="%MEDIA_DIR%\..\mkISO.cmd"
echo pushd "%WINPE_CD_MOUNT%\WinPE"                     > %MKISO%
echo OSCDIMG.EXE -h -n -l%BuildName% -bETFSBOOT.COM "%MEDIA_DIR%"  "%MEDIA_DIR%\..\%BuildName%.iso"  >> %MKISO%
echo popd                                               >> %MKISO%
echo echo Copying %BuildName%.iso to network share ...  >> %MKISO%
echo copy /Y %BuildName%.iso  %SHARE_ISO_COPY%          >> %MKISO%
:REMOVE
echo .
echo .
echo .   You MUST now remove the disk from SDIloader.
echo .
echo .
REM pause
echo "%PKGS%\WinPE\Windows Embedded Trial\bin\sdimount" -u "%MEDIA_DIR%\%BuildName%.SDI"
"%PKGS%\WinPE\Windows Embedded Trial\bin\sdimount" -u "%MEDIA_DIR%\%BuildName%.SDI"

echo  select volume %SDIvol%     > diskpart.sdi
diskpart /s diskpart.sdi | findstr /C:"Volume %SDIvol%" > sdi.tmp
del diskpart.sdi
for /f %%i in ('dir /b sdi.tmp') do ( set STILLRUNNING=%%~zi    )
del sdi.tmp
if %STILLRUNNING% GTR 0 goto :REMOVE

echo Make a USB stick WinPE-bootable by formatting it with PeToUSB.exe.
echo and copy the contents of %MEDIA_DIR% to the USB stick.
echo .
echo To create a bootable WinPE CD: run mkISO.cmd
findstr /I "cannot found rror missing"  ..\SDIcopy.log | findstr /V "100%"


