#!/usr/bin/perl -w
use strict;

use File::Find;
use Getopt::Std;

my %opt;
getopts("milce:",\%opt);

my $count;
use vars qw($file);

my $expr = (defined $opt{'e'}) ? $opt{'e'} : shift;
warn "Matching '$expr'\n";

my $re = ($opt{'i'}) ? qr/$expr/i : qr/$expr/;


sub match
{
 if ($_ =~ $re)
  {
   if ($opt{'l'})
    {
     print "$File::Find::name\n";
     return 1;
    }
   $count++;
   unless ($opt{'c'})
    {
     print "$File::Find::name:$.: $_"
    }
  }
 return 0;
}

sub wanted
{
 $File::Find::prune = 0;
 if (-T $_ && !/%$/)
  {
   local $file   = ($_);
   local ($_);
   no strict 'refs';
   open($file,"<$file") || die "Cannot open $file:$!";
   while (<$file>)
    {
     last if &match;
    }
   close($file);
   if ($opt{'c'} && $count)
    {
     print "$File::Find::name: $count\n"
    }
  }
 elsif (-d $_)
  {
   $File::Find::prune = 1 if ($_ eq 'mTk' && $opt{'m'});
  }
}

@ARGV = '.' unless (@ARGV);

find(\&wanted,@ARGV);
