#!/bin/sh

bad=0

check_file() {
  name=$1
  tmp=$2

  if [  `awk ' { if ( length > x ) { x = length } } END { print x }' $tmp` -gt 100 ]; then
    echo "File \"$name\" has lines with more than 120 columns."
    bad=1
  fi

  grep -P '\t' $tmp > /dev/null
  if [ $? -eq 0 ]; then
    echo "File \"$name\" has tabulations in it."
    bad=1
  fi

  grep -P '( |\t)+$' $tmp > /dev/null
  if [ $? -eq 0 ]; then
    echo "File \"$name\" has trailing whitespace."
    bad=1
  fi
}

for f in $(git diff-index --cached --name-only HEAD --diff-filter=ACMR | egrep "\.ml(i?)$")
do
  tf=$(git checkout-index --temp $f | cut -f 1)
  trap "rm -f -- $tf" EXIT
  check_file $f $tf
  rm -f -- $tf
  trap - EXIT
done

exit $bad
