#!/usr/bin/env bash

FILES=6664 # needs to be this or greater

echo '## Tools we are using:'
find --version | grep -e find -e Features
grep --version | grep -e grep

cd `mktemp -d`
echo "## Testing in $PWD"

mkdir dir{1,2}
echo "## Creating $((2*FILES)) empty files"
for ((i=0; i<${FILES}; ++i)); do
    touch dir{1,2}/$i;
done
echo -- -Werror > dir1/0

echo '## Find and grep work fine on the directories themselves'
find dir1/ dir2/ -type f -exec grep -l -- Werror {} +

echo '## Now we create a file with no permissions'
touch file
chmod 0 file

echo '## As long as we do not use `-exec [..] +` but `-exec [..] \;`, '
echo '## everything continues to work (ignore "grep: file: Permission denied")'
find file dir1/ dir2/ -type f -exec grep -l -- Werror {} \;

echo '## But when we do, we get an infinite loop'
find file dir1/ dir2/ -type f -exec grep -l -- Werror {} + | sed 20q
