#!/bin/bash

# Reject non-ASCII characters in C/C++ and CMake source files.
. `dirname -- ${BASH_SOURCE[0]}`/common_functions.sh
setup_trap
cd_top
use_pygrep
check_fast_mode_flag

# Report lines containing any byte outside the 7-bit ASCII range.
check_charset()
{
    ! head $1 | grep -q 'automatically generated by SWIG' || return
    LC_ALL=C awk 'BEGIN{re="[\200-\377]"} $0 ~ re {printf "%s:%d: non-ASCII character\n", FILENAME, FNR}' "$1"
}

# Parallel execution: if it's the main invocation of the script, collect the file names
# to process and run them in subprocesses.
is_main_run && {
    find bench dist examples ext src test tools/checksum_bitflip cmake lang CMakeLists.txt \
        -name '*.[ch]' -o \
        -name '*.cpp' -o \
        -name '*.i' -o \
        -name '*.in' -o \
        -name '*.cmake' -o \
        -name 'CMakeLists.txt' | \
        $FGREP -v \
            -e 'Makefile.in' \
            -e 'checksum/power8' \
            -e '3rdparty' \
            -e 'docs/tools' \
            -e 'log/log_auto' \
    | do_in_parallel
    exit $?
}

# A list of files provided: process them one by one.

for f in "$@"; do
    check_charset $f
done

exit 0
