@echo off
REM ==============================================================
REM Start=DS WinPE startup code
REM Version=6.9 SP5/7.1 SP1
REM --------------------------------------------------------------
:Start
if not defined log if not defined __log if exist "%systemdrive%\__log__" set __log=%~dpn0.log
if defined log if not defined __log set __log=true& call %0 %* >>"%~dp0%log%" & set __log=& goto :eof
if defined echo (echo on) else if exist "%systemdrive%\__echo__" (echo on) else (echo off)
set __dbg=rem & if defined debug (set __dbg=) else if exist "%systemdrive%\__debug__" (set __dbg=) else (set __dbg=rem)

REM ==============================================================
REM Call=Call or continue from specified label
REM --------------------------------------------------------------
:Call
set __rtn=
if not "%~2" == "" if /i "%~1" == "call" (
  set __rtn=goto :eof
  shift & shift
  call :%~0 %1 %2 %3 %4 %5 %6 %7 %8 %9
  goto :eof
) else if /i "%~1" == "goto" (
  set __lbl=%~2
  shift 1 & shift 1
  goto %__lbl%
)

REM ==============================================================
REM SetUtl=Set path to startup utilities file
REM --------------------------------------------------------------
:SetUtl
set __utl=%~dp0
set __utl="%__utl%startutl.bat" 
%__rtn%

REM ==============================================================
REM SetPath=Set path, and set working dir to system root
REM --------------------------------------------------------------
:SetPath
path %systemdrive%\;%path%
cd \
%__rtn%

REM ==============================================================
REM AltirisEnv=Predefine Altiris environment variables to default values
REM User scripts in \prestart or \startup can redefine these
REM --------------------------------------------------------------
:EnvVars
if exist "%systemdrive%\setenv.bat" call "%systemdrive%\setenv.bat" %*
%__rtn%

REM ==============================================================
REM LockWindows=Lock Windows
REM --------------------------------------------------------------
:LockWindows
if defined _ScreenLock "%systemdrive%\wndctl" -lock
%__rtn%

REM ==============================================================
REM cls=Clear screen and display startup message
REM --------------------------------------------------------------
:cls
rem cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo Please wait while DS WinPE%__winpe% %ALTIRIS_PROCESSOR% Pre-boot Automation Environment (%ALTIRIS_BOOTDEVICE% boot) loads...
echo.
%__rtn%

REM ==============================================================
REM SetTitle=Set the window title
REM --------------------------------------------------------------
:SetTitle
echo Initializing Windows PE%__winpe%...
set _cmdwnd=DS WinPE%__winpe% %ALTIRIS_PROCESSOR% Pre-boot Automation Environment (%ALTIRIS_BOOTDEVICE% boot)
title %_cmdwnd%
%__rtn%

REM ==============================================================
REM OptComponents=Install optional components (WinPE1 only)
REM --------------------------------------------------------------
:OptComponents
call %__utl%:is-winpe1
if not errorlevel 1 if exist "%systemroot%\system32\oc2.bat" (
  start "Installing Components..." /min "%systemroot%\system32\oc2.bat"
  "%systemdrive%\wndctl" -hide "Installing Components..."
)
%__rtn%

REM ==============================================================
REM LoadRegistry=Load registry entries
REM --------------------------------------------------------------
:LoadRegistry
if exist "%systemdrive%\reg\*.*" call %__utl%:regkey-load "%systemdrive%\reg\*.*"
%__rtn%

REM ==============================================================
REM RegisterAutoutil=Register the autoutil utility
REM --------------------------------------------------------------
:RegisterAutoutil
if exist "%systemdrive%\autoutil.*" "%systemdrive%\autoutil" -reg >nul 2>nul
%__rtn%

REM ==============================================================
REM PreloadDrivers=Call wpeint to load drivers, will be called again (see :Network) if @inject@ exists
REM --------------------------------------------------------------
:PreloadDrivers
if defined _PreloadDrivers (
  echo Loading drivers...
  if exist %systemdrive%\unattend.xml (
    wpeinit -unattend=%systemdrive%\unattend.xml
  ) else (
    wpeinit
rem    if exist %systemdrive%\staticip.bat (
rem      start "Configure Static IP" /wait /min %systemdrive%\staticip.bat
rem    )
  )
rem  if exist %systemdrive%\staticip.bat (
rem    start "Configure Static IP" /min %systemdrive%\staticip.bat
rem    %systemdrive%\wndctl -hide "Configure Static IP"
rem  )
)
%__rtn%

