@REM
@REM Script to start OOBE tracing
@REM
@REM  Usage:
@REM      start_oobe_tracing.cmd (-level [0|1|2]) ([-online|-offline]) (-reboot) (-autostop) (-postoobedelay <minutes>)
@REM
@REM  PARAMETER -level [0|1|2]
@REM      specify ETL tracing level, default level is 0
@REM          -level 0 : minimal tracing level
@REM          -level 1 : timing tracing level
@REM          -level 2 : analysis tracing level
@REM
@REM  PARAMETER [-online|-offline]
@REM      specify online or offline injection of autologgers, default is online
@REM          -offline : load system hive before injecting the registry key of autologgers and unload after injection
@REM
@REM  PARAMETER -reboot
@REM      specify reboot or not at the end of script, default is not reboot
@REM
@REM  PARAMETER -autostop
@REM      specify auto stop tracing after OOBE or not, default is not autostop
@REM          -autostop : this parameter is only effective under online
@REM
@REM  PARAMETER -postoobedelay <minutes>
@REM      specify post oobe delay in minutes
@REM          -postoobedelay : this parameter will change the -autostop scheduled task post logon delay
@REM                           only effective under autostop
@REM

@echo off

SETLOCAL EnableDelayedExpansion
REM -level default = 0
set level=0
REM default online
set online=online
REM reboot default 0 (not reboot)
set reboot=0
REM -autostop default = 0
set autostop=0
REM -postoobedelay default = 5
set postoobedelay=5

if "%~1" == "" (
    goto usage
)

:start_loop
REM shift_args default = 0
set shift_args=0

if "%~1" == "" (
    REM no more arguments
    goto end_loop
)

REM start_level
if /I "%~1" == "-level" (
    if "%~2" == "" (
        goto usage
    )
    if "%~2" == "0" (
        set level=0
        set shift_args=2
        goto end_level
    )
    if "%~2" == "1" (
        set level=1
        set shift_args=2
        goto end_level
    )
    if "%~2" == "2" (
        set level=2
        set shift_args=2
        goto end_level
    )
    goto usage
)
:end_level

REM start_online
if /I "%~1" == "-online" (
    set online=online
    set shift_args=1
)
:end_online

REM start_offline
if /I "%~1" == "-offline" (
    set online=offline
    set shift_args=1
)
:end_offline

REM start_reboot
if /I "%~1" == "-reboot" (
    set reboot=1
    set shift_args=1
)
:end_reboot

REM start_autostop
if /I "%~1" == "-autostop" (
    set autostop=1
    set shift_args=1
)
:end_autostop

REM start_postoobedelay
if /I "%~1" == "-postoobedelay" (
    if "%~2" == "" (
        goto usage
    )
    set postoobedelay=%~2
    set shift_args=2
)
:end_postoobedelay

if !shift_args! EQU 0 (
    @echo "%~1" is an unknown argument
    goto usage
)
if !shift_args! GEQ 1 (
    shift
)
if !shift_args! GEQ 2 (
    shift
)
goto start_loop

:end_loop
call :setregfile
if "!online!" == "offline" (
    for %%p in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do if exist %%p:\windows\system32\write.exe set WINDOWSDRIVE=%%p
    reg load HKLM\LoadedHive !WINDOWSDRIVE!:\Windows\System32\Config\SYSTEM
)
regedit /s "!reg_file!"
if "!online!" == "offline" (
    reg unload HKLM\LoadedHive

    if !reboot! EQU 1 (
        wpeutil shutdown
    )
)
if "!online!" == "online" (
    if !autostop! GEQ 1 (
        set xml_file=%~dp0StopTracingTask.xml
        set path_to_cmd=%~dp0stop_autologger.cmd
        powershell -Command "(Get-Content '!xml_file!') | Foreach-Object { $_ -replace 'Stop tracing for OOBE 5 minutes after logon', 'Stop tracing for OOBE !postoobedelay! minutes after logon' -replace '<Delay>PT5M</Delay>', '<Delay>PT!postoobedelay!M</Delay>' -replace 'path to stop_autologger.cmd', '!path_to_cmd!' } | Out-File '!xml_file!'"
        schtasks /create /tn "OOBE AutoStop Tracing" /xml "!xml_file!"
    )

    if !reboot! EQU 1 (
        shutdown /r /t 0
    )
)
goto end

:usage
@echo Usage:
@echo.    start_oobe_tracing.cmd ^(-level [0^|1^|2]^) ^([-online^|-offline]^) ^(-reboot^) ^(-autostop^) ^(-postoobedelay ^<minutes^>^)
goto end

:setregfile
set reg_file=%~dp0config\add_autologger_!level!_!online!.reg
exit /b

:end
