/**
 * File:	include/network/install.ycp
 * Package:	Network configuration
 * Summary:	Installation routines
 * Authors:	Michal Svec <msvec@suse.cz>
 *
 * $Id: install.ycp 20257 2004-11-09 13:19:45Z mvidner $
 *
 * All install.inf settings are stored in a global variable InstallInf.
 */

{

textdomain "network";

import "Hostname";
import "NetworkDevices";

/**
 * Read one install.inf item
 * @param item InstallInf map key
 * @param var install.inf SCR variable
 * @return true on success
 */
define boolean ReadInstallInfItem(path install_inf, string item, string var) {
    if(item == nil || item == "" || var == nil || var == "") return false;
    string val = (string) SCR::Read(add(install_inf, var));
    if(val == nil) return false;
    if(val != "") InstallInf[item] = val;
    return true;
}

define string DeleteFirstWord (string s) {
    string ret = regexpsub (s, "^[^ ]* +(.*)", "\\1");
    return (ret == nil)? s: ret;
}

/**
 * Read install.inf from the correct location
 * @return true on success
 */
define boolean ReadInstallInf() {

    InstallInf = $[];

    /* Detect install.inf location */
    path install_inf = nil;
    if(SCR::Read(.target.size,"/etc/install.inf") > 0) {
	install_inf = .etc.install_inf;
	InstallInf["installation"] = "yes";
    }
    /* FIXME
    else if(SCR::Read(.target.size,"/var/lib/YaST2/install.inf") > 0)
	install_inf = .var.lib.YaST2.install_inf;
    */
    else
	return false;

    /* Read install.inf items */
    ReadInstallInfItem(install_inf, "ipaddr", "IP");
    ReadInstallInfItem(install_inf, "netmask", "Netmask");
    ReadInstallInfItem(install_inf, "bootproto", "NetConfig");
    ReadInstallInfItem(install_inf, "netdevice", "Netdevice");
    ReadInstallInfItem(install_inf, "gateway", "Gateway");
    ReadInstallInfItem(install_inf, "nameserver", "Nameserver");
    ReadInstallInfItem(install_inf, "hostname", "Machinename");
    ReadInstallInfItem(install_inf, "domain", "Domainname");
    ReadInstallInfItem(install_inf, "module", "Alias");
    ReadInstallInfItem(install_inf, "options", "Options");
    ReadInstallInfItem(install_inf, "hwaddr", "HWAddr");

    /* Split network device */
    string netdevice = InstallInf["netdevice"]:"";
    if(netdevice != "") {
	string devtype = NetworkDevices::device_type(netdevice);
	if(devtype != nil && devtype != "")
	    InstallInf["type"] = devtype;
	InstallInf = remove(InstallInf, "netdevice");
    }

    /* Split FQ hostname */
    string domain = InstallInf["domain"]:"";
    string hostname = InstallInf["hostname"]:"";
    if(hostname != "") {
	list split = Hostname::SplitFQ(hostname);
	InstallInf["hostname"] = split[0]:hostname;
	if(domain == "")
	    InstallInf["domain"] = split[1]:"";
    }

    // FIXME: alias = eth0 tulip
    // FIXME: options = ne io=0x200

    // #42203: correctly parse module and options for proposal
    // "eth0 qeth" -> "qeth"
    // FIXME: this only works for a single module
    string mod = InstallInf["module"]:"";
    if (mod != "") {
	InstallInf["module"] = DeleteFirstWord (mod);
    }
    string options = InstallInf["options"]:"";
    if (options != "") {
	InstallInf["options"] = DeleteFirstWord (options);
    }

    y2milestone("InstallInf(%1)", InstallInf);
    return true;
}

/* EOF */
}
