#!/usr/bin/perl -w
# This hunts down files whose names match /\bfind\b/. This is a kludge
# to handle problems with OSX's caseless filename matching.

$V = $ENV{'V_lsfind'} || 1;
@ARGV = '.' unless @ARGV;
$D = $F = '';	# last-used dir and file names
$finds = 0;		# Number of matched file names
$depth = 0;		# Depth of recursion
$maxdepth = 0;	# Max depth of recursion
%dirs = ();		# Directories examined, to break recursive loops

for $x (@ARGV) {
	if (-d $x) {
		&onedir($x);
	} else {
	}
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
sub onedir {
	local($d) = @_;
	local($f,@files);
	if ($dirs{$d}++ > 1) {	# Note the directories visited
		print "###	$d visited $dirs{$d} times.\n" if $V>1;
		return;
	}
	++$depth;
	if ($depth > $maxdepth) {
		$maxdepth = $depth;
		print "$maxdepth	$d\n" if $V>1;
	}
	$d =~ s"/*$"/";	# Insert '/' at end of each
	print "DIR	$d\n" if $V>2 || $depth > 97;
	$D = $d;
	$d = '' if $d eq './';	# Suppress ./ in output
	@files = glob("$d*");
file:
	for $f (@files) {		# Files in this directory
		$F = $f;
		if ($f =~ /\bfind\b/i) {	# Is it a "find" file?
			system "ls -lid '$f'\n" if $V>0;
			++$finds;
		}
		if (-l $f) {		# Don't recurse into symlinks
		} elsif (-d $f) {	# Recurse into directories
			&onedir($f);
			next file;
		}
	}
	--$depth;
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
print "Found $finds find files.\n" if $V>0 && $finds > 0;
print "Max depth = $maxdepth.\n" if $V>2;
exit 0;
