@echo off
setlocal EnableExtensions

rem All Office Deployment Tool files are stored beside this batch file.
set "deployment_dir=%~dp0"
set "odt_setup=%deployment_dir%setup.exe"
set "publisher_xml=%deployment_dir%step-1-install-only-publisher-2021-retail.xml"
set "m365_xml=%deployment_dir%step-2-install-microsoft-365-without-publisher.xml"

title Publisher 2021 then Microsoft 365 Installer

rem Administrator rights are required by the Office Deployment Tool.
net session >nul 2>&1
if errorlevel 1 (
  echo.
  echo ERROR: Administrator rights are required.
  echo Right-click START-INSTALLATION.bat in the main folder and select
  echo "Run as administrator".
  echo.
  pause
  exit /b 1
)

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

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

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

echo.
echo ================================================================
echo STEP 1 OF 2: Installing Publisher 2021 Retail
echo ================================================================
echo Close all Microsoft Office applications if prompted.
echo.

"%odt_setup%" /configure "%publisher_xml%"
set "publisher_result=%errorlevel%"

if not "%publisher_result%"=="0" (
  echo.
  echo ERROR: Publisher 2021 did not install successfully.
  echo Office Deployment Tool exit code: %publisher_result%
  echo Microsoft 365 installation has been cancelled.
  echo.
  pause
  exit /b %publisher_result%
)

echo.
echo Publisher 2021 completed successfully.
echo.
echo ================================================================
echo STEP 2 OF 2: Installing Microsoft 365 without Publisher
echo ================================================================
echo.

"%odt_setup%" /configure "%m365_xml%"
set "m365_result=%errorlevel%"

if not "%m365_result%"=="0" (
  echo.
  echo ERROR: Microsoft 365 did not install successfully.
  echo Office Deployment Tool exit code: %m365_result%
  echo Publisher 2021 was completed, but Step 2 needs attention.
  echo.
  pause
  exit /b %m365_result%
)

echo.
echo ================================================================
echo SUCCESS
echo ================================================================
echo Publisher 2021 was installed first.
echo Microsoft 365 was then installed with Publisher excluded.
echo.
pause
exit /b 0
