"%BuildLab%") do (
    Set BuildLab=%%i
    REM --- If not empty, %%j is username of private builder (unused here)
)

set IsPhone=0
if /I "%EditionId%" EQU "MobileCore" ( 
    set IsPhone=1
)

:: If this is an ARM machine, replace ARM with WOA for buildArch
if %IsPhone% equ 0 (
    if /I "%PROCESSOR_ARCHITECTURE%" EQU "ARM" (
        Set BuildArch=woa%BuildArch:~3%
    )
)

Set BuildPath=%BuildLab%\%BuildString%\%BuildArch%

for /f "tokens=2,3* delims=/ " %%i in ('echo %date%') do set _DATETIME=%%k%%i%%j
if %IsPhone% equ 1 (
    for /f "tokens=1-3 delims=:. " %%i in ('echo %TIME%') do set _DATETIME=%_DATETIME%-%%i%%j%%k
) else (
    for /f "tokens=1-3 delims=:. " %%i in ('echo %time%') do set _DATETIME=%_DATETIME%-%%i%%j%%k
)

set nocab=0

if /I "%1" == "-nocab" (
    if %2!== "-outputpath" goto :EOF
    if %3!==! goto :EOF
    set fileoutputpath=%3
    set nocab=1
)

if /I "%1" == "-outputpath" (
    if %2!==! goto :EOF
    set fileoutputpath=%2
) else (
    if %IsPhone% equ 1 (
        set fileoutputpath=C:\data\test\Roamdiag.%_DATETIME%
    ) else (
        set fileoutputpath=%USERPROFILE%\Desktop\RoamDiag.%_DATETIME%
    )
)

if %nocab% equ 0 (
    set cabouput=RoamingLogs.%_DATETIME%.cab
    set caboutputpath=%fileoutputpath%
    set fileoutputpath=%fileoutputpath%\temp
)

set fileoutput="%fileoutputpath%\Diag-Roaming.txt"
if not exist %fileoutputpath% mkdir %fileoutputpath% 
::--------------------------------------
echo.
echo Saving trace and roamdiag
settingsynchost.exe -SaveTrace "%fileoutputpath%"
settingsynchost.exe -StartTrace
settingsynchost.exe -CaptureDiag "%fileoutputpath%"
echo Build path is %BuildPath% >> %fileoutput%
echo done.

::--------------------------------------
reg export HKLM\Software\Microsoft\Windows\CurrentVersion\SettingSync "%fileoutputpath%\settingsync_hklm.reg"
reg export HKLM\SOFTWARE\Microsoft\IdentityCRL "%fileoutputpath%\identitycrl.reg"
reg export HKLM\Software\Microsoft\Windows\CurrentVersion\Live "%fileoutputpath%\live_hklm.reg"

set default_app_sid=S-1-5-21-2702878673-795188819-444038987-2781
if %IsPhone% equ 0 (
    reg export HKCU\Software\Microsoft\Windows\CurrentVersion\Live "%fileoutputpath%\live_hkcu.reg"
    reg export HKCU\Software\Microsoft\Windows\CurrentVersion\OneDriveOptIn "%fileoutputpath%\skydriveoptin.reg"
    reg export HKCU\Software\Microsoft\Windows\CurrentVersion\SettingSync "%fileoutputpath%\settingsync_hkcu.reg"
) else (
    reg export HKU\%default_app_sid%\Software\Microsoft\Windows\CurrentVersion\Live "%fileoutputpath%\live_hku.reg"
    reg export HKU\%default_app_sid%\Software\Microsoft\Windows\CurrentVersion\OneDriveOptIn "%fileoutputpath%\skydriveoptin.reg"
    reg export HKU\%default_app_sid%\Software\Microsoft\Windows\CurrentVersion\SettingSync "%fileoutputpath%\settingsync_hku.reg"
)

:: Get policy regkeys. Check EnableLUA if UAC prompt is off, then AAD GetTicket will fail
reg export HKLM\Software\Policies\Microsoft\Windows\SettingSync "%fileoutputpath%\settingsync_policies.reg"
reg export HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System "%fileoutputpath%\systempolicies.reg"

echo. >> %fileoutput%
echo =========== RSP Provider info ================= >> %fileoutput%
reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\IdentityStore\Providers /s >> %fileoutput%

for /f "tokens=6 delims=\" %%a in ('reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\IdentityStore\Providers') do (
set ProviderGUID=%%a
)

for /f "tokens=3 delims= " %%a in ('reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\IdentityStore\Providers\%ProviderGUID%\SettingSync /v CollectionConfig') do (
set CollectionConfigGUID=%%a
) 

if [%CollectionConfigGUID%] neq [] (
    reg query HKCR\CLSID\%CollectionConfigGUID% /s >> %fileoutput%
)

for /f "tokens=3 delims= " %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\IdentityStore\Providers\%ProviderGUID%\SettingSync" /v SyncProvider') do (
set SyncProviderGUID=%%a
) 

if [%SyncProviderGUID%] neq [] (
    reg query HKCR\CLSID\%SyncProviderGUID% /s >> %fileoutput%
)

echo. >> %fileoutput%
::--------------------------------------
if %IsPhone% equ 1 ( 
    goto :PhoneEventLogs
) else (
    goto :WindowsEventLogs
)
::--------------------------------------
:WindowsEventLogs
call :ExportEventLogs Microsoft-Windows-SettingSync* %fileoutputpath%
call :ExportEventLogs Microsoft-Windows-Shell-Core* %fileoutputpath%
call :ExportEventLogs Microsoft-Windows-PushNotification-Platform* %fileoutputpath%
call :ExportEventLogs Microsoft-Windows-PushNotification-InProc* %fileoutputpath%
call :ExportEventLogs Microsoft-Windows-PushNotification-Developer* %fileoutputpath%
call :ExportEventLogs Microsoft-Windows-PackageStateRoaming* %fileoutputpath%
call :ExportEventLogs Microsoft-Windows-AppxDeployment-Server* %fileoutputpath%
call :ExportEventLogs Microsoft-Windows-AAD* %fileoutputpath%
call :ExportEventLogs Microsoft-Windows-OneBackup* %fileoutputpath%
call :ExportEventLogs Microsoft-Windows-CloudStore* %fileoutputpath%
copy /Y "%Windir%\Logs\SettingSync\SettingSync.etl" "%fileoutputpath%\SettingSync.backup.etl"
copy /Y "%Windir%\Logs\SettingSync\ThemeUI.etl" "%fileoutputpath%\ThemeUI.backup.etl"

if %nocab% equ 0 (
    call :CABIT %fileoutputpath%
    if exist %fileoutputpath%\%cabouput% (
        move /y "%fileoutputpath%\%cabouput%" "%caboutputpath%\." 2>&1>NUL
        rd /s /q "%fileoutputpath%"

        echo Logs in %caboutputpath%\%cabouput%
    )
)

goto :EOF
::--------------------------------
:ExportEventLogs
    for /f usebackq %%g in (`wevtutil el ^| findstr /i "%1"`) do (
      set var01=%%g
      set var02=!var01:/=%!
      wevtutil epl %%g %2\!var02!.evtx
      if not errorlevel 0 (
        copy /Y %SystemRoot%\System32\winevt\Logs\%1 %2
      )
    )
GOTO :EOF
::-----------------------------------
:PhoneEventLogs
tracelog -flush selfhostcritical
copy /Y C:\data\systemdata\etw\selfhostcritical.etl* %fileoutputpath%
goto :EOF

REM -----------
REM * CAB IT! *
REM -----------
:CABIT

set DIRECTIVEFILE="%1\Schema.ddf"
set TARGET="%1"
set TEMPFILE="%1\TEMP"

if not exist %TARGET% (
    echo %TARGET% does not exist.
    goto :EOF
)

pushd %TARGET%

echo. > %DIRECTIVEFILE%
echo .set CabinetNameTemplate=%cabouput% >> %DIRECTIVEFILE%
echo .set DiskDirectoryTemplate= >> %DIRECTIVEFILE%
echo .set InfFileName=%TEMPFILE% >> %DIRECTIVEFILE%
echo .set RptFileName=%TEMPFILE% >> %DIRECTIVEFILE%
echo .set MaxDiskSize=0 >> %DIRECTIVEFILE%
echo .set CompressionType=LZX >> %DIRECTIVEFILE%

call :CAB_DIR .

MakeCab /f %DIRECTIVEFILE% 2>&1>NUL

del /f %DIRECTIVEFILE%
set TEMPFILE="%1\TEMP"

popd

goto :EOF

REM CAB Helper
:CAB_DIR
echo .set DestinationDir=%1 >> %DIRECTIVEFILE%
for /f %%i in ('dir /b /a:-d %1') do (
    echo %1\%%i >> %DIRECTIVEFILE%
)
for /f %%i in ('dir /b /a:d %1') do (
    call :CAB_DIR %1\%%i
)
goto :EOF
