#!/usr/bin/perl
#use Mooix::Thing;
#use Mooix::Verb;
run sub {
	my $this=shift;
	%_=@_;
	
	# Make sure that this command is not spoofed, just in case.
        if ($_{avatar} != $this) {
		fail "No!"; 
	}
	
	my $object=$_{direct_object} || $this->usage("bad direct object");
	my $session=$_{session} || $this->usage("bad session");

	my $id;
	if (exists $_{quote}) {
		$id=$_{quote};
	}
	else {
		$id=$object->name;
	}
	length $id or $this->usage("invalid name");

	# Make the object.
	my ($stat, $val, $nstat, $nmsg) = $this->safechange(
		object => $this->portfolio."/".$id,
		newid => 1,
		field => "parent",
		value => $object,

		# Set name in same pass, if one was given.
		( exists $_{quote} ? (
				object => "mooix:#1",
				field => "name",
				value => $_{quote}
			) : ''
		)
	);
	if (! $stat) {
		fail $val;
	}
	if (exists $_{quote} && ! $nstat) {
		$session->write("Problem setting name: $nmsg");
	}
	my $obj = $val;
	$obj->init;
	
	# Move the new object into the builder's inventory.
	if (! $obj->physics->move(object => $obj, to => $this)) {
		# Maybe they cannot hold it?
		$obj->physics->move(object => $obj, to => $this->location);
	}
	
	$session->write("Object created (".$this->refstring($obj).").");

	# Cause "it" to be updated to point to the new object.
	print $obj."\n";
	exit Mooix::Verb::SETITREF;
}
