#!/usr/bin/env bash

# Verify that all text files end in a trailing newline.

# Exit on first failing command.
set -e
# Exit on unset variable.
set -u

success=0

while read -r line; do
  if ! [[ -s "${line}" && -z "$(tail -c 1 "${line}")" ]]; then
    printf "File must end in a trailing newline: %s\n" "${line}" >&2
    success=255
  fi
done < <(git ls-files \
  | xargs grep ".*" \
    --files-with-matches \
    --binary-files=without-match \
    --exclude="*.svg" \
    --exclude="*third-party*")

exit "${success}"
