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

script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
if [[ $# -ne 1 ]]; then
  echo "usage: $0 <ios|tvos|ios,tvos>" >&2
  exit 64
fi
case "$1" in
  ios|tvos|ios,tvos) ;;
  *)
    echo "platforms must be ios, tvos, or ios,tvos" >&2
    exit 64
    ;;
esac
export BEAM_VLCKIT_PLATFORMS=$1
# shellcheck source=common.sh
source "$script_dir/common.sh"

beam_assert_work_dir
trap beam_release_canonical_lock EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
beam_acquire_canonical_lock
for command in git python3 shasum strings nm otool dwarfdump plutil xcodebuild xcrun; do
  beam_require_command "$command"
done
[[ "$(uname -s)" == Darwin ]] || beam_die "Apple VLCKit must be built on macOS"

free_gib=$(beam_free_gib "$beam_vlckit_build_root")
if (( free_gib < BEAM_PREFLIGHT_FREE_GIB )); then
  beam_die "at least $BEAM_PREFLIGHT_FREE_GIB GiB free is required at $beam_vlckit_build_root; found ${free_gib} GiB"
fi

xcode_version=$(xcodebuild -version | awk 'NR == 1 {print $2}')
xcode_build=$(xcodebuild -version | awk 'NR == 2 {print $3}')
[[ "$xcode_version" == "$BEAM_XCODE_VERSION" ]] || \
  beam_die "Xcode $BEAM_XCODE_VERSION is pinned; selected version is $xcode_version"
[[ "$xcode_build" == "$BEAM_XCODE_BUILD" ]] || \
  beam_die "Xcode build $BEAM_XCODE_BUILD is pinned; selected build is $xcode_build"

if beam_platform_selected ios; then
  ios_sdk=$(xcrun --sdk iphoneos --show-sdk-version)
  [[ "$ios_sdk" == "$BEAM_IOS_SDK_VERSION" ]] || \
    beam_die "iOS SDK $BEAM_IOS_SDK_VERSION is pinned; selected SDK is $ios_sdk"
fi
if beam_platform_selected tvos; then
  tvos_sdk=$(xcrun --sdk appletvos --show-sdk-version)
  [[ "$tvos_sdk" == "$BEAM_TVOS_SDK_VERSION" ]] || \
    beam_die "tvOS SDK $BEAM_TVOS_SDK_VERSION is pinned; selected SDK is $tvos_sdk"
fi

if [[ ! -d "$beam_vlckit_source_dir" ]]; then
  if [[ ${BEAM_PREFLIGHT_ALLOW_UNPREPARED:-0} == 1 ]]; then
    echo "Source destination checks deferred until prepare_sources.sh completes."
    exit 0
  fi
  beam_die "prepared VLCKit source is missing; run prepare_sources.sh first"
fi

# Match the patched archive command exactly enough to catch an SDK that exists
# on disk but has no eligible generic destination. A simulator runtime is not
# required when Xcode can resolve the generic archive destination.
check_destination() {
  local project=$1
  local scheme=$2
  local sdk=$3
  local destination=$4
  local deployment_setting=$5
  local deployment_value=$6
  local derived="$beam_vlckit_work_dir/preflight-derived/$scheme-$sdk"
  rm -rf "$derived"
  if ! xcodebuild \
      -project "$beam_vlckit_source_dir/$project.xcodeproj" \
      -scheme "$scheme" \
      -configuration Release \
      -sdk "$sdk" \
      -destination "$destination" \
      -derivedDataPath "$derived" \
      ARCHS=arm64 \
      ONLY_ACTIVE_ARCH=NO \
      CODE_SIGNING_ALLOWED=NO \
      CODE_SIGNING_REQUIRED=NO \
      ENABLE_BITCODE=NO \
      "$deployment_setting=$deployment_value" \
      -showBuildSettings >/dev/null; then
    beam_die "Xcode cannot resolve $scheme with -sdk $sdk -destination '$destination'"
  fi
}

if beam_platform_selected ios; then
  check_destination MobileVLCKit MobileVLCKit iphoneos \
    'generic/platform=iOS' IPHONEOS_DEPLOYMENT_TARGET "$BEAM_IOS_DEPLOYMENT_TARGET"
  check_destination MobileVLCKit MobileVLCKit iphonesimulator \
    'generic/platform=iOS Simulator' IPHONEOS_DEPLOYMENT_TARGET "$BEAM_IOS_DEPLOYMENT_TARGET"
fi
if beam_platform_selected tvos; then
  check_destination MobileVLCKit TVVLCKit appletvos \
    'generic/platform=tvOS' TVOS_DEPLOYMENT_TARGET "$BEAM_TVOS_DEPLOYMENT_TARGET"
  check_destination MobileVLCKit TVVLCKit appletvsimulator \
    'generic/platform=tvOS Simulator' TVOS_DEPLOYMENT_TARGET "$BEAM_TVOS_DEPLOYMENT_TARGET"
fi

rm -rf "$beam_vlckit_work_dir/preflight-derived"
echo "Apple VLCKit preflight passed for $beam_vlckit_platforms with ${free_gib} GiB free."
