#! /bin/sh

# Single space and remove trailing whitespace from source files.
t=__wt.$$
trap 'rm -f $t' 0 1 2 3 13 15

# Clear lines that only contain whitespace, compress multiple empty lines
# into a single line, discard trailing empty lines.
whitespace()
{
	! head $1 | grep -q 'automatically generated by SWIG' || return
	sed -e 's/[	 ][	 ]*$//' < $1 | \
	    cat -s | \
	    sed -e '${' -e '/^$/d' -e '}' > $t
	cmp $t $1 > /dev/null 2>&1 || (echo "$1" && cp $t $1)
}

cd ..

find bench dist examples ext src test \
    -name '*.[chi]' -o \
    -name '*.dox' -o \
    -name '*.in' -o \
    -name '*.py' -o \
    -name 's_*' -o \
    -name 'Makefile.am' |
    sed -e '/Makefile.in/d' \
        -e '/checksum\/power8/d' \
        -e '/3rdparty/d' \
        -e '/docs\/tools/d' \
        -e '/log\/log_auto/d' \
| while read f ; do
	whitespace $f
done

exit 0
