#!/usr/bin/perl

$grep = $ENV{'GREP'} || 'grep';

# grepw - case-insensitive grep stdin for all/any of the arg words in a line
# Steve Kinzler, steve@kinzler.com, Nov 04/Nov 05
# https://kinzler.com/me/home.html#unix

while ($ARGV[0] =~ /^-/) {
	     shift, last if $ARGV[0] eq '--';
	$h = shift, next if $ARGV[0] eq '-?';
	$o = shift, next if $ARGV[0] eq '-or';
	$V = shift, next if $ARGV[0] eq '-V';
	$args .= ' ' . shift;
}

# WARNING: -or not supported with all grep(1)'s
print("usage: $0 [ -or ] [ -V ]
	[ $grep flags ] word ...
	-or	match lines containing any of the words (default all)
	-V	print the assembled shell command being run to stderr
Words are case insensitive sequences of [a-z0-9_].\n"), exit if $h;

@words = grep(/./, split(/\W+/, join(' ', @ARGV)));

$cmd = "$grep -i$args '" . join('\|', @words) . "'"	     if   $o;
$cmd = "$grep -i$args "  . join(" | $grep -i$args ", @words) if ! $o;

print STDERR "$cmd\n" if $V;
exec system $cmd;
