/*
** SellaNMS
** $Id: dependency.cxx,v 1.7 2005/12/01 05:57:59 sella Exp $
** Copyright (c) 2001-2005 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

static const char rcsid[] = "$Id: dependency.cxx,v 1.7 2005/12/01 05:57:59 sella Exp $";

#include "dependency.h"
#include <stdio.h>

RESULT Dependency::setName(string &name) {
	this->name = name;

	return SUCCESS;
}

string Dependency::getName() {
	return this->name;
}

ACTION Dependency::evaluate(variable_binding_t &v) {
	return 0;
}

RESULT Dependency::compile(ConfigNode *config) {
	RESULT result = SUCCESS;

	string			value;
	vector<string>	values;

	if (config->assignValue("required", value)) {
		if (value.compare("yes") == 0) {
			this->required = true;
		} else if (value.compare("no") == 0) {
			this->required = false;
		} else {
			LMSG("Policy: invalid value (%s) of dependency setting 'required'.", value.c_str());

			result = FAILURE;
		}
	} else {
		this->required = DEFAULT_DEPENDENCY_REQUIRED;
	}

	if (!config->assignValue("variable", this->variable)) {
		LMSG("Policy: no dependency variable specified.");

		result = FAILURE;
	}

	if (config->assignValues("values", values)) {
		for (vector<string>::iterator v = values.begin(); v != values.end(); ++v) {
			this->values.insert(this->values.end(), *v);
		}
	} else {
		this->values.clear();
	}
	
	return result;
}

RESULT Dependency::clear() {
	this->required = DEFAULT_DEPENDENCY_REQUIRED;
	this->variable.erase();
	this->values.clear();

	return SUCCESS;
}

RESULT Dependency::print(unsigned long depth) {
	string	tab = "", str;
	for (unsigned long i = 0; i < depth; i++) {
		tab.append("   ");
	}

	LMSG("%sdependency %s {", tab.c_str(), this->name.c_str());
	LMSG("%s   required     %s;", tab.c_str(), this->required ? "yes" : "no");
	LMSG("%s   variable     %s;", tab.c_str(), this->variable.c_str());
	LMSG("");

	if (!this->values.empty()) {
		LMSG("%s   valid-values [", tab.c_str());
		for (slist<string>::iterator v = this->values.begin(); v != this->values.end(); ++v) {
			str.append(" " + *v);
		}
		LMSG("%s   valid-values [%s ];", tab.c_str(), str.c_str());
	}
	
	LMSG("%s}", tab.c_str());

	return SUCCESS;
}

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim600: fdm=marker
** vim: noet ts=3 sw=3 sts=3
*/
