#!/usr/bin/env sh

echo "$@" >> /tmp/xcrun-$$.log

# We could not care less, luckily this always comes first
if [[ ${1} == --sdk || ${1} == -sdk ]]; then
  shift;
  shift;
fi

case ${1} in

  -find)
    if which ${HOST}-${2} > /dev/null 2>&1; then
      echo "which ${HOST}-${2}" >> /tmp/xcrun-$$.log
      which ${HOST}-${2} >> /tmp/xcrun-$$.log
      which ${HOST}-${2}
      exit 0
    elif which ${2} > /dev/null 2>&1; then
      echo "which ${2}" >> /tmp/xcrun-$$.log
      which ${2} >> /tmp/xcrun-$$.log
      which ${2}
      exit 0
    else
      echo "ERROR :: Fake xcrun failed to locate ${2}" >> /tmp/xcrun-$$.log
      echo "ERROR :: Fake xcrun failed to locate ${2}"
      exit 1
    fi
    ;;

  --show-sdk-path)
    if [[ -n ${CONDA_BUILD_SYSROOT} ]]; then
      echo ${CONDA_BUILD_SYSROOT} >> /tmp/xcrun-$$.log
      echo ${CONDA_BUILD_SYSROOT}
      exit 0
    fi
    echo "TODO :: Implement fallback for xcrun --show-sdk-path"
    exit 1
    ;;

esac

if [[ -n ${HOST} ]] && [[ -f ${HOST}-${1} ]]; then
  echo "calling ${HOST}-$@" >> /tmp/xcrun-$$.log
  ${HOST}-"$@"
else
  echo "calling $@" >> /tmp/xcrun-$$.log
  "$@"
fi
