#!/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 adding someone
	# else to the list, which could possibly be used to do something
	# nasty.
	my $object=$_{object} || $this->usage("object required");
	my $stack=Mooix::CallStack::get();
	my $stack=$stack->nextsegment;
	if ($stack->index ne $object->index) {
		$this->usage("caller (".$object->index.") does not match passed object (".$object->index.")");
	}
	
	my @to=("shutdownlist", "startuplist");
	if (exists $_{to}) {
		if ($_{to} eq 'startup') {
			@to=('startuplist');
		}
		elsif ($_{to} eq 'shutdown') {
			@to=('shutdownlist');
		}
		else {
			$this->usage("to parameter takes either startup or shutdown as its value");
		}
	}

	my $n=0;
	foreach my $list (@to) {
		# Add to list, unless it is already there.
		if (! grep { $_ == $object } $this->$list->list) {
			if ($this->$list->add(object => $object)) {
				$n++;
			}
		}
	}

	return $n;
}
