#!/bin/bash
#
# Called by git-commit with no arguments.  This checks to make
# sure that all .c and .h files are indented correctly before
# a commit is made.
#
# To enable this hook, make this file executable and place it
# in $GIT_DIR/hooks.

. git-sh-setup

CHFILES=$(git-diff-index -M --name-only --cached HEAD | \
          grep '.*\.[ch]$' | grep -v '^zlib/')

for CHFILE in $CHFILES;
do
  MKTEMPLATE=`basename $CHFILE`.XXXXXXXX
  TEMPFILE=`mktemp -t "$MKTEMPLATE"` || exit 1
  $GIT_DIR/../scindent $GIT_DIR/../$CHFILE -o $TEMPFILE
  if diff $GIT_DIR/../$CHFILE $TEMPFILE
  then
    rm -f $TEMPFILE
  else
    rm -f $TEMPFILE
    NEEDS_FORMAT=1
    echo >&2 "$CHFILE needs to be indented with:"
    echo >&2 "   $GIT_DIR/../scindent \\"
    echo >&2 "      $GIT_DIR/../$CHFILE"
  fi
done
if [ -z "$NEEDS_FORMAT" ]
then
  exit 0
else
  exit 1
fi
