#!/bin/bash
set -e

# Optional: Check for a Gitea runner-specific environment variable.
# You can set a variable like GITEA_RUNNER in your runner configuration.
if [ -z "$GITEA_RUNNER" ]; then
    echo "Warning: This script does not appear to be running in a Gitea runner environment."
fi

# Get the user's home directory (if needed)
USER_HOME="$HOME"

# Set up Unreal Engine paths for Windows
# Adjust the UE_ROOT path if your installation differs.
UE_ROOT="E:/Games/UE_5.5/"
UE_EDITOR="$UE_ROOT/Engine/Binaries/Win64/UnrealEditor.exe"
UE_UAT="$UE_ROOT/Engine/Build/BatchFiles/RunUAT.bat"

# Set up project paths based on the current working directory
PROJECT_ROOT="$(pwd)"
PROJECT_FILE="$PROJECT_ROOT/Luckyrobots.uproject"
ARCHIVE_DIR="$PROJECT_ROOT/Builds/Windows"

# Create the archive directory if it does not exist
mkdir -p "$ARCHIVE_DIR"

echo "Starting Windows build of Luckyrobots via Gitea Runner..."

# Run the build command using Unreal's Automation Tool (UAT)
"$UE_UAT" -ScriptsForProject="$PROJECT_FILE" Turnkey \
  -command=VerifySdk \
  -platform=Win64 \
  -UpdateIfNeeded \
  -EditorIO \
  -EditorIOPort=59484 \
  -project="$PROJECT_FILE" \
  BuildCookRun \
  -nop4 \
  -utf8output \
  -cook \
  -project="$PROJECT_FILE" \
  -target=Luckyrobots \
  -unrealexe="$UE_EDITOR" \
  -platform=Win64 \
  -installed \
  -stage \
  -archive \
  -package \
  -build \
  -iterativecooking \
  -pak \
  -iostore \
  -compressed \
  -prereqs \
  -archivedirectory="$ARCHIVE_DIR" \
  -CrashReporter \
  -clientconfig=Shipping
  # Uncomment the following lines if you wish to test the build without pak and iostore:
  # -skipiostore \
  # -skippak