#!/usr/bin/perl -w
# originally by Michael Schwern

# Like grep -r except...
#   * you can leave off the directory and it will use . instead of waiting like
#     a dumbshit for STDIN
#   * It handles paths with spaces and quotes.
#   * it will not traverse into these directories or files.
my @Prunes = (qw(.svn CVS blib *~ *.bak _darcs), '#*');

my @Args = @ARGV;
my $Dir;
if( grep(!/^-/, @Args) <= 1 ) {
    $Dir = '.';
}
else {
    $Dir = pop @Args;
}

@Args = map { "'$_'" }
        map { s/'/'"'"'/g; $_ } @Args;

# Escape spaces and quotes
$Dir =~ s{([ '"])}{\\$1}g;

my $prunes = join ' -o ', map { "-name '$_' -prune" } @Prunes;
exec "find $Dir $prunes -o -type f -print0 | ".
     "xargs -0 grep @Args";