#!/bin/bash

project_folder=$(git rev-parse --show-toplevel)
if git rev-parse --verify HEAD >/dev/null 2>&1
then
	against=HEAD
else
	# Initial commit: diff against an empty tree object
	against=$(git hash-object -t tree /dev/null)
fi

# Redirect output to stderr.
exec 1>&2

tmpdir=$(mktemp -d repo-XXXXXXXX)
trap "rm -rf $tmpdir" EXIT INT

exclude_dirs=(
  conan_recipes
  src/mage/cpp/community_detection_module/grappolo
  src/mage/cpp/text_module/utf8
)

exclude_files=(
  src/mage/cpp/util_module/algorithm/md5.hpp
  src/mage/cpp/util_module/algorithm/md5.cpp
)

modified_files=$(git diff --cached --name-only --diff-filter=AM $against | sed -nE "/.*\.(cpp|cc|cxx|c|h|hpp)$/p")
FAIL=0
for file in $modified_files; do

  run_check=true
  for exclude_dir in ${exclude_dirs[@]}; do
    if [[ $file == $exclude_dir* ]]; then
      run_check=false
      echo "Skipping $file because it is in $exclude_dir"
      break
    fi
  done
  for exclude_file in ${exclude_files[@]}; do
    if [[ $file == $exclude_file ]]; then
      run_check=false
      echo "Skipping $file because it is in $exclude_file"
      break
    fi
  done
  if ! $run_check; then
    continue
  fi

  echo "Checking $file..."

  cp $project_folder/.clang-format $project_folder/.clang-tidy $tmpdir

  git checkout-index --prefix="$tmpdir/" -- $file

  # Do not break header checker
  echo "Running header checker..."
  $project_folder/tools/header-checker.py $tmpdir/$file $file --amend-year
  CODE=$?
  if [ $CODE -ne 0 ]; then
    FAIL=1
  fi
done;

exit ${FAIL}
