#!/usr/bin/env bash
set -euo pipefail

cd "$(git rev-parse --show-toplevel)"

mapfile -t files < \
    <(git ls-files)
mapfile -t scripts < \
    <(awk '/#!(\/usr\/bin\/env bash|\/bin\/sh)/&&FNR==1{print FILENAME}' "${files[@]}")
mapfile -t buildfiles < \
    <(find . \( -name 'WORKSPACE' -o -name 'BUILD' -o -name '*.bzl' \))

>&2 echo "shellcheck"
for f in "${scripts[@]}"; do >&2 echo "  $f"; done
shellcheck "${scripts[@]}"
>&2 echo -e "OK\n"

>&2 echo "buildifier -mode diff"
for f in "${buildfiles[@]}"; do >&2 echo "  $f"; done
fail=0
out=$(buildifier -mode diff -diff_command='diff -u' "${buildfiles[@]}") || fail=1
if [[ "$fail" == 1 ]]; then
    >&2 echo "ERROR: buildifier:"
    echo "$out"
    >&2 echo
    >&2 echo "You may try running:"
    >&2 echo "  buildifier ${buildfiles[*]}"
    exit 1
fi
>&2 echo -e "OK\n"