# FileSearch.pm
# An Example File Search module

package FileSearch;

# Create a new class instance (object)
sub new {
	my $arg0 = shift;
	my $cls = ref($arg0) || $arg0;
	my $this = {};
	bless $this, $cls;
	$this->initialize(@_);
	return $this;
}

sub initialize {
	my $this = shift;
	my %options = @_;
	foreach (keys %options) {
		$this->{$_} = $options{$_};
	}
}

sub find {
	my ($this, $path) = @_;
	my @matches = ();
	my $pattern = $this->{'PATTERN'};
	local *DIRENT;
	
	opendir DIRENT, $path or return ();
	
	# Get a list of entries in this directory, 
	# and sort it lexicographically
	my @entries = sort { lc($a) cmp lc($b) } readdir(DIRENT);
	
	foreach my $fn (@entries) {
		my $entry = "$path/$fn";
		
		# Recursive search if the entry is a directory,
		# We should ignore . and .. in our search
		if (-d $entry and $fn !~ m/^\.\.?$/) {
			push @matches, $this->find($entry);
		}
		$fn =~ m/$pattern/ and push @matches, $entry;
	}
	closedir DIRENT;
	return @matches;
}

1;
