#!/usr/bin/perl
use Mooix::Thing;
use Mooix::Root;

run sub {
	my $this=shift;

	my $unfound = @_;
	my %needed = map { $_ => 1 } @_;
	
	my %indextoobj;
	my @todo = ($this, $Mooix::Root);
	while (@todo && $unfound) {
		my $obj = shift @todo;
		next if $indextoobj{$obj->index};
		$indextoobj{$obj->index}=$obj;
		$unfound-- if $needed{$obj->index};
		last unless $unfound;
		
		# Find all references to other objects from this one..
		# XXX Currently just link and dirs, not sticky files.
		opendir(DIR, $obj->id);
		while (my $f = readdir(DIR)) {
			next if $f eq '.' || $f eq '..';
			if (-d $obj->id."/$f") {
				my $v=$obj->$f;
				push @todo, $v if ref $v eq 'Mooix::Thing';
			}
		}
		closedir DIR;
	}

	return map { $indextoobj{$_} } @_;
}
