#!/usr/bin/sh

set -euo pipefail

md5hash(){
  md5sum $1 | cut -d ' ' -f1
}

hashDir(){
  (find $1 -type f -print0  | sort -z | xargs -0 sha1sum;
   find $1 \( -type f -o -type d \) -print0 | sort -z | xargs -0 stat -c '%n %a') | sha1sum
}

rc=0

# check dockerfile changes
change=$(./hack/generate-dockerfile-from-midstream | md5sum | cut -d ' ' -f1)
if [ "$change" != "$(md5hash Dockerfile)" ] ; then
    rc=1
    echo "A change was found in CI file Dockerfile that was not sourced from the midstream file Dockerfile.in (or vice versa)."
    echo "Please reset the CI file (e.g. Dockerfile), update Dockerfile.in, run make gen-dockerfiles and commit the results"
    echo ""
fi

bundleHash=$(hashDir ./bundle)
make bundle
if [ "$(hashDir ./bundle)" != "$bundleHash" ] ; then
  echo ""
  echo "A change was found in the ./bundle that was not generated using the config."
  echo "Please run make bundle and commit the results"
  echo ""
fi

set +e
fitCheck=$(grep -nEIr --include=*_test.go 'FContext|FEntry|FDescribe|FDescribeTable|FIt')
if [ "$fitCheck" != "" ]; then
    rc=1
    echo "Focused tests were found in the code base"
    echo "Please remove any of FIt, FEntry, FDescribeTable, etc. from:"
    echo ""
    echo "$fitCheck"
    echo ""
fi
set -e

exit $rc
