#!/bin/bash

set -e -u -o pipefail

# An AWK regex matching tracked file paths to be excluded from the search.
# Example: '.*\.po|README'
PATH_EXCLUDE_REGEX='.*\.po|.*\.patch|.*\.diff|\/debian\/.*'

export GIT_DIR="$ABS_TOP_SRCDIR/.git"
export GIT_WORK_TREE="$ABS_TOP_SRCDIR"

if [ ! -d "$GIT_DIR" ]; then
    echo "Git repository is required for this test!" 1>&2
    exit 77
fi

{
    # Look for lines with trailing whitespace in all files tracked by Git
    git grep -n -I '\s\+$' -- "$(git rev-parse --show-toplevel)" ||
        # Don't fail if no such lines were found anywhere
        [[ $? == 1 ]]
} |
    awk -- "
        BEGIN {
            found = 0
        }
        ! /^($PATH_EXCLUDE_REGEX):/ {
            if (!found) {
                print \"Trailing whitespace found:\"
                found = 1
            }
            print
        }
        END {
            exit found
        }
    "
