#!/usr/bin/perl
#use Mooix::Thing;
#use Mooix::Root;
run sub {
	my $this=shift;

	my @err;
	if (! @_) {
		push @err, "Cannot parse that package data.";
	}
	
	# These are the fields in this package that can be set based on
	# data in the package.
	my %fields = map { $_ => 1 } qw{date dbversion hostname mooname installs};
	
	# Separate out the first object, which should describe this
	# package, and set fields based on it, and do other checks. Also
	# collect warnings from parser.
	my ($object, $field, @value, $first);
	for (my $i=0; $i < @_; $i+=2) {
		if ($_[$i] eq 'object') {
			if ($object eq $first && defined $field) {
				if ($field eq 'dbversion') {
					if ($value[0] > $Mooix::Root->system->mooinfo->dbversion) {
						push @err, "The package is from a newer version of mooix, and cannot be installed here.";
					}
				}
				elsif ($field eq 'format') {
					if ($value[0] ne $this->format) {
						push @err, "Package format mismatch.";
					}
				}
				elsif ($field eq 'parent') {
					if ($value[0] != $this->parent) {
						push @err, "This package object cannot install that data. You need an object derived from $value[0] to handle it.";
					}
				}
				
				if ($fields{$field}) {
					$this->$field(@value);
				}
			}
			$object=$_[$i+1];
			$first=$object unless defined $first;
			$field=undef;
		}
		elsif ($_[$i] eq 'field') {
			$field=$_[$i+1];
			@value=();
		}
		elsif ($_[$i] eq 'value') {
			push @value, $_[$i+1];
		}
		elsif ($_[$i] eq 'warning') {
			push @err, $_[$i+1];
		}
	}
	
	return @err;
}
