/**
 * File:	lan/cmdline.ycp
 * Package:	Network configuration
 * Summary:	Network cards cmdline handlers
 * Authors:	Michal Svec <msvec@suse.cz>
 *
 * $Id: cmdline.ycp 21544 2005-02-11 16:24:46Z mvidner $
 */

{

textdomain "network";

import "CommandLine";
import "Label";
import "Lan";
import "NetworkDevices";
import "RichText";

/**
 * Handler for action "list"
 * @param options action options
 */
define boolean ListHandler(map<string, string> options) {

    boolean cf = false;
    boolean uncf = false;
    if(options["configured"]:nil != nil) cf = true;
    if(options["unconfigured"]:nil != nil) uncf = true;

    string summary = "";
    if(cf == false && uncf == false)
	/* Headline */
	summary = "\n" + _("Network Configuration Summary:") + "\n\n" +
	    RichText::Rich2Plain(Lan::Summary("summary")[0]:"") + "\n";
    else {
	list l = Lan::Summary("split");
	if(cf) summary = RichText::Rich2Plain(sformat("%1", l[0]:"")) + "\n";
	/* Headline */
	if(uncf) summary = _("Available Devices:") + "\n" +
	    sformat("%1", mergestring(maplist(term i, l[1]:[], { return "* " + i[1]:"" + "\n"; } ), ""));
	summary = "\n" + summary;
    }

    y2debug("%1", summary);
    CommandLine::Print(summary);
    return true;
}

/**
 * Handler for action "add"
 * @param options action options
 */
define boolean AddHandler(map<string, string> options) {
    string dev = options["device"]:"";

    /* Progress text */
    CommandLine::Print(sformat(_("Adding Device: %1"), dev));

    if(!Lan::Add()) {
	CommandLine::Print(Label::ErrorMsg());
	return false;
    }

    if(options["ipaddr"]:"" != "") Lan::ipaddr = options["ipaddr"]:"";
    if(options["netmask"]:"" != "") Lan::netmask = options["netmask"]:"";
    if(options["bootproto"]:"" != "") Lan::bootproto = options["bootproto"]:"";
    Lan::type = NetworkDevices::device_type(dev);
    Lan::device = NetworkDevices::device_num(dev);

    Lan::Commit();
    /* Progress result */
    CommandLine::Print(_("Success"));
    return true;
}

/**
 * Handler for action "delete"
 * @param options action options
 */
define boolean DeleteHandler(map<string, string> options) {
    string dev = options["device"]:"";

    /* Progress text */
    CommandLine::Print(sformat(_("Deleting Device: %1"), dev));

    if(Lan::Delete(dev) && Lan::Commit())
	/* Progress result */
	CommandLine::Print(_("Success"));
    else
    {
	/* Progress result */
	CommandLine::Print(Label::ErrorMsg());
	return false;
    }
    return true;
}

/* EOF */
}
