#!/usr/bin/perl
#use Mooix::Thing;
#use Mooix::Root;
run sub {
	my $this=shift;
	%_=@_;
	exists $_{id} or $this->usage("bad id");
	my $id=$_{id};

	if ($id =~ /^\//) {
		# Try an absolute path lookup.
		my $obj=Mooix::Thing->get($_{id});
		return $obj if $obj;
	}
	elsif ($id =~ /^~([^\/]+)(.*?)$/) {
		# Relative to someone else.
		my $name=lc($1);
		my $path=$2;
		# Look up names and aliases in the sessionmanager's avatars
		# list, for lack of anywhere better to look.
		foreach my $avatar ($Mooix::Root->system->sessionmanager->avatars->list) {
			if ($name eq lc($avatar->name) ||
			    grep { $name eq lc($_) } $avatar->alias) {
				my $obj = Mooix::Thing->get($avatar->id."/$path");
				return $obj if $obj;
			}
		}
	}
	else {
		# For relative paths, the objects are searched for on this path
		# of objects.
		my @path=grep { ref $_ } map {
			if (/^\./) {
				$_ = Mooix::Thing->get($this->id."/$_");
			}
			elsif (/^\//) {
				$_ = Mooix::Thing->get($Mooix::Root->id."/$_");
			}
			$_;
		} $this->refpath;

		foreach my $item (@path) {
			my $obj = Mooix::Thing->get($item->id."/$_{id}");
			return $obj if $obj;
		}
	}
		
	return ""; # failure
}
