@echo off
cls
setlocal EnableExtensions EnableDelayedExpansion

echo *************************************************************************************
echo ** Unzip Rapture Kit download files into: RAPTURE_KIT folder
echo *************************************************************************************

rem --- Paths ---
set "DESKTOP_PATH=%USERPROFILE%\Desktop"
set "DOWNLOADS_PATH=%USERPROFILE%\Downloads"
set "DEST_DESKTOP=%DESKTOP_PATH%\RAPTURE_KIT"
set "DEST_DOWNLOADS=%DOWNLOADS_PATH%\RAPTURE_KIT"
set "SRC_DIR=%DOWNLOADS_PATH%"

rem --- Ensure tar is available (Windows 10+ typically has it) ---
where tar >nul 2>&1
if errorlevel 1 (
  echo ERROR: 'tar' command not found on this system. Please install/enable tar and try again.
  pause
  exit /b 1
)

rem --- Primary destination: Desktop ---
if exist "%DEST_DESKTOP%" (
  echo ERROR: The folder "%DEST_DESKTOP%" already exists.
  echo Please rename or delete it BEFORE running this script again.
  pause
  exit /b 1
)

echo.
echo Creating destination: "%DEST_DESKTOP%"
mkdir "%DEST_DESKTOP%" >nul 2>&1
if errorlevel 1 (
  echo ERROR: Could not create "%DEST_DESKTOP%".
  echo Please check permissions and try again.
  pause
  exit /b 1
)

call :ExtractTo "%SRC_DIR%" "%DEST_DESKTOP%"
if defined EXTRACT_FAILED (
  echo.
  echo -------------------------------------------------------------------------------------
  echo WARNING: Primary extraction to Desktop failed. Attempting fallback to Downloads...
  echo -------------------------------------------------------------------------------------

  rem --- Fallback destination: Downloads ---
  if exist "%DEST_DOWNLOADS%" (
    echo ERROR: The folder "%DEST_DOWNLOADS%" already exists.
    echo Please rename or delete it BEFORE running this script again.
    pause
    exit /b 1
  )

  echo Creating destination: "%DEST_DOWNLOADS%"
  mkdir "%DEST_DOWNLOADS%" >nul 2>&1
  if errorlevel 1 (
    echo ERROR: Could not create "%DEST_DOWNLOADS%".
    echo Please check permissions and try again.
    pause
    exit /b 1
  )

  set "EXTRACT_FAILED="
  call :ExtractTo "%SRC_DIR%" "%DEST_DOWNLOADS%"
  if defined EXTRACT_FAILED (
    echo.
    echo Extraction failed in both locations. See errors above.
    pause
    exit /b 1
  ) else (
    echo.
    echo *************************************************************************************
    echo Fallback succeeded. The Rapture Kit has been extracted to:
    echo     "%DEST_DOWNLOADS%"
    echo *************************************************************************************
    pause
    exit /b 0
  )
) else (
  echo.
  echo *************************************************************************************
  echo Extraction succeeded. The Rapture Kit has been extracted to:
  echo     "%DEST_DESKTOP%"
  echo *************************************************************************************
  pause
  exit /b 0
)

rem =========================
rem Subroutine: :ExtractTo
rem Args:
rem   %1 -> Source directory where zip parts reside
rem   %2 -> Destination directory to extract into
rem Sets:
rem   EXTRACT_FAILED=1 if any missing file or tar error occurs
rem =========================
:ExtractTo
setlocal EnableDelayedExpansion
set "SRC=%~1"
set "DEST=%~2"
set "ANY_WARN="
set "ANY_ERR="

echo.
echo Extracting from: "!SRC!"
echo Into         : "!DEST!"
echo.

for /L %%I in (1,1,9) do (
  set "ZIP=Rapture_Kit_3.1-Part_%%I_of_9.zip"
  set "ZIP_PATH=!SRC!\!ZIP!"

  if exist "!ZIP_PATH!" (
    echo Extracting: !ZIP!
    tar -xf "!ZIP_PATH!" -C "!DEST!"
    if errorlevel 1 (
      echo   ERROR: Extraction failed for "!ZIP_PATH!"
      set "ANY_ERR=1"
    ) else (
      echo   OK
    )
  ) else (
    echo WARNING: Missing file: "!ZIP_PATH!"
    set "ANY_WARN=1"
  )
)

endlocal & (
  if defined ANY_WARN set "EXTRACT_FAILED=1"
  if defined ANY_ERR set "EXTRACT_FAILED=1"
)
exit /b
