@echo off
setlocal
chcp 65001 >nul
cd /d "%~dp0"

if defined RESTORE_OUTPUT_DIR (
    set "BACKUP_DIR=%RESTORE_OUTPUT_DIR%\_partial_backups"
) else (
    set "BACKUP_DIR=pack\restored\_partial_backups"
)

for %%I in ("%BACKUP_DIR%") do set "BACKUP_DIR_FULL=%%~fI"

if not exist "%BACKUP_DIR%" (
    echo [INFO] No old backup directory was found.
    call :maybe_pause
    exit /b 0
)

echo [WARN] The following directory will be deleted permanently:
echo        %BACKUP_DIR_FULL%
echo.
set /p ANSWER=Type YES and press Enter to continue: 

if /I not "%ANSWER%"=="YES" (
    echo [INFO] Delete cancelled.
    call :maybe_pause
    exit /b 0
)

rmdir /s /q "%BACKUP_DIR%"

if exist "%BACKUP_DIR%" (
    echo [ERROR] Delete failed. The directory may still be in use.
    call :maybe_pause
    exit /b 1
)

echo [DONE] Old backups have been deleted.
call :maybe_pause
exit /b 0

:maybe_pause
if defined RESTORE_NO_PAUSE exit /b 0
pause
exit /b 0
