#!/bin/zsh
set -euo pipefail

# Determina diretório do projeto a partir do próprio script (sem paths hardcoded).
SCRIPT_DIR="$(cd "$(dirname "$0")" >/dev/null 2>&1 && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." >/dev/null 2>&1 && pwd)"

DEFAULT_OUTPUT_DIR="$PROJECT_DIR/builds/windows/quest-test"
OUTPUT_DIR="${1:-$DEFAULT_OUTPUT_DIR}"
UNITY_EXECUTABLE="${UNITY_EXECUTABLE:-}"

if [[ -z "$UNITY_EXECUTABLE" ]]; then
  echo "Env var obrigatória: UNITY_EXECUTABLE (path do executável Unity)."
  exit 1
fi

if [[ ! -x "$UNITY_EXECUTABLE" ]]; then
  echo "UNITY_EXECUTABLE não existe ou não é executável: $UNITY_EXECUTABLE"
  exit 1
fi

mkdir -p "$OUTPUT_DIR"

"$UNITY_EXECUTABLE" \
  -batchmode \
  -nographics \
  -projectPath "$PROJECT_DIR" \
  -executeMethod EventsAppQuest.Editor.WindowsBuild.QuestWindowsBuildEntry.BuildQuestWindows \
  -outputPath "$OUTPUT_DIR" \
  -logFile "$OUTPUT_DIR/windows-quest-build.log" \
  -quit \
  -buildTarget StandaloneWindows64

echo "Build Windows concluída. Output em: $OUTPUT_DIR (pasta)."
echo "Log Unity: $OUTPUT_DIR/windows-quest-build.log"

