@echo off
setlocal
title Backup User Accounts on %COMPUTERNAME%
color 0e

rem --- Prompt for offline backup
:prompt-offline
cls
echo.
set OFFLINE_BACKUP=n
set /p OFFLINE_BACKUP=Run offline? (y/n) [n]:
if [%OFFLINE_BACKUP%]==[y] goto set-offline
if [%OFFLINE_BACKUP%]==[Y] goto set-offline
if [%OFFLINE_BACKUP%]==[n] goto set-online
if [%OFFLINE_BACKUP%]==[N] goto set-online
goto prompt-offline

:set-offline
echo.
set /p OFFLINE_WIN_DIR=Enter the path to the Windows directory: 
set OFFLINE=/offlineWinDir:%OFFLINE_WIN_DIR%
goto prompt-backup-type

:set-online
set OFFLINE=""

:prompt-backup-type
cls
echo.
set BACKUP_TYPE=y
set /p BACKUP_TYPE=Backup all users? (y/n) [y]: 
if [%BACKUP_TYPE%]==[y] goto set-all-users
if [%BACKUP_TYPE%]==[Y] goto set-all-users
if [%BACKUP_TYPE%]==[n] goto get-domain
if [%BACKUP_TYPE%]==[N] goto get-domain
goto prompt-backup-type

:set-all-users
rem --- Set the store path to save migrated data into
set USMT_STORE=%~dp0..\usmt_store\%COMPUTERNAME%

rem --- Set the command line args for performing the migration
set SCANSTATE_USERS=/ue:*\Administrator /ue:*\ekadmin
set SCANSTATE_XML=/i:"%USMT_STORE%\config\MigUser.xml" /i:"%USMT_STORE%\config\MigDocSystem.xml" /i:"%USMT_STORE%\config\MigCustom.xml" %OFFLINE%
goto start

rem --- Get domain to backup
:get-domain
cls
echo.
set BACKUP_DOMAIN=%USERDOMAIN%
if [%BACKUP_DOMAIN%]==[] set BACKUP_DOMAIN=%COMPUTERNAME%
echo Enter the domain for the user account we'll be backing up.
set /p BACKUP_DOMAIN=(default is %BACKUP_DOMAIN%): 

rem --- Get user to backup
:get-user
cls
echo.
set BACKUP_USER=
set /p BACKUP_USER=Enter the username to backup: 
if [%BACKUP_USER%]==[] goto get-user

rem --- Set the store path to save migrated data into
set USMT_STORE=%~dp0..\usmt_store\%COMPUTERNAME%_%BACKUP_USER%

rem --- Set the command line args for performing the migration
set SCANSTATE_USERS=/ue:* /ui:%BACKUP_DOMAIN%\%BACKUP_USER%
set SCANSTATE_XML=/i:"%USMT_STORE%\config\MigUser.xml" /i:"%USMT_STORE%\config\MigCustom.xml" %OFFLINE%

:start
cls
rem --- Make sure we run the correct USMT architecture
set USMT_TOOLS=%~dp0x86
if exist "%ProgramFiles(x86)%" set USMT_TOOLS=%~dp0amd64

rem --- Create USMT_STORE directory if it doesn't exist
if not exist "%USMT_STORE%" mkdir "%USMT_STORE%"

rem --- Create USMT_LOG directory if it doesn't exist
if not exist "%USMT_STORE%\logs" mkdir "%USMT_STORE%\logs"

rem --- Create config directory if it doesn't exist
if not exist "%USMT_STORE%\config" mkdir "%USMT_STORE%\config"

rem --- Copy all XML files and restore script into USMT_STORE so the proper files and settings can be restored to the destination computer
echo.
echo Preparing USMT configuration...
copy /v /y "%USMT_TOOLS%\MigUser.xml" "%USMT_STORE%\config\" >nul
copy /v /y "%~dp0config\MigCustom.xml" "%USMT_STORE%\config\" >nul
copy /v /y "%~dp0config\MigDocSystem.xml" "%USMT_STORE%\config\" >nul
copy /v /y "%~dp0restore.bat" "%USMT_STORE%\" >nul

rem --- Copy source USMT binaries for restore operation
if not exist "%USMT_STORE%\tools" mkdir "%USMT_STORE%\tools"
if exist "%USMT_TOOLS%\..\amd64" xcopy /e /q /y "%USMT_TOOLS%\..\amd64" "%USMT_STORE%\tools\amd64\" >nul
if exist "%USMT_TOOLS%\..\x86" xcopy /e /q /y "%USMT_TOOLS%\..\x86" "%USMT_STORE%\tools\x86\" >nul
if exist "%USMT_TOOLS%\..\arm" xcopy /e /q /y "%USMT_TOOLS%\..\arm" "%USMT_STORE%\tools\arm\" >nul

rem --- Generate migration configuration file
echo.
echo Generating migration configuration...
"%USMT_TOOLS%\scanstate.exe" /genconfig:"%USMT_STORE%\config\config.xml" /l:"%USMT_STORE%\logs\config.log" %SCANSTATE_XML% /v:5

echo.
echo Performing migration...
rem --- Perform migration to %USMT_STORE%
"%USMT_TOOLS%\scanstate.exe" "%USMT_STORE%" /vsc /localonly /l:"%USMT_STORE%\logs\backup.log" /c %SCANSTATE_USERS% /config:"%USMT_STORE%\config\config.xml" %SCANSTATE_XML% /v:5 %1

echo.
echo Done!
echo Press any key to exit...
pause >nul
