/**
 * File:	clients/power-management_auto.ycp
 * Package:	Configuration of power-management
 * Summary:	Client for autoinstallation
 * Authors:	Jiri Srain <jsrain@suse.cz>
 *
 * $Id: power-management_auto.ycp 19957 2004-10-26 14:03:47Z 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 power-management 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 ("power-management_auto", [ "Summary", mm ]);
 */

{

textdomain "power-management";

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

import "Mode";
import "PowerManagement";
include "power-management/wizards.ycp";

/**
 * Fill the new_schemes map so that schemes that weren't existing before
 * importing have their config file initialized before modifying it
 * @param settings the import map
 */
define void CreateNewSchemesMap (map settings) {
    if (! Mode::autoinst ())
	return;
    list<string> r_schemes = (list<string>)
        SCR::Dir (.sysconfig.powersave.schemes.section);
    if (size (r_schemes) == 0) // no scheme file to clone
	return;
    list<map<string,string> > schemes = settings["schemes"]:[];
    foreach (map<string,string> s, schemes, {
	string id = s["_scheme_id"]:"";
	if (! contains (r_schemes, id))
	{
	    PowerManagement::new_schemes[id] = r_schemes[0]:"";
	}
    });
}

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(PowerManagement::Summary(), 0, "");
}
/* Reset configuration */
else if (func == "Reset") {
    PowerManagement::Import($[]);
    ret = $[];
}
/* Change configuration (run AutoSequence) */
else if (func == "Change") {
    ret = PowerManagementAutoSequence();
}
/* Import configuration */
else if (func == "Import") {
    CreateNewSchemesMap (param);
    ret = PowerManagement::Import(param);
}
/* Return actual state */
else if (func == "Export") {
    ret = PowerManagement::Export();
}
/* did configuration change */
else if (func == "GetModified") {
    ret = PowerManagement::modified;
}
/* set configuration as changed */
else if (func == "SetModified") {
    PowerManagement::modified = true;
    ret = true;
}
/* Return needed packages */
else if (func == "Packages") {
    ret = PowerManagement::AutoPackages();
}
/* Read current state */
else if (func == "Read") {
    import "Progress";
    Progress::off();
    ret = PowerManagement::Read();
    Progress::on();
}
/* Write givven settings */
else if (func == "Write") {
    import "Progress";
    Progress::off();
    PowerManagement::write_only = true;
    ret = PowerManagement::Write();
    Progress::on();
}
/* Unknown function */
else {
    y2error("Unknown function: %1", func);
    ret = false;
}

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

return ret;

/* EOF */
}
