#!/bin/zsh
set -o pipefail  # Fail if xcodebuild fails, even with xcbeautify

# --- Configuration ---
TARGET_NAME="AtTable"
DEVICE_NAME="iPhone 17 Pro"
BUILD_PATH="$(pwd)/build"

echo "🔍 Checking compilation for $TARGET_NAME..."

# Build Only (No Install/Launch)
# Uses -target instead of -scheme to bypass potential scheme misconfigurations
# Explicitly unsets CC/CXX/LIBCLANG_PATH to avoid environment pollution
# Also overrides them in xcodebuild arguments to ensure Xcode uses default toolchain
# Uses SYMROOT instead of -derivedDataPath because -derivedDataPath requires -scheme
env -u CC -u CXX -u LIBCLANG_PATH xcodebuild \
  -target "$TARGET_NAME" \
  -sdk iphonesimulator \
  -destination "platform=iOS Simulator,name=$DEVICE_NAME" \
  -configuration Debug \
  SYMROOT="$BUILD_PATH" \
  CC=clang CXX=clang++ LIBCLANG_PATH= \
  build | xcbeautify

# Check exit code of the pipeline
if [ $? -eq 0 ]; then
    echo "✅ Build Succeeded. No errors found."
else
    echo "❌ Build Failed."
    exit 1
fi
