#!/usr/bin/env bash

set -eu

TOOLCHAIN=~/Library/Developer/Toolchains/swift-latest.xctoolchain/Info.plist
if [ ! -f "${TOOLCHAIN}" ]; then
  TOOLCHAIN=/Library/Developer/Toolchains/swift-latest.xctoolchain/Info.plist
fi

if [ ! -f "${TOOLCHAIN}" ]; then
  echo "error: unable to find any toolchain named swift-latest.xctoolchain"
  exit 1
fi

bundle_id=$(/usr/libexec/PlistBuddy -c "print :CFBundleIdentifier" "${TOOLCHAIN}")
TOOLCHAINS="${bundle_id}" exec "$@"
