@echo off
::
:: UltraDefrag - a powerful disk defragmenter for Windows.
::
:: bt.cmd - enqueues a command to be executed at the next
::          system reboot using the boot time interface
::          of UltraDefrag. Optionally reboots the computer
::          after enqueuing the command.
::
:: Copyright (c) 2021 Green Gate Systems, LLC.
::
:: Synopsis:
::
::   bt [options] <command>
::
:: Options:
::
::   /E - enqueue the command without asking for confirmation
::   /R - enqueue the command and reboot the computer, without
::        asking for confirmation
:: 
:: Examples:
::
::   bt udefrag c: d:
::   bt /E udefrag -o c:
::   bt /R call %SystemRoot%\system32\ud-boot-time-custom.cmd
::

setlocal

:parse_command_line
if /i '%1' neq '/E' if /i '%1' neq '/R' goto check_command

if /i '%1' equ '/E' set ENQUEUE_THE_COMMAND=1
if /i '%1' equ '/R' set REBOOT_THE_COMPUTER=1

shift
goto parse_command_line

:check_command
if '%1' equ '' (
    echo No command specified!
    goto fail
)

if '%ENQUEUE_THE_COMMAND%' equ '1' goto build_command
if '%REBOOT_THE_COMPUTER%' equ '1' goto build_command

echo Boot time defragmentation requires a system reboot.
choice /c RLC /m "R - Reboot now, L - Reboot later, C - Cancel"
if '%ERRORLEVEL%' equ '3' goto success
if '%ERRORLEVEL%' equ '1' set REBOOT_THE_COMPUTER=1

:build_command
set BOOT_TIME_DEFRAG_COMMAND=%BOOT_TIME_DEFRAG_COMMAND% %1
shift
if '%1' neq '' goto build_command

call :generate_boot_time_script %BOOT_TIME_DEFRAG_COMMAND% > ud-boot-time.script || goto fail
powershell -c "Get-Content ud-boot-time.script | Set-Content -Encoding Unicode %SystemRoot%\system32\ud-boot-time.cmd" || goto fail
del /q ud-boot-time.script

:: enable the boot time defragmentation
call "%SystemRoot%\system32\boot-on.cmd" || goto fail

:: reboot the computer
if '%REBOOT_THE_COMPUTER%' neq '1' goto success
shutdown -r -t 0 || goto fail

:success
endlocal
exit /B 0

:fail
endlocal
exit /B 1

:generate_boot_time_script
    echo @echo off
    echo :: this script has been generated by the bt.cmd script
    echo.

    if /i '%1' equ 'call' (
        echo %*
    ) else (
        echo :: turn the boot time defragmentation off for subsequent boots
        echo boot-off
        echo.
        echo :: set UltraDefrag specific environment variables
        echo set UD_CLEAN_TEMP_FILES=%UD_CLEAN_TEMP_FILES%
        echo set UD_IN_FILTER=%UD_IN_FILTER%
        echo set UD_EX_FILTER=%UD_EX_FILTER%
        echo set UD_FRAGMENT_SIZE_THRESHOLD=%UD_FRAGMENT_SIZE_THRESHOLD%
        echo set UD_FILE_SIZE_THRESHOLD=%UD_FILE_SIZE_THRESHOLD%
        echo set UD_FRAGMENTS_THRESHOLD=%UD_FRAGMENTS_THRESHOLD%
        echo set UD_SORTING=%UD_SORTING%
        echo set UD_SORTING_ORDER=%UD_SORTING_ORDER%
        echo set UD_OPTIMIZER_FILE_SIZE_THRESHOLD=%UD_OPTIMIZER_FILE_SIZE_THRESHOLD%
        echo set UD_FRAGMENTATION_THRESHOLD=%UD_FRAGMENTATION_THRESHOLD%
        echo set UD_TIME_LIMIT=%UD_TIME_LIMIT%
        echo set UD_REFRESH_INTERVAL=%UD_REFRESH_INTERVAL%
        echo set UD_DISABLE_REPORTS=%UD_DISABLE_REPORTS%
        echo set UD_DBGPRINT_LEVEL=%UD_DBGPRINT_LEVEL%
        echo set UD_DRY_RUN=%UD_DRY_RUN%
        echo set UD_LOG_FILE_PATH=%UD_LOG_FILE_PATH%
        echo.
        echo :: perform the task
        echo %*
        echo.
        echo exit
    )
exit /B 0
