@echo off
setlocal

set "PROJECT_DIR=%~dp0.."
set "PROJECT_FILE=%PROJECT_DIR%\AgrarianGame.uproject"
set "LOG_DIR=%PROJECT_DIR%\Saved\BuildLogs"
set "LOG_FILE=%LOG_DIR%\BuildEditor-Windows.log"
set "UE_ROOT=C:\Program Files\Epic Games\UE_5.7"
set "BUILD_BAT=%UE_ROOT%\Engine\Build\BatchFiles\Build.bat"

if not exist "%LOG_DIR%" mkdir "%LOG_DIR%"

if not exist "%BUILD_BAT%" (
  echo Unreal Engine 5.7 was not found at:
  echo %UE_ROOT%
  echo.
  echo Edit UE_ROOT in this script if your engine is installed somewhere else.
  if not defined AGRARIAN_NO_PAUSE pause
  exit /b 1
)

echo Building AgrarianGameEditor with UnrealBuildTool...
echo Log: %LOG_FILE%
call "%BUILD_BAT%" AgrarianGameEditor Win64 Development -Project="%PROJECT_FILE%" -WaitMutex -architecture=x64 -NoUBA > "%LOG_FILE%" 2>&1
set "BUILD_EXIT_CODE=%ERRORLEVEL%"

type "%LOG_FILE%"

if not "%BUILD_EXIT_CODE%"=="0" (
  echo.
  echo Build failed with exit code %BUILD_EXIT_CODE%.
  echo Log file: %LOG_FILE%
  echo.
  if not defined AGRARIAN_NO_PAUSE pause
  exit /b %BUILD_EXIT_CODE%
)

echo.
echo Build completed successfully.
echo Log file: %LOG_FILE%
echo.
if not defined AGRARIAN_NO_PAUSE pause
exit /b 0