REM ==============================================================
REM FindBoot=Find the boot drive
REM This will set the ALTIRIS_BOOTDISK environment variable to the
REM drive letter of the boot drive. It will also set the NET
REM variable to the drive letter of the drive containing the
REM network folder.
REM --------------------------------------------------------------
:FindBoot
call %__utl%:fnd-bootdisk atrsboot ALTIRIS_BOOTDISK %ALTIRIS_BOOTDEVICE% %systemdrive%
%__rtn%

REM ==============================================================
REM InjectFiles=Inject the dynamic files from the boot drive (WinPE2 only)
REM --------------------------------------------------------------
:InjectFiles
call %__utl%:is-winpe3
if not errorlevel 1 (
  if exist "%ALTIRIS_BOOTDISK%\@inject@" set __src=%ALTIRIS_BOOTDISK%\@inject@
  if exist "%ALTIRIS_BOOTDISK%\boot\altiris\iso\@inject@" set __src=%ALTIRIS_BOOTDISK%\boot\altiris\iso\@inject@
  if defined __src (
    setlocal EnableDelayedExpansion EnableExtensions
    echo Injecting dynamic files...
    for /f "delims=| usebackq" %%i in (`xcopy /E /R /H /Y "!__src!" %systemdrive%\`) do echo.  %%i
    if exist "!__src!%~pnx0" endlocal & "%~dpnx0" goto InjectContinue
    endlocal
  )
)
%__rtn%
:InjectContinue

REM ==============================================================
REM bwfixup=Run bwinst to cleanup the automation partition
REM --------------------------------------------------------------
:bwfixup
if not exist "%ALTIRIS_BOOTDISK%\bwinst.ok" if exist "%systemdrive%\bwinst.*" (
  echo Completing automation partition installation...
  "%systemdrive%\bwinst.exe" -bwfix
  if exist "%ALTIRIS_BOOTDISK%" echo Done >"%ALTIRIS_BOOTDISK%\bwinst.ok" 2>nul
)
%__rtn%

REM ==============================================================
REM UserPrestart=Load user prestart applications from prestart folder
REM --------------------------------------------------------------
:UserPrestart
dir /b /a-d "%systemdrive%\prestart\*.*" >nul 2>nul
if not errorlevel 1 (
  echo Loading user prestart applications...
  call %__utl%:startup "%systemdrive%\prestart\*.*" "" wait echo call ""
)
%__rtn%

REM ==============================================================
REM Network=Initialize WinPE and the network
REM --------------------------------------------------------------
:Network
if defined _StartNetwork (
  echo Initializing network...
  call %__utl%:net-start
  echo Establishing network connection...
  if not defined _AuthParams set _AuthParams=10 5 20 "-169.254" 180
  call %__utl%:net-validate %_AuthParams%
) else (
  set ALTIRIS_DS_SERVER=
  set ALTIRIS_DS_SHARE=
  set ALTIRIS_SHARE=
  set ALTIRIS_SHARE_=
  set ALTIRIS_UNC=
  set share=
  set _AuthParams=
  set _Computer=
  set _Domain=
  set _PwlFile=
)
%__rtn%

REM ==============================================================
REM Login=Login to the network
REM --------------------------------------------------------------
:Login
if defined _StartNetwork if defined _Computer if defined _Domain if defined _PwlFile if exist "%systemdrive%\%_PwlFile%" (
  rem echo Authenticating network connection...
  call %__utl%:authenticate 180 "%systemdrive%\%_PwlFile%" "%_Computer%" "%_Domain%"
  rem if exist __rtn if %errorlevel% neq 0 %__rtn%
)
%__rtn%

REM ==============================================================
REM MapDrives=Map drives
REM --------------------------------------------------------------
:MapDrives
if defined _StartNetwork if exist "%systemdrive%\mapdrv.bat" (
  echo Mapping network drives...
  call "%systemdrive%\mapdrv.bat"
  rem if exist __rtn if %errorlevel% neq 0 %__rtn%
)
%__rtn%

REM ==============================================================
REM FindShare=Find the drive that is mapped to the share
REM Set the drive to the share
REM Set current directory to the share
REM --------------------------------------------------------------
:FindShare
if defined _StartNetwork if exist "%systemdrive%\mapdrv.bat" (
if defined ALTIRIS_SHARE_ call %__utl%:fnd-share "%ALTIRIS_DS_SERVER%" "%ALTIRIS_DS_SHARE%" ALTIRIS_SHARE ALTIRIS_UNC ALTIRIS_DS_SERVER ALTIRIS_DS_SHARE "%ALTIRIS_SHARE%" "%ALTIRIS_UNC%" "%ALTIRIS_DS_SERVER%" "%ALTIRIS_DS_SHARE%" ""
if not defined ALTIRIS_SHARE_ (
  echo No Deployment share mapped
) else if "%ALTIRIS_SHARE%" == "%ALTIRIS_SHARE_%:" (
  echo Deployment share mapped to %ALTIRIS_SHARE%
) else (
  echo Deployment share remapped from %ALTIRIS_SHARE_%: to %ALTIRIS_SHARE%
  set ALTIRIS_SHARE_=%ALTIRIS_SHARE:~0,1%
  set share=%ALTIRIS_SHARE%
  rem call %__utl%:set-temp %_work% %ALTIRIS_SHARE%
  set express=%ALTIRIS_SHARE%
)
)
rem Add the share to the path
if defined ALTIRIS_SHARE_ if exist "%ALTIRIS_SHARE%\" (
  path %path%;%ALTIRIS_SHARE%\
  cd /d %ALTIRIS_SHARE%
)
%__rtn%

REM ==============================================================
REM GetSrv=Find closest server, and map a drive to its share
REM --------------------------------------------------------------
:GetSrv
if defined _StartNetwork if defined _GetSrv_Map if defined _GetSrv_Pwl if exist "%_GetSrv_Lst%" if exist "%_GetSrv_Bat%" (
  echo Mapping drive to image server...
  call "%_GetSrv_Bat%" /s "%_GetSrv_Lst%" /m %_GetSrv_Map% /u "%_GetSrv_Pwl%" /d %_GetSrv_Dom%
)
%__rtn%

REM ==============================================================
REM UserApps=Load user applications from startup folder
REM --------------------------------------------------------------
:UserApps
dir /b /a-d "%systemdrive%\startup\*.*" >nul 2>nul
if not errorlevel 1 (
  echo Loading user startup applications...
  call %__utl%:startup "%systemdrive%\startup\*.*" "" wait echo call ""
)
%__rtn%

REM ==============================================================
REM CheckEnv=Make sure that the environment is ready to run the agent
REM --------------------------------------------------------------
:CheckEnv
if not defined temp set temp=%_work%
call %__utl%:set-temp "%temp%" "%ALTIRIS_SHARE%"
%__rtn%

REM ==============================================================
REM Agent=Start agent (as detached process)
REM --------------------------------------------------------------
:Agent
if defined _StartNetwork if defined _RunAgent if exist "%systemdrive%\runagent.bat" (
  call "%systemdrive%\runagent.bat" Start "%_RunAgentRemote%" %ALTIRIS_PROCESSOR% "%ALTIRIS_SHARE%" "%_Agent_Persistent%" "%_Agent_AutoUpdate%" "%_Agent_Lock%" %_Agent_Params%
  if defined _ScreenLock ("%systemdrive%\wndctl" -hide) else ("%systemdrive%\wndctl" -minimize)
)
%__rtn%

REM ==============================================================
REM UserPoststart=Load user poststart applications from poststart folder
REM --------------------------------------------------------------
:UserPoststart
dir /b /a-d "%systemdrive%\poststart\*.*" >nul 2>nul
if not errorlevel 1 (
  echo Loading user poststart applications...
  call %__utl%:startup "%systemdrive%\poststart\*.*" "" wait echo call ""
)
%__rtn%



REM ==============================================================
REM RDeploy=If this is an rdeploy imaging boot, then run RDeploy
REM --------------------------------------------------------------
:RDeploy
if defined _RunRDeploy (
  echo Starting RDeploy...
  "%ALTIRIS_SHARE%\rdeploy\windows\%ALTIRIS_PROCESSOR%\rdeploy.exe" %_RDClient% %_RDClientConfig%
  if defined _ScreenLock ("%systemdrive%\wndctl" -hide) else ("%systemdrive%\wndctl" -minimize)
)
%__rtn%





REM **************************************************************
REM **************************************************************
REM CustomActions=Add user custom actions here...
REM **************************************************************
REM **************************************************************
:CustomActions
if exist "%systemdrive%\startup.bat" (
  echo Calling custom actions...
  call "%systemdrive%\startup.bat" :CustomActions %*
)
%__rtn%




REM ==============================================================
REM Done=Finished
REM --------------------------------------------------------------
:done
call %__utl%:DeleteEnv "__"
if defined __DeleteVars call %__utl%:DeleteEnv "_"
set dbg=
echo.
echo Startup complete.
