/**
 * File:	runlevel.ycp
 * Module:	System Services (Runlevel) (formerly known as Runlevel Editor)
 * Summary:	Main File
 * Authors:	Martin Lazar <mlazar@suse.cz>, 2004
 *
 * $Id: runlevel.ycp 21394 2005-02-08 09:36:55Z jsuchome $
 *
 */
{

textdomain "runlevel";

import "RunlevelEd";
import "CommandLine";

include "runlevel/wizard.ycp";

boolean AddHandler(map<string,string> opts) {
    if (nil == CommandLine::UniqueOption( opts, ["service"])) 
	return false;
    list<string> rls = nil;
    if (opts["runlevels"]:nil != nil) {
	rls = splitstring(opts["runlevels"]:"", ",");
    }
    list<string> services = splitstring(opts["service"]:"", ",");
    foreach(string service, services, {
	integer ierr = RunlevelEd::ServiceInstall(service, rls);
	if (ierr == 1) {
	    CommandLine::Print(sformat(_("Error: service '%1' not found."), service));
	}
    });
    return true;
}

boolean DeleteHandler(map<string,string> opts) {
    if (nil == CommandLine::UniqueOption( opts, ["service"])) 
	return false;
    list<string> rls = nil;
    if (opts["runlevels"]:nil != nil) {
	rls = splitstring(opts["runlevels"]:"", ",");
    }
    list<string> services = splitstring(opts["service"]:"", ",");
    foreach(string service, services, {
	RunlevelEd::ServiceRemove(service, rls);
    });
    return true;
}

boolean SetHandler(map<string,string> opts) {
    if (nil == CommandLine::UniqueOption( opts, ["runlevel"])) 
	return false;;
    RunlevelEd::SetDefaultRunlevel(opts["runlevel"]:"5");
    return true;
}

void CommandLineTableDump(list<list> table)
{
    integer columns = 0;
    list<integer> len = [];
    integer totallen = 0;
    integer c = 0;
    foreach(list l, table, {
	columns = size(l) > columns ? size(l) : columns;
	c = 0;
	while(c<size(l)) {
	    if (l[c]:nil != nil) {
		len[c] = size(l[c]:"") > len[c]:0 ? size(l[c]:"") : len[c]:0;
	    }
	    c = c+1;
	}
    });
    c=0;
    while(c<columns) {
	totallen = totallen + len[c]:0 + 3;
	c = c+1;
    }
    if (totallen >= 80) {
	len[columns-1] = 80 - (totallen - len[columns-1]:0);
	if (len[columns-1]:0<3) len[columns-1] = 3;
    }
    foreach(list l, table, {
	string line = "";
	c=0;
	if (size(l)>0) {
	    while(c<columns) {
		totallen = size(line);
		line = line + l[c]:"";
		if (c<columns-1) {
		    while(size(line)<totallen+len[c]:0) {line=line+" ";}
		    line = line + " | ";
		}
		c=c+1;
	    }
	} else {
	    while(c<columns) {
		totallen = size(line);
		while(size(line)<totallen+len[c]:0) {line=line+"-";}
		if (c<columns-1) {
		    line = line + "-+-";
		}
		c=c+1;
	    }
	}
	CommandLine::Print(line);
    });
}

boolean SummaryHandler(map<string,string> opts) {
    list<string> service_names = nil;
    // translators: table headers
    list<list> services = [[_("Service"),_("Runlevels"),_("Description")],[]];
    map<string,boolean> rl = $[];
    if (opts["service"]:nil != nil) {
	service_names = (list<string>)union([ opts["service"]:"" ], RunlevelEd::ServiceDependencies(opts["service"]:"", true));
    } else {
	service_names = RunlevelEd::GetAvailableServices(true);
	CommandLine::Print(sformat(_("Default Runlevel after Boot: %1"), RunlevelEd::GetDefaultRunlevel()));
	CommandLine::Print(sformat(_("Current Runlevel: %1"), RunlevelEd::GetCurrentRunlevel()));
	CommandLine::Print("");
    }
    foreach(string service_name, service_names, {
	string description = RunlevelEd::GetServiceShortDescription(service_name);
	if (description == nil || size(description) == 0) {
	    description = RunlevelEd::GetServiceDescription(service_name);
	    if (findfirstof(description, "\n")!=nil) {
		description = substring(description, 0, findfirstof(description, "\n"));
	    }
	}
	list<string> runlevels = [];
	if (opts["defaults"]:nil != nil) {
	    runlevels = RunlevelEd::GetServiceDefaultStartRunlevels(service_name);
	} else {
	    runlevels = RunlevelEd::GetServiceCurrentStartRunlevels(service_name);
	}
	foreach(string r, runlevels, {rl[r]=true;});
	services = add(services, [service_name, runlevels, description]);
    });
    integer c = 0;
    while(c<size(services)) {
	if (services[c,1]:nil != nil && is(services[c,1]:nil, list)) {
	    list<string> rll = [];
	    foreach(string r, boolean l, rl, { rll=add(rll, contains(services[c,1]:[], r) ? r: " ");});
	    services[c,1] = mergestring(rll, " ");
	}
	c = c+1;
    }
    CommandLineTableDump(services);
}

map cmdline = $[
    "id": "runlevel",
    // translators: command line help text for runlevel module
    "help": _("Configuration of system services (runlevel)"),
    "guihandler": RunlevelSequence,
    "initialize": RunlevelEd::Read,
    "finish": RunlevelEd::Write,
    "actions": $[
	"summary": $[
	    // translators: command line help text for "summary" action
	    "help": _("Show a list of current system service status"),
	    "handler": SummaryHandler,
	],
	"add": $[
	    // translators: command line help text for "add" action
	    "help": _("Enable the service"),
	    "handler": AddHandler,
	],
	"delete": $[
	    // translators: command line help text for "delete" action
	    "help": _("Disable the service"),
	    "handler": DeleteHandler,
	],
	"set": $[
	    // translators: command line help text for "set" action
	    "help": _("Set default runlevel after boot"),
	    "handler": SetHandler,
	],
    ],
    "options": $[
	"runlevel" : $[
	    // translators: command line help text for "runlevel" option
	    "help": _("Specify default runlevel"),
	    "type": "enum",
	    "typespec": ["2", "3", "5"],
	],
	"runlevels" : $[
	    // translators: command line help text for "runlevels" option
	    "help": _("Comma separated list of runlevels"),
	    "type": "string",
	],
	"service": $[
	    // translators: command line help text for "service" option
	    "help": _("Comma separated service names"),
	    "type": "string",
	],
	"defaults": $[
	    // translators: command line help text for "defaults" option
	    "help": _("List default runlevels instead of current"),
	    "type": "",
	],
    ],
    "mappings": $[
	"summary": ["service", "defaults"],
	"add": ["service", "runlevels"],
	"delete": ["service", "runlevels"],
	"set": ["runlevel"],
    ]
];

y2milestone("----------------------------------------");
y2milestone("System service (runlevel) started");


any ret = CommandLine::Run(cmdline);
    
y2milestone("System service (runlevel) finished");
y2milestone("----------------------------------------");

return ret;

}
