#!/usr/bin/env bash
set -euo pipefail
root="$(cd "$(dirname "$0")/.." && pwd)"
if [[ "${1:-}" = run && "${2:-}" = image-sparrow-* ]]; then
  export RUSTUP_HOME="$root/.tools/rust"
fi
if command -v rustup >/dev/null 2>&1; then exec rustup "$@"; fi
if [[ -x "$root/.tools/cargo/bin/rustup" ]]; then
  export RUSTUP_HOME="${RUSTUP_HOME:-$root/.tools/rust}"
  exec "$root/.tools/cargo/bin/rustup" "$@"
fi
if command -v rustup-init >/dev/null 2>&1; then
  mkdir -p "$root/.tools/bin"
  if [[ ! -x "$root/.tools/bin/rustup" ]]; then cp "$(command -v rustup-init)" "$root/.tools/bin/rustup"; fi
  exec "$root/.tools/bin/rustup" "$@"
fi
printf '%s\n' 'rustup is unavailable; run scripts/bootstrap.sh' >&2
exit 1
