#!/usr/bin/env bash
set -eu

if ! command -v clang-cl >/dev/null 2>&1; then
  echo "clang-cl is required for Windows cross-builds on Linux."
  echo "Install LLVM/Clang toolchain first, for example on Ubuntu:"
  echo "  sudo apt update"
  echo "  sudo apt install -y clang lld llvm"
  exit 1
fi

SELF_DIR="$(cd "$(dirname "$0")" && pwd)"
REAL_CARGO_XWIN=""

IFS=':'
for dir in ${PATH}; do
  if [ -z "${dir}" ] || [ "${dir}" = "${SELF_DIR}" ]; then
    continue
  fi

  if [ -x "${dir}/cargo-xwin" ]; then
    REAL_CARGO_XWIN="${dir}/cargo-xwin"
    break
  fi
done
unset IFS

if [ -z "${REAL_CARGO_XWIN}" ]; then
  echo "cargo-xwin binary was not found outside ${SELF_DIR}."
  echo "Install it with:"
  echo "  cargo install --locked cargo-xwin"
  exit 1
fi

exec "${REAL_CARGO_XWIN}" "$@"
