:
#!/bin/sh
PATH="BINDIR:$PATH"; export PATH
prog=`echo "$0" | sed 's|.*/||'`
case "$prog" in
*egrep) grep=${EGREP-egrep} ;;
*fgrep) grep=${FGREP-fgrep} ;;
*) grep=${GREP-grep} ;;
esac
pat=""
after_dash_dash=""
files_with_matches=0
files_without_matches=0
no_filename=0
with_filename=0
while test $ case "$after_dash_dash$1" in
--d* | --rec*) echo >&2 "$0: $1: option not supported"; exit 2;;
--files-with-*) files_with_matches=1;;
--files-witho*) files_without_matches=1;;
--no-f*) no_filename=1;;
--wi*) with_filename=1;;
--*) ;;
-*)
case "$1" in
-*[dr]*) echo >&2 "$0: $1: option not supported"; exit 2;;
esac
case "$1" in
-*H*) with_filename=1;;
esac
case "$1" in
-*h*) no_filename=1;;
esac
case "$1" in
-*L*) files_without_matches=1;;
esac
case "$1" in
-*l*) files_with_matches=1;;
esac;;
esac
case "$after_dash_dash$1" in
-[ef]) opt="$opt $1"; shift; pat="$1"
if test "$grep" = grep; then grep=egrep
fi;;
-[ABCdm])opt="$opt $1 $2"; shift;;
--) opt="$opt $1"; after_dash_dash=1;;
-*) opt="$opt $1";;
*) if test -z "$pat"; then
pat="$1"
else
break;
fi;;
esac
shift
done
if test -z "$pat"; then
echo "grep through gzip files"
echo "usage: $prog [grep_options] pattern [files]"
exit 2
fi
if test $ gzip -cdfq | $grep $opt "$pat"
exit $?
fi
res=0
for i do
gzip -cdfq "$i" |
if test $files_with_matches -eq 1; then
$grep $opt "$pat" > /dev/null && printf "%s\n" "$i"
elif test $files_without_matches -eq 1; then
$grep $opt "$pat" > /dev/null || printf "%s\n" "$i"
elif test $with_filename -eq 0 && { test $ $grep $opt "$pat"
else
i=${i//\\/\\\\}
i=${i//|/\\|}
i=${i//&/\\&}
if test $with_filename -eq 1; then
sed_script="s|^[^:]*:|${i}:|"
else
sed_script="s|^|${i}:|"
fi
$grep $opt "$pat" | sed "$sed_script"
fi
r=$?
test $res -lt $r && res=$r
done
exit $res