#!/bin/sh

# NAME
#	g - grep for lines matching multiple patterns
#
# SYNOPSIS
#       g [-grepoptions] [-e] pattern [ [-grepoptions] [-e] pattern ] ...
#
# DESCRIPTION
# 	G is just a shorthand for:
#       grep [-grepoptions] [-e] pattern [ | grep [-grepoptions] [-e] pattern ] ...
#
# AUTHOR
#       Ole Tange http://ole.tange.dk
#       Version: 95-MAR-13
#
# SEE ALSO
#	grep(1)

G_GREPOPTIONS=""
G_GREPCMD=""
G_PIPE=""

while [ -n "$1" ] ; do
  # Collect all options up to the regexp (not including the '-e' option)
  while expr match "a$1" a-[^e].* > /dev/null ; do
	  G_GREPOPTIONS="$G_GREPOPTIONS $1"
	  shift;
  done
  # the '-e' option must be the last before the regexp
  if expr match "a$1" a-e > /dev/null ; then
	  G_GREPOPTIONS="$G_GREPOPTIONS $1"
	  shift;
  fi	

  G_GREPCMD="$G_GREPCMD $G_PIPE grep $G_GREPOPTIONS '$1'"
  G_PIPE="|"	# to avoid a pipe in the startout grep
  G_GREPOPTIONS=""
  shift;
done

eval $G_GREPCMD
