#!/bin/sh
#
# To enable this hook:
#     1. rename this file to "pre-commit".
#     2. copy it into .git/hooks dir
#     3. in .git/hooks dir, run chown +x pre-commit
#     4. in .git/hooks dir, run ln -s pre-commit pre-applypatch
#

files=`git diff --cached --name-only --diff-filter=ACMRTUXB`
modifed_files=`git diff --name-only --diff-filter=ACMRTUXB`

for file in $files
do
	echo $file | egrep '\.c$|\.cc$|\.cp$|\.cpp$|\.cxx$|\.h$|\.hh$|\.hpp$|\.hxx$|\.inl$' > /dev/null
	if [ $? -eq 0 ]
	then
		echo $modifed_files | grep "\<$file\>" > /dev/null
		if [ $? -eq 0 ]
		then
			echo error: $file: is modified
                        exit 1
		fi

	        echo $file | egrep '[[:upper:]]' > /dev/null
        	if [ $? -eq 0 ]
        	then
			echo error: $file: uppercase in file name
			exit 1
        	fi

		awk '
			BEGIN {
				error_count = 0;
				max_errors = 10;
			}

			function found_error(desc)
			{
				error_count++;
				if (error_count > max_errors) {
					exit;
				}
				print "error (" error_count "): "  desc  " @ " FILENAME " +"FNR;
			}
			
			(length() > 100) { found_error("length"); }

			/\t/ { found_error("tab"); }

			/ $/ { found_error("space");}

			/\xd/ { found_error("dos new line");}

			END {
				if (error_count > max_errors) {
					print "showing only the first " max_errors " errors.";
    				}
    				exit error_count;
			}

		' $file

		if [ $? -ne 0 ]
		then
			exit 1
		fi
	fi
done

echo OK
exit 0

