/**
 * File:	include/network/runtime.ycp
 * Package:	Network configuration
 * Summary:	Runtime routines
 * Authors:	Michal Svec <msvec@suse.cz>
 *
 * $Id: runtime.ycp 21025 2005-01-27 16:38:39Z mvidner $
 */

{

import "Arch";
import "Desktop";
import "ISDN";
import "Mode";
import "NetworkDevices";
import "Package";
import "Service";

textdomain "network";

/**
 * Read status information
 * @return true if success
 */
define boolean ReadStatus() {
    return true;
}

/**
 * Run SuSEconfig
 * @return true if success
 */
define boolean RunSuSEconfig() {
    y2milestone("Running SuSEconfig ...");
    SCR::Execute(.target.bash, "/sbin/SuSEconfig --module apache 2>/dev/null");
    SCR::Execute(.target.bash, "/sbin/SuSEconfig --module sendmail 2>/dev/null");
    SCR::Execute(.target.bash, "/sbin/SuSEconfig --module postfix 2>/dev/null");
    return true;
}

/**
 * Run one SuSEconfig module
 * @param modul SuSEconfig module
 * @return true if success
 */
define boolean RunSuSEconfigModule(string modul) {
    if(modul == nil || modul == "") {
	y2error("Wrong SuSEconfig module: %1", modul);
	return false;
    }
    y2milestone("Running SuSEconfig module %1", modul);
    return SCR::Execute(.target.bash, sformat("/sbin/SuSEconfig --module %1", modul)) == 0;
}

/**
 * Check if running on a local system or over a network
 * @return true if running on a local system
 * /
boolean RunningLocal() {
    import "DNS";
    import "Hostname";

    string domain = DNS::domain;
    string hostname = DNS::hostname;
    string fqhostname = Hostname::MergeFQ(hostname, domain);
    string display = lookup((map) SCR::Execute(.target.bash_output, "echo $DISPLAY"), "output", nil);
    string ssh_client = lookup((map) SCR::Execute(.target.bash_output, "echo $SSH_CLIENT"), "output", nil);

    y2milestone("display=%1", display);
    y2milestone("ssh_client=%1", ssh_client);
    y2milestone("fqhostname=%1", fqhostname);

    /* $SSH_CLIENT:
     * if(not set)	local
     * else		REMOTE
     * /
    if(ssh_client != "" && ssh_client != nil) return false;

    /* $DISPLAY:
     *			local
     * :0.0		local
     * localhost:0.0	local
     * beholder:0.0	local
     * wyvern:11.0	REMOTE
     * /
    if(display != "" && display != nil) {
	string dhost = splitstring(display, ":")[0]:"";
	if(dhost != "" && dhost != nil && dhost != "localhost" &&
	    dhost != hostname && dhost != fqhostname) return false;
    }
    return true;
}

/**
 * Link detection
 * @return true if link found
 * @see ethtool(8)
 */
boolean HasLink() {
    string ifname = "eth0";

    string command = sformat("ethtool %1 | grep -q 'Link detected: no'", ifname);
    if((integer) SCR::Execute(.target.bash, command) == 1) return false;
    return true;
}

/**
 * Reload network configuration. If it was not running, start it instead
 * @return true if success
 */
define boolean ReloadOrStartNetwork() {
    // Workaround for bug #46055:
    // Go to the directory which is hopefully not mounted over the
    // network so that yast survives network restart.
    y2milestone("Reloading service %1", "network");
    // rcnetwork reload works even if it was not running
    string cmd = "cd /; /etc/init.d/network reload";
    return SCR::Execute (.target.bash, cmd) == 0;
}

/**
 * Stop network
 * @return true if success
 */
define boolean StopNetwork() {
    // Workaround for bug #46055:
    y2milestone("Stopping service %1", "network");
    string cmd = "cd /; /etc/init.d/network stop";
    return SCR::Execute (.target.bash, cmd) == 0;
}

/**
 * Enable the network service
 * @return true if success
 */
define boolean EnableNetwork() {
    // Workaround for bug #46055:
    y2milestone("Enabling service %1", "network");
    string cmd = "cd /; /sbin/insserv -d /etc/init.d/network";
    return SCR::Execute (.target.bash, cmd) == 0;
}

/**
 * Are there interfaces controlled by smpppd and kinternet?
 * They are the ones with USERCONTROL=yes (#44303)
 * @return true/false
 */
boolean HaveDialupLikeInterfaces () {
    list <string> devs = NetworkDevices::Locate ("USERCONTROL", "yes");
    y2milestone ("user controlled interfaces: %1", devs);
    if (devs != [])
    {
	return true;
    }

    devs = ISDN::Locate ("USERCONTROL", "yes");
    y2milestone ("user controlled ISDN interfaces: %1", devs);

    return devs != [];
}

/**
 * Setup smpppd(8)
 * @return true if success
 */
define boolean SetupSMPPPD() {
    boolean ret = true;
    /* Stop and disable */
    if (!HaveDialupLikeInterfaces ()) {
	ret = Service::Disable("smpppd") && ret;
	ret = Service::Stop("smpppd") && ret;
    }
    /* Start and enable */
    else {
	ret = Service::Enable("smpppd") && ret;

	/* Installation? */
	if(Mode::normal ()) {
	    if(Service::Status("smpppd") == 0)
		ret = Service::Reload("smpppd") && ret;
	    else
		ret = Service::Start("smpppd") && ret;
	}
    }

    y2debug("ret=%1", ret);
    return ret;
}

/**
 * Run KInternet
 * @return true if success
 */
define boolean RunKInternet() {
    /* Any dialup devices? */
    if (!HaveDialupLikeInterfaces ())
    {
	y2milestone ("No dialup-like interfaces");
	return true;
    }

    /* Installation? */
    if(!Mode::normal ()) {
	y2milestone("No normal mode");
	return true;
    }

    /* Text mode? */
    map ui = UI::GetDisplayInfo();
    boolean ncurses = (boolean) ui["TextMode"]:nil;
    y2milestone("Text mode: %1", ncurses);
    if(ncurses == nil || ncurses == true) return true;

    string kdepath = "/opt/kde3/bin/";

    // Is it installed?
    string kinternet = kdepath + "kinternet";
    if ((integer) SCR::Read (.target.size, kinternet) < 0)
    {
	y2milestone ("%1 does not exist", kinternet);
	return true;
    }

    // run it by creating a file in /var/lib/Desktop
    Desktop::RunViaDesktop ("kinternet", ["--quiet"]);
}

/* EOF */
}
