/**
 * Module:      Linuxrc
 * File:	modules/Linuxrc.ycp
 * Purpose:	Interaction with linuxrc
 *
 * Author:	Anas Nashif <nashif@suse.de?
 * $Id: Linuxrc.ycp 21650 2005-02-16 17:22:35Z nashif $
 */
{
    module "Linuxrc";
    import "Mode";
    import "Stage";

    map<string,string> install_inf = nil;

    boolean _manual = nil;


// routines for reading data from /etc/install.inf

    void ReadInstallInf () {
	install_inf = $[];
	// don't read anything if the file doesn't exist
	if (SCR::Read (.target.size, "/etc/install.inf") == -1)
	{
	    y2error ("Reading install.inf, but file doesn't exist!!!");
	    return;
	}
	list<string> entries = (list<string>)SCR::Dir (.etc.install_inf);
	if (entries == nil)
	{
	    y2error ("install.inf is empty");
	    return;
	}
	foreach (string e, entries, {
	    string val = (string)SCR::Read (add (.etc.install_inf, e));
	    install_inf[e] = val;
	});
    }

    global void ResetInstallInf () {
        install_inf = nil;
        return;
    }

    global string InstallInf (string key) {
	if (install_inf == nil)
	    ReadInstallInf ();
	return install_inf[key]:nil;
    }

    // installation mode wrappers

    global boolean manual () {
	if (_manual != nil)
	    return _manual;
	_manual = InstallInf ("Manual") == "1";
	if (! _manual)
	{
	    string tmp = (string)SCR::Read (.target.string, "/proc/cmdline");
	    if (tmp != nil && contains (splitstring (tmp, " \n"), "manual"))
	    {
		_manual = true;
	    }
	}
	return _manual;
    }

    /**
     * running via serial console
     */
    global boolean serial_console () {
	 return InstallInf ("Console") != nil;
    }

    /**
     * braille mode ?
     */
    global boolean braille () {
	return InstallInf ("Braille") != nil;
    }

    /**
     * vnc mode ?
     */
    global boolean vnc () {
	return InstallInf ("VNC") == "1";
    }

    /**
     * ssh mode ?
     */
    global boolean usessh () {
	return InstallInf ("UseSSH") == "1";
    }

    /**
     * we're running in textmode (-> UI::GetDisplayInfo())
     */
    global boolean text () {
	return InstallInf ("Textmode") == "1";
    }

// end of install.inf reading routines

    /**
     * Write /etc/yast.inf during installation
     * @param linuxrc	map of key value pairs for /etc/yast.inf
     * @return void
     */
    global define void WriteYaSTInf (map<string,string> linuxrc)
    {
	string yast_inf = "";
	foreach (string ykey, string yvalue, linuxrc,
	``{
	    yast_inf = yast_inf + ykey + ": " + yvalue + "\n";
	});
	y2milestone ("WriteYaSTInf(%1) = %2", linuxrc, yast_inf);

        if (!Mode::test ())
        {
	    WFM::Write(.local.string, "/etc/yast.inf", yast_inf);
        }
        return;
    }

    /*
     * Copy /etc/install.inf into built system so that the
     * second phase of the installation can find it.
     * @param root mount point of system
     * @return boolean true on success
     */
    global boolean SaveInstallInf(string root)
    {
        if (Stage::initial () && !Mode::test () )
        {
            SCR::Execute (.target.bash, "/bin/grep -v Sourcemounted /etc/install.inf >" +
                    root + "/etc/install.inf");

            // just for debug so we can see the original install.inf later
            SCR::Execute (.target.bash, "/bin/cp /etc/install.inf " +
                    root + "/var/lib/YaST2/install.inf");
            SCR::Execute (.target.bash, "/bin/chmod 0600 " +
                    root + "/var/lib/YaST2/install.inf");
        }
        return true;
    }
}
