#!/bin/sh
for arg in "$@"; do
  if [ "$arg" = "-derivedDataPath" ]; then exec /usr/bin/xcodebuild "$@"; fi
done

for arg in "$@"; do
  if [ "$arg" = "-destination" ] && [ -n "$OPENBOT_XCODE_COMPILATION_CACHE" ]; then
    set -- "$@" \
      COMPILATION_CACHE_ENABLE_CACHING=YES \
      "COMPILATION_CACHE_CAS_PATH=$OPENBOT_XCODE_COMPILATION_CACHE" \
      COMPILATION_CACHE_LIMIT_SIZE=20G \
      CLANG_ENABLE_PREFIX_MAPPING=YES \
      CLANG_ENABLE_PROJECT_PREFIX_MAPPING=YES \
      SWIFT_ENABLE_PREFIX_MAPPING=YES \
      SWIFT_ENABLE_PROJECT_PREFIX_MAPPING=YES \
      "CLANG_OTHER_PREFIX_MAPPINGS=$OPENBOT_XCODE_PREFIX_MAPPINGS" \
      "SWIFT_OTHER_PREFIX_MAPPINGS=$OPENBOT_XCODE_PREFIX_MAPPINGS"
    break
  fi
done

if [ -n "$OPENBOT_XCODE_DERIVED_DATA" ]; then
  exec /usr/bin/xcodebuild -derivedDataPath "$OPENBOT_XCODE_DERIVED_DATA" "$@"
fi
exec /usr/bin/xcodebuild "$@"
