/**
 * File:	src/modules/Remote.ycp
 * Module:	Network configuration
 * Summary:	Module for Remote Administration via VNC
 * Authors:	Arvin Schnell <arvin@suse.de>
 *		Martin Vidner <mvidner@suse.cz>
 *
 * $Id: Remote.ycp 19988 2004-10-27 09:37:05Z mvidner $
 *
 */

{

module "Remote";
textdomain "network";

import "Label";
import "Mode";
import "Package";
import "Service";

/**
 * Allow remote administration
 */
global boolean allow_administration = false;

/**
 * Reset all module data.
 */
global void Reset() {
    allow_administration = false;
}

/**
 * Read the current status
 * @return true on success
 */
global boolean Read() {
    boolean xdm = Service::Enabled ("xdm");
    boolean dm_ra = (string) SCR::Read (.sysconfig.displaymanager.DISPLAYMANAGER_REMOTE_ACCESS) == "yes";

    boolean xinetd = Service::Enabled ("xinetd");
    // are the proper services enabled in xinetd?
    list<map> xinetd_conf = (list<map>) SCR::Read(.etc.xinetd_conf.services);
    list<map> vnc_conf = filter (map m, xinetd_conf, {
	string s = m["service"]:"";
	return s == "vnc1" || s == "vnchttpd1";
    });
    boolean vnc = size (vnc_conf) == 2 &&
	vnc_conf[0, "enabled"]:false &&
	vnc_conf[1, "enabled"]:false;

    y2milestone ("XDM: %1, DM_R_A: %2", xdm, dm_ra);
    y2milestone ("xinetd: %1, VNC: %2", xinetd, vnc);
    allow_administration = xdm && dm_ra && xinetd && vnc;
    return true;
}

/**
 * Update the SCR according to network settings
 * @return true on success
 */
global boolean Write() {

    if(allow_administration) {
	/* Install require packages */
	list<string> packages = ["xinetd", "tightvnc", "xorg-x11", "xorg-x11-Xvnc", ];
	// #40155: have to specify a concrete window manager,
	// "windowmanager" only works for yast2 -i
	if (!Package::Installed ("kdebase3"))
	{
	    packages = add (packages, "fvwm2");
	}
	if(!Package::InstallAll(packages)) {
	    y2error("Installing of required packages failed");
	    return false;
	}

	/* Enable xinetd */
	if(!Service::Enable("xinetd")) {
	    y2error("Enabling of xinetd failed");
	    return false;
	}

	/* Enable XDM */
	if(!Service::Enable("xdm")) {
	    y2error("Enabling of xdm failed");
	    return false;
	}
    }

    /* Set DISPLAYMANAGER_REMOTE_ACCESS in sysconfig/displaymanager */
    SCR::Write(.sysconfig.displaymanager.DISPLAYMANAGER_REMOTE_ACCESS,
		allow_administration ? "yes" : "no");
    SCR::Write(.sysconfig.displaymanager.DISPLAYMANAGER_ROOT_LOGIN_REMOTE,
		allow_administration ? "yes" : "no");
    SCR::Write(.sysconfig.displaymanager, nil);

    // Enable/disable vnc1 and vnchttpd1 in xinetd.d/vnc
    // If the port is changed, change also the help in remote/dialogs.ycp
    // The agent is in yast2-inetd.rpm
    list<map> xinetd = (list<map>) SCR::Read(.etc.xinetd_conf.services);
    xinetd = maplist (map m, xinetd, {
	string s = m["service"]:"";
	if (s == "vnc1" || s == "vnchttpd1") {
	    m["changed"] = true;
	    m["enabled"] = allow_administration;
	}
	return m;
    });
    SCR::Write(.etc.xinetd_conf.services, xinetd);

    if(Mode::normal ()) {
	SCR::Execute(.target.bash, "/sbin/SuSEconfig --module xdm");
	SCR::Execute(.target.bash, "/sbin/SuSEconfig --module kde3");
	SCR::Execute(.target.bash, "/sbin/SuSEconfig --module kdm3");

	boolean dm_was_running = Service::Status("xdm") == 0;
	if(allow_administration) {
	    SCR::Write(.etc.inittab.id, "5:initdefault:");
	    SCR::Write(.etc.inittab, nil);

	    Service::Restart("xinetd");
	    if (!dm_was_running)
	    {
		//#41611: with Service::Start, yast hangs :-(
		SCR::Execute (.target.bash_background, "/etc/init.d/xdm start");
	    }
	}
	else
	{
	    // xinetd may be needed for other services so we never turn it
	    // off. It will exit anyway if no services are configured.
	    // If it is running, restart it.
	    Service::RunInitScript ("xinetd", "try-restart");
	}

	// FIXME: reload XDM (#40458)
	// For now, inform the user
	if (dm_was_running)
	{
	    import "Report";
	    Report::Message (sformat (
	    // message popup
	    // %1 is a system command
	    // Note: it is a DISPLAY manager, not a WINDOW manager
_("For the settings to take effect, the display manager
must be restarted. Because this terminates all X Window System
sessions, do it manually from the console with
\"%1\".
Note that restarting the X server alone is not enough."),
"rcxdm restart"));
	}
    }

    return true;
}

/**
 * Create summary
 * @return summary text
 */
global define string Summary() {

    if(allow_administration) {
	/* Label in proposal text */
	return _("Remote administration is enabled.");
    }
    else {
	/* Label in proposal text */
	return _("Remote administration is disabled.");
    }

}

/* EOF */
}
