#!/usr/bin/env bash
#
# Copyright 2016-2021 Axel Huebl
#
# License: LGPLv3+

# Search recursive inside a folder if a shell scripts or batch system files
# contains non-ASCII characters. This causes problems with some systems.
# We can also not guarantee that everyone has UTF8 locales installed, so
# why depend on it.
#
# @result 0 if no files are found, else 1
#

ok=0

pattern="\.c$|\.cpp$|\.F90$|\.h$|\.H$|\.ini$|\.py$|"\
"\.rst$|\.sh$|\.tex$|\.txt$|\.xml$|\.yml$|\.yaml$|"\
"CMakeLists\.txt|inputs"

for i in $(find . \
                -not -path "./.git/*" \
                -not -path "./.spack-env/*" \
                -not -path "./share/openPMD/thirdParty/*" \
                -type f | \
           grep -P "${pattern}")
do
  # non-ASCII test regex via jerrymouse at stackoverflow under CC-By-SA 3.0:
  #   http://stackoverflow.com/questions/3001177/how-do-i-grep-for-all-non-ascii-characters-in-unix/9395552#9395552
  result=$(grep --color='always' -P -n "[\x80-\xFF]" $i)

  if [ $? -eq 0 ]
  then
    echo "$i contains non-ASCII characters!"
    echo "$result"
    ok=1
  fi
done

exit $ok
