#!/bin/sh

# copy this file to .git/hooks (i.e., overwrite the file .git/hooks/pre-commit)
# to perform source checks before commits

check_source () {
  echo $1
  $PWD/scripts/src_check $1
  if [ $? -eq 0 ]
  then
    APIS=`echo "$1" | grep -F "_api.h"`
    if [ ${#APIS} -ne 0 ]; then
      echo "WARNING: you are committing API headers:"
      echo $APIS
      git diff --cached $APIS
      exec < /dev/tty
      echo
      read -p "Are you sure? [yn]" reply
      echo
      case $reply in
        [Yy]* ) return 0;;
        [Nn]* ) return 1;;
        * ) echo "Please answer yes or no.";;
      esac
    fi
  else
    return 1
  fi
  return 0
}

# different versions of git use different letters for deleted/removed files
FILES=`git diff --cached --name-status HEAD | \
  awk '$1 != "R" && $1 != "D" { print $2 }' | \
  grep -v -e 'src/external'                 | \
  grep -e '.*\.c$' -e '.*\.h$'`

if [ ${#FILES} -gt 0 ]
then
  check_source $FILES
fi
