#!/bin/sh
#
# Check that the code follows a consistent code style
#

astyle --version 2>> /dev/null
if [ $? -eq 127 ] ; then
    echo "--AStyle not found installed on the system, skiping style check"
    exit 0;
fi

# Something close to our style which is the same as the Qt4 code style
#astyle --indent=spaces=4 --brackets=linux --indent-labels --pad=oper --unpad=paren --one-line=keep-statements --convert-tabs --indent-preprocessor `find -type f -name '*.cpp'` `find -type f -name '*.h'`

echo "--Checking style--"
for file in `git diff-index --name-only HEAD | grep *.cpp` ; do
    tempfoo=`basename $0`
    newfile=`mktemp /tmp/${tempfoo}.XXXXXX` || exit 1
    astyle --indent=spaces=4 --brackets=linux --indent-labels --pad=oper --one-line=keep-statements --convert-tabs --indent-preprocessor < $file > $newfile 2>> /dev/null
    diff "${file}" "${newfile}"
    r=$?
    rm "${newfile}"
    if [ $r != 0 ] ; then
        echo "Code style error in $file, please fix before commiting."
        exit 1
    fi
done
echo "--Checking style pass--"
