#!/bin/sh

#  This file is part of t8code.
#  t8code is a C library to manage a collection (a forest) of multiple
#  connected adaptive space-trees of general element classes in parallel.
#
#  Copyright (C) 2015 the developers
#
#  t8code is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  t8code is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with t8code; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.


#
# A hook script to verify what is about to be committed.
# This hook checks whether the code is properly indented using
# the provided indent script.
# Move into .git/hooks folder and name it "pre-commit".
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#

# This is the indent script in the project's directory
INDENT=./scripts/t8indent
# This is the script to remove duplicate const after indent (bug of indent)
REMOVE_D_CONST=./scripts/remove_double_const.scp
# This is the spell checking script
TYPOS=`which typos 2> /dev/null`
if [ -z "$TYPOS" ]
then
  # Exit if the spell checking script was not found
  echo "ERROR: typos not found."
  echo "Please install typos."
  echo "See https://github.com/crate-ci/typos#install"
  exit 1
fi

if git rev-parse --verify HEAD >/dev/null 2>&1
then
	against=HEAD
else
	# Initial commit: diff against an empty tree object
	against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# Redirect output to stderr.
exec 1>&2


nocontinue=0
# create a temporary file
TMP_FILE=`mktemp`
for file in `git diff --cached --name-only`
do
  # only indent existing files, this is necessary since if we rename or delete
  # a file it is added to the committed files and we thus would try to indent a
  # nonexisting file.
  if [ ! -e $file ]
  then
    continue
  fi

  status=`$TYPOS $file`
  if ! [ -z "$status" ]; then
    echo "File $file contains typos."
    nocontinue=1
  fi 

  # We only indent .c, .cxx, .h and .hxx files
#-a ${file: -2} != ".h" ]
  FILE_ENDING="${file##*.}"
  if [ $FILE_ENDING = "c" -o $FILE_ENDING = "h" -o $FILE_ENDING = "cxx" -o $FILE_ENDING = "hxx" ]
  then
    echo "Checking file $file"
    # indent the source file and store temporarily
    $INDENT -o $TMP_FILE $file 
    # For .cxx files we need to correct a bug in indent
    $REMOVE_D_CONST $TMP_FILE
    # Check whether the indented file and the original differ
    # We are only interested in the exit status of diff so we throw away its output.
    diff -q $TMP_FILE $file > /dev/null
    status=$?
    if test $status -ne 0 
    then
      echo "File $file is not indented."
      nocontinue=1
    fi
  fi
done

# exit 1 if there was a nonindented file
# this will abort the commit
exit $nocontinue
