#!/bin/sh

REPOS="$1"
TXN="$2"

SVNLOOK=/usr/bin/svnlook

if ! $SVNLOOK log -t "$TXN" "$REPOS" | grep "[a-zA-Z0-9]" > /dev/null; then
    echo "No commit message specified" 1>&2
    exit 1
fi

FOUNDERR=0
IFS=$'\n'
for a in `$SVNLOOK changed -t "$TXN" "$REPOS" | $(dirname "$0")/style-guide-regex.pl`
do
#  echo "testing file $a" >&2
  if [ "`$SVNLOOK propget -t \"$TXN\" \"$REPOS\" svn:eol-style \"$a\" 2>/dev/null`" != "native" ]; then
    echo -e "Style error: the property 'svn:eol-style' for $a is not set to 'native'.\nUse 'svn propset svn:eol-style native $a'." >&2
    FOUNDERR=1
    break
  fi
  $SVNLOOK cat -t "$TXN" "$REPOS" "$a" 2>/dev/null | perl `dirname "$0"`/checker.pl "$a"
  if [ $? -ne 0 ]; then
    FOUNDERR=1
    break
  fi
done

for a in `$SVNLOOK changed -t "$TXN" "$REPOS" | perl -lne 'print $1 if m{^[^D]...(kernel/trunk/.*\.txt)$}i'`
do
  $SVNLOOK cat -t "$TXN" "$REPOS" "$a" 2>/dev/null | perl `dirname "$0"`/txtchecker.pl "$a"
  if [ $? -ne 0 ]; then
    FOUNDERR=1
    break
  fi
done

if [ $FOUNDERR -ne 0 ]; then
  echo -e "\nYour commit violates the code style.\nFix the style and try again.\nThe list of rules: http://board.kolibrios.org/viewtopic.php?f=7&t=1950" >&2
  exit 1
fi

exit 0
