@echo off
setlocal EnableExtensions
title Publisher 2021 and Microsoft 365 - 32-bit Installation

set "deployment_dir=%~dp0"
set "setup_file=%deployment_dir%setup.exe"
set "publisher_xml=%deployment_dir%step-1-install-only-publisher-2021-retail-32bit.xml"
set "m365_xml=%deployment_dir%step-2-install-microsoft-365-without-publisher-32bit.xml"

net session >nul 2>&1
if errorlevel 1 (
  echo.
  echo ERROR: Administrator permission is required.
  echo Right-click START-INSTALLATION-32BIT.bat and select "Run as administrator".
  pause
  exit /b 1
)

if not exist "%setup_file%" (
  echo.
  echo ERROR: setup.exe from the Microsoft Office Deployment Tool is missing.
  echo Copy setup.exe into this folder:
  echo "%deployment_dir%"
  pause
  exit /b 2
)

if not exist "%publisher_xml%" (
  echo.
  echo ERROR: The Step 1 Publisher configuration file is missing.
  echo Expected file:
  echo "%publisher_xml%"
  pause
  exit /b 3
)

if not exist "%m365_xml%" (
  echo.
  echo ERROR: The Step 2 Microsoft 365 configuration file is missing.
  echo Expected file:
  echo "%m365_xml%"
  pause
  exit /b 4
)

echo.
echo ================================================================
echo STEP 1 OF 2: Installing 32-bit Publisher 2021 Retail only
echo ================================================================
echo.
"%setup_file%" /configure "%publisher_xml%"
set "publisher_result=%errorlevel%"

if not "%publisher_result%"=="0" (
  echo.
  echo ERROR: Publisher installation failed with exit code %publisher_result%.
  echo Microsoft 365 installation has not been started.
  pause
  exit /b %publisher_result%
)

echo.
echo Publisher 2021 completed successfully.
echo.
echo ================================================================
echo STEP 2 OF 2: Installing 32-bit Microsoft 365 without Publisher
echo ================================================================
echo.
"%setup_file%" /configure "%m365_xml%"
set "m365_result=%errorlevel%"

if not "%m365_result%"=="0" (
  echo.
  echo ERROR: Microsoft 365 installation failed with exit code %m365_result%.
  pause
  exit /b %m365_result%
)

echo.
echo SUCCESS: 32-bit Publisher 2021 and Microsoft 365 were installed.
echo Publisher came from Office Professional Plus 2021 Retail, and the
echo Microsoft 365 installation was configured without Publisher.
echo.
pause
exit /b 0
