. "$BST_LIB"
require grep grep/present

# Busybox also provides a grep applet; this suite covers the GNU grep this ticket
# ships. Assert the shipped /usr/bin/grep is GNU grep (update-alternatives winner).
check_out grep/gnu-version "GNU grep" grep --version

tmp=${TMPDIR:-/tmp}/bst-grep.$$
printf 'alpha\nbeta\ngamma\nBeta\n' > "$tmp"

check_out grep/basic    "beta"    grep beta "$tmp"
check_out grep/count-i  "2"       sh -c "grep -ic beta '$tmp'"
check_out grep/invert   "gamma"   grep -v beta "$tmp"
check_out grep/ere      "alpha"   grep -E 'a(lph|mm)a' "$tmp"
check_out grep/pipe     "gamma"   sh -c "printf 'x\ngamma\n' | grep gamma"

# --disable-perl-regexp keeps libpcre2 out of the closure: -P must be rejected.
# GNU grep 3.11 built --disable-perl-regexp reports "Perl matching not supported
# in a --disable-perl-regexp build"; older builds say "not compiled". Require the
# rejection (nonzero rc) AND a message naming the disabled perl-regexp support.
out=$(grep -P beta "$tmp" 2>&1); rc=$?
case "$rc:$out" in
  0:*) bst_notok grep/no-perl-regexp "grep -P unexpectedly succeeded (libpcre2 present?)" ;;
  *"disable-perl-regexp"*|*"Perl matching not supported"*|*"not compiled"*)
       bst_ok grep/no-perl-regexp ;;
  *)   bst_notok grep/no-perl-regexp "expected -P disabled; rc=$rc $(_bst_1line "$out")" ;;
esac

rm -f "$tmp"
