From ca412a3d76fd5ac7a33bedba853ce07d6b378d46 Mon Sep 17 00:00:00 2001
From: Thomas Lange <lange@debian.org>
Date: Sat, 23 Sep 2017 12:56:41 +0200
Subject: [PATCH 8/8] automatic detect if grep support perl regex

busybox grep and non-gnu grep does not support perl regex
We need the pipe to get anything to match. We have to disable set -e
because the test may cause a non-zero exit code
---
 functions | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/functions b/functions
index c8bc13d..e4ca246 100644
--- a/functions
+++ b/functions
@@ -1247,6 +1247,16 @@ setup_merged_usr() {
 if in_path perl; then
 	PKGDETAILS=pkgdetails_perl
 
+	# test if grep supports --perl-regexp
+	set +e
+	echo x | grep --perl-regexp . >/dev/null 2>&1
+	if [ $? -eq 2 ]; then
+	    gropt=-E
+        else
+	    gropt=--perl-regexp
+	fi
+	set -e
+
 	pkgdetails_field () {
 		# uniq field mirror Packages values...
 		perl -le '
@@ -1317,7 +1327,7 @@ while (read STDIN, $x, 1) {
 }' "$@"
 		elif [ "$1" = "GETDEPS" ]; then
 			local pkgdest="$2"; shift; shift
-LC_ALL=C grep -P '^$|^Package:|^Depends:|^Pre-Depends:'  $pkgdest | perl -e '
+LC_ALL=C grep "$gropt" '^$|^Package:|^Depends:|^Pre-Depends:'  $pkgdest | perl -e '
 %seen = map { $_ => 1 } @ARGV;
 while (<STDIN>) {
 	if (/^Package: (.*)$/) {
@@ -1345,13 +1355,13 @@ while (<STDIN>) {
 			local m="$2"
 			local p="$3"
 			shift; shift; shift
-			LC_ALL=C grep -P '^$|^Architecture:|^Filename:|^MD5sum:|^Package:|^Priority:|^SHA256:|^Size:|^Version:|^Depends:|^Pre-Depends:' "$p" | pkgdetails_field 1 Package: "$m" "$@"
+			LC_ALL=C grep "$gropt" '^$|^Architecture:|^Filename:|^MD5sum:|^Package:|^Priority:|^SHA256:|^Size:|^Version:|^Depends:|^Pre-Depends:' "$p" | pkgdetails_field 1 Package: "$m" "$@"
 		elif [ "$1" = "FIELD" ]; then
 			local f="$2"
 			local m="$3"
 			local p="$4"
 			shift; shift; shift; shift
-			LC_ALL=C grep -P '^$|^Architecture:|^Filename:|^MD5sum:|^Package:|^Priority:|^SHA256:|^Size:|^Version:|^Depends:|^Pre-Depends:' "$p" | pkgdetails_field 0 "$f" "$m" "$@"
+			LC_ALL=C grep "$gropt" '^$|^Architecture:|^Filename:|^MD5sum:|^Package:|^Priority:|^SHA256:|^Size:|^Version:|^Depends:|^Pre-Depends:' "$p" | pkgdetails_field 0 "$f" "$m" "$@"
 		elif [ "$1" = "STANZAS" ]; then
 			local pkgdest="$2"; shift; shift
 			perl -e '
-- 
2.11.0

