#!/bin/bash
set -euo pipefail

# The following bazel fetch helps quickly warm up the cache for the following python script.
# It is not required for correct operation.
# shellcheck disable=SC2046
bazel fetch $(git ls-files -- '*/BUILD*' | # List all BUILD files known to git
  grep -v experimental |                   # Exclude experimental/
  sed 's#^\([^/]\+\)/.*#//\1/...#g' |      # Change a/b/c/BUILD to //a/...
  sort -u)                                 # Depulicate

# @marc-zych: It's faster to explicitly fetch all of the top level packages
# rather than having the bazel query implicitly fetch //... which can be a
# few times slower. It doesn't make any sense but it works pretty well
# empirically.

set -x

python3.6 -m tools.python.apply_futurize_step \
  --multi_step \
  --do_code_update \
  --do_file_validators \
  --do_bazel_test \
  --do_git_commit \
  --do_buildfile_updates \
  "$@";
