#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
DERIVED_BASE="$ROOT_DIR/.deriveddata"
TARGET="${1:-all}"

build_objc() {
  xcodebuild \
    -project "$ROOT_DIR/QGVAPlayerDemo/QGVAPlayerDemo.xcodeproj" \
    -scheme "QGVAPlayerDemo" \
    -configuration Debug \
    -sdk iphoneos \
    -destination 'generic/platform=iOS' \
    -derivedDataPath "$DERIVED_BASE/objc" \
    IPHONEOS_DEPLOYMENT_TARGET=12.0 \
    CODE_SIGNING_ALLOWED=NO \
    build
}

build_swift() {
  xcodebuild \
    -project "$ROOT_DIR/QGVAPlayerDemoSwift/QGVAPlayerDemoSwift.xcodeproj" \
    -scheme "QGVAPlayerDemoSwift" \
    -configuration Debug \
    -sdk iphoneos \
    -destination 'generic/platform=iOS' \
    -derivedDataPath "$DERIVED_BASE/swift" \
    IPHONEOS_DEPLOYMENT_TARGET=14.5 \
    CODE_SIGNING_ALLOWED=NO \
    build
}

mkdir -p "$DERIVED_BASE/objc" "$DERIVED_BASE/swift"

case "$TARGET" in
  objc)
    build_objc
    ;;
  swift)
    build_swift
    ;;
  all)
    build_objc
    build_swift
    ;;
  *)
    echo "Usage: $0 [objc|swift|all]" >&2
    exit 1
    ;;
esac
