/**
 * File:	clients/dhcp-server_auto.ycp
 * Package:	Configuration of dhcp-server
 * Summary:	Client for autoinstallation
 * Authors:	Jiri Srain <jsrain@suse.cz>
 *
 * $Id: dhcp-server_auto.ycp 20559 2004-12-15 12:04:28Z jsrain $
 *
 * This is a client for autoinstallation. It takes its arguments,
 * goes through the configuration and return the setting.
 * Does not do any changes to the configuration.
 */

/**
 * @param function to execute
 * @param map/list of dhcp-server settings
 * @return map edited settings, Summary or boolean on success depending on called function
 * @example map mm = $[ "FAIL_DELAY" : "77" ];
 * @example map ret = WFM::CallFunction ("dhcp-server_auto", [ "Summary", mm ]);
 */

{

textdomain "dhcp-server";

y2milestone("----------------------------------------");
y2milestone("DhcpServer auto started");

import "DhcpServer";
import "DhcpServerUI";

/**
 * Remove all comments from export map
 * @param exported map export map
 * @return map exported map without comments
 */
define map KillComments (map exported) ``{
    list settings = exported["settings"]:[];
    settings = maplist(map decl, (list<map>)settings, ``{
	if (haskey (decl, "comment_before"))
	    decl = remove (decl, "comment_before");
	if (haskey (decl, "comment_after"))
	    decl = remove (decl, "comment_after");
	if (haskey (decl, "options"))
	{
	    list<map> options = (list<map>)(decl["options"]:[]);
	    options = maplist (map m, options, ``{
		if (haskey (m, "comment_before"))
		    m = remove (m, "comment_before");
		if (haskey (m, "comment_after"))
		    m = remove (m, "comment_after");
		return m;
	    });
	    decl["options"] = options;
	}
	if (haskey (decl, "directives"))
	{
	    list<map> directives = (list<map>)(decl["directives"]:[]);
	    directives = maplist (map m, directives, ``{
		if (haskey (m, "comment_before"))
		    m = remove (m, "comment_before");
		if (haskey (m, "comment_after"))
		    m = remove (m, "comment_after");
		return m;
	    });
	    decl["directives"] = directives;
	}
	return decl;
    });
    exported["settings"] = settings;
    return exported;
}

any ret = nil;
string func = "";
map param = $[];

/* Check arguments */
if(size(WFM::Args()) > 0 && is(WFM::Args(0), string)) {
    func = (string)(WFM::Args(0));
    if(size(WFM::Args()) > 1 && is(WFM::Args(1), map))
        param = (map)(WFM::Args(1));
}
y2debug("func=%1", func);
y2debug("param=%1", param);

/* Create a summary*/
if(func == "Summary") {
    ret = select(DhcpServer::Summary([]), 0, "");
}
/* Reset configuration */
else if (func == "Reset") {
    DhcpServer::Import($[]);
    ret = $[];
}
/* Were the settings modified? */
else if (func == "GetModified") {
    ret = DhcpServer::GetModified ();
}
/* Mark settings as modified */
else if (func == "SetModified") {
    DhcpServer::SetModified ();
    ret = true;
}
/* Change configuration (run AutoSequence) */
else if (func == "Change") {
    ret = DhcpServerUI::DhcpAutoSequence();
}
/* Import configuration */
else if (func == "Import") {
    ret = DhcpServer::Import(param);
}
/* Return actual state */
else if (func == "Export") {
    ret = KillComments (DhcpServer::Export());
}
/* Return needed packages */
else if (func == "Packages") {
    ret = DhcpServer::AutoPackages();
}
/* Read current state */
else if (func == "Read") {
    import "Progress";
    boolean progress_orig = Progress::set (false);
    ret = DhcpServer::Read();
    Progress::set (progress_orig);
}
/* Write givven settings */
else if (func == "Write") {
    import "Progress";
    boolean progress_orig = Progress::set (false);
    DhcpServer::SetWriteOnly (true);
    ret = DhcpServer::Write();
    Progress::set (progress_orig);
}
/* Unknown function */
else {
    y2error("Unknown function: %1", func);
    ret = false;
}

y2debug("ret=%1", ret);
y2milestone("DhcpServer auto finished");
y2milestone("----------------------------------------");

return ret;

}


