#!/usr/bin/perl
# This runs stackless.
#use Mooix::Thing;
use Mooix::CallStack;
run sub {
	my $this=shift;
	%_=@_;

	# The caller has to tell us the object because there is no sane way
	# to get the object by looking at the callstack. However, the
	# callstack can be used to make sure that the object is the same as
	# the caller, which is done, to prevent someone from removing
	# someone else.
	my $object=$_{object} || $this->usage("object required");
	my $stack=Mooix::CallStack::get();
	$stack=$stack->nextsegment;
	if ($stack->index ne $object->index) {
		$this->usage("caller (".$stack->index.") does not match passed object (".$object->index.")");
	}
	
	my @from=("shutdownlist", "startuplist");
	if (exists $_{from}) {
		if ($_{from} eq 'startup') {
			@to=('startuplist');
		}
		elsif ($_{from} eq 'shutdown') {
			@to=('shutdownlist');
		}
		else {
			$this->usage("from parameter takes either startup or shutdown as its value");
		}
	}

	my $n=0;
	foreach my $list (@from) {
		if ($this->$list->remove(object => $object)) {
			$n++;
		}
	}

	return $n;
}
