1#!/bin/sh
2
3# Bzgrep wrapped for bzip2,
4# adapted from zgrep by Philippe Troin <phil@fifi.org> for Debian GNU/Linux.
5## zgrep notice:
6## zgrep -- a wrapper around a grep program that decompresses files as needed
7## Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca>
8
9PATH="/usr/bin:$PATH"; export PATH
10
11prog=`echo $0 | sed 's|.*/||'`
12case "$prog" in
13	*egrep)	grep=${EGREP-egrep}	;;
14	*fgrep)	grep=${FGREP-fgrep}	;;
15	*)	grep=${GREP-grep}	;;
16esac
17pat=""
18while test $# -ne 0; do
19  case "$1" in
20  -e | -f) opt="$opt $1"; shift; pat="$1"
21           if test "$grep" = grep; then  # grep is buggy with -e on SVR4
22             grep=egrep
23           fi;;
24  -A | -B) opt="$opt $1 $2"; shift;;
25  -*)	   opt="$opt $1";;
26   *)      if test -z "$pat"; then
27	     pat="$1"
28	   else
29	     break;
30           fi;;
31  esac
32  shift
33done
34
35if test -z "$pat"; then
36  echo "grep through bzip2 files"
37  echo "usage: $prog [grep_options] pattern [files]"
38  exit 1
39fi
40
41list=0
42silent=0
43op=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
44case "$op" in
45  *l*) list=1
46esac
47case "$op" in
48  *h*) silent=1
49esac
50
51if test $# -eq 0; then
52  bzip2 -cdfq | $grep $opt "$pat"
53  exit $?
54fi
55
56res=0
57for i do
58  if test -f "$i"; then :; else if test -f "$i.bz2"; then i="$i.bz2"; fi; fi
59  if test $list -eq 1; then
60    bzip2 -cdfq "$i" | $grep $opt "$pat" 2>&1 > /dev/null && echo $i
61    r=$?
62  elif test $# -eq 1 -o $silent -eq 1; then
63    bzip2 -cdfq "$i" | $grep $opt "$pat"
64    r=$?
65  else
66    j=${i//\\/\\\\}
67    j=${j//|/\\|}
68    j=${j//&/\\&}
69    j=`printf "%s" "$j" | tr '\n' ' '`
70    bzip2 -cdfq "$i" | $grep $opt "$pat" | sed "s|^|${j}:|"
71    r=$?
72  fi
73  test "$r" -ne 0 && res="$r"
74done
75exit $res
76