$OpenBSD: patch-kde-dev-scripts_wcgrep,v 1.2 2013/07/05 14:32:50 zhuk Exp $
Cleanup development scripts, making them more portable and secure.
Upstream review 110858: http://git.reviewboard.kde.org/r/110858/
--- kde-dev-scripts/wcgrep.orig	Fri Jun 28 22:03:13 2013
+++ kde-dev-scripts/wcgrep	Fri Jul  5 18:14:32 2013
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 
 # Copyright 2004 Ben Reser <ben@reser.org>
 # Licensed under the terms subversion ships under or GPLv2.
@@ -21,7 +21,9 @@
 # behavior:
 #
 # WCGREP_GREP      Controls what command is used for the grep command.
-#                  If unset or null wcgrep will use the command named grep.
+#                  If unset or null wcgrep will check if ggrep (GNU grep)
+#                  is available and use it; if "ggrep" could not be found
+#                  in the path, "grep" will be used.
 # WCGREP_GREPARGS  Controls what arguments are always passed to the grep
 #                  command before the arguments given on the command line.
 #                  If unset or null it defaults to -HnI (always print file
@@ -34,26 +36,55 @@
 #                  ignore vim backup files and subversion admin dirs.
 
 
-arg_count=$#
-for (( i=1; i <= $arg_count; i++ )); do
-    arg="$1"
-    shift 1
-    if [ -z "$pattern" ]; then
-        if [ "$arg" == "--" ]; then
-            grepargs="$grepargs $arg"
-            pattern="$1"
-            shift 1
-            ((i++))
-        elif [ "${arg:0:1}" != "-" ]; then
-            pattern="$arg"
-        else
-            grepargs="$grepargs $arg"
-        fi  
+grepargs=
+pattern=
+
+# When loop completes, $@ should contain only list of paths to traverse.
+# If empty, set it to "." for compatibility (path list is not optional everywhere).
+# This way we can use "$@" feature when calling find(1).
+#
+# XXX The grepargs handling is broken, unlike the handling of path arguments.
+
+usage() {
+    echo "usage: $(basename \"$0\") [grep-argument [...] [--]] pattern" >&2
+    echo "       [path ...]" >&2
+    exit 1
+}
+
+while [ $# -gt 0 ]; do
+    if [ X"$1" = X-- ]; then
+        grepargs="$grepargs $1"
+        pattern="$2"
+        shift 2
+        break
+    elif [ X"${1#-}" = X"$1" ]; then
+        pattern="$1"
+        shift
+        break
     else
-        pathargs="$pathargs $arg"
-    fi
+        grepargs="$grepargs $1"
+    fi  
+    shift
 done
+test -z "$pattern" && usage
+if [ $# -eq 0 ]; then
+	set -- .
+fi
 
-find $pathargs -regex ${WCGREP_IGNORE:-'.*~$\|\./\.git/.*|.*/\.svn\(/\|$\)'} -prune -o \
-    -type f -print0 | xargs -r0 ${WCGREP_GREP:-grep} ${WCGREP_GREPARGS:--HnI} \
-    $grepargs "$pattern"
+WCGREP_IGNORE=${WCGREP_IGNORE:-'.*~$\|\./\.git/.*|.*/\.svn\(/\|$\)'}
+WCGREP_GREPARGS="${WCGREP_GREPARGS:--HnI}"
+
+# Some OSes have GNU tools with "g" prefix, try that first
+# TODO: check if:
+#  a) this tool is still needed;
+#  b) it is okay to break backward compatibility in future for
+#     portability reasons (avoid GNU toolchain dependency).
+
+WCGREP_GREP="${WCGREP_GREP:-$(command -v ggrep)}"
+WCGREP_GREP="${WCGREP_GREP:-grep}"
+
+WCGREP_FIND="$(command -v gfind)"
+WCGREP_FIND="${WCGREP_FIND:-find}"
+
+${WCGREP_FIND} "$@" \( -regex "${WCGREP_IGNORE}" -prune -o -type f \) -print0 | \
+    xargs -r0 ${WCGREP_GREP} ${WCGREP_GREPARGS} $grepargs "$pattern"
