/**
 *
 * File:
 *   routines.ycp
 *
 * Module:
 *   Network/YPServer
 *
 * Summary:
 *   YPServer module.
 *
 * Authors:
 *   Dan Vesely <dan@suse.cz>
 *   Martin Vidner <mvidner@suse.cz>
 *
 * $Id: routines.ycp 20204 2004-11-05 09:19:26Z jsuchome $
 *
 * YPServer module part.
 *
 */

{
    textdomain "nis_server";

    import "CWMFirewallInterfaces";
    import "NisServer";
    import "Popup";
    import "Service";

    /**
     * Converts from any type to boolean:
     * true, "true" and nonzero integers are true,
     * everything else including nil is false.
     * @param arg argument to convert
     * @return converted value
     */
    define boolean toboolean (any arg) ``{
	if (arg == nil) return false;
	else if (is (arg, boolean)) return arg;
	else if (is (arg, string))  return arg == "true";
	else if (is (arg, integer)) return arg != 0;
	else return false;
    }

    /**
     * @return Checks if the YP server was already configured
     * by reading the SCR, not the module data
     * (not to confuse the user if he backs up to the first dialog)
     */
    define boolean isYPServerConfigured () ``{
	return Service::Enabled ("ypserv");
    }

    /**
     * Decides whether slave exists
     * (according to makefile.NOPUSH)
     * @return `have_slave or `none_slave
     */
    define symbol SlaveExists () ``{
	return NisServer::nopush? `none_slave: `have_slave;
    }

    // unused
    /**
     * @return Checks if this host is in the slaves list on master
     */
    define boolean hasMasterThisSlave () ``{
	// list of maps: $["host": "foo.com", "map":"passwd.byuid"]
	list<map> running_maps = (list<map>) SCR::Read (.run.ypwhich_m);
	if (running_maps != nil)
	{
	    list<string> running_maps_str = maplist (map m, running_maps, ``(m["map"]:""));
	    if (contains (running_maps_str, "ypservers"))
	    {
		map out	= (map)SCR::Execute (.target.bash_output, "/usr/bin/ypcat ypservers");
		string slaves = out["stdout"]:"";
		if (slaves != nil)
		{
		    out	= (map)SCR::Execute (.target.bash_output, "/usr/lib/yp/yphelper --hostname");
		    string hostname = out["stdout"]:"";
		    if (hostname == "")
		    {
			return false;
		    }
		    hostname = substring (hostname, 0, size (hostname) - 1);
		    if (contains (splitstring (slaves, "\n"), hostname))
		    {
			return true;
		    }
		}
	    }
	}
	return false;
    }

    /**
     * Do the check if DHCP client is able to change domain name and
     * warn user about this (see bug #28727)
     */
    define void CheckForDHCPClient (string domain) {

	if (NisServer::dhcp_changes_domain)
	{
	    if (NisServer::start_ypbind)
	    {
		Popup::Warning (
// warning popup
_("Your machine is set up to change the NIS domain name via DHCP.
This may replace the domain name just entered. Check your
settings and consider not running a DHCP client on a NIS server.
"));
	    }
	}
	// TODO: check
	// if domain from dhcp client is different then the one currently set
    }


    /**
     * Create the widget for opening firewall ports
     */
    define map<string,any> GetFirewallWidget () {

	map<string,any> settings = $[
	    "services"	: [ "nis-server" ],
	    "display_details" : true,
	    // firewall openning help
	    "help" : _("<p><b>Firewall Settings</b><br>
To open the firewall to allow accessing the NIS server
from remote computers, set <b>Open Port in Firewall</b>.
To select interfaces on which to open the port, click <b>Firewall Details</b>.
This option is only available if the firewall is enabled.</p>
"),
	];
	return CWMFirewallInterfaces::CreateOpenFirewallWidget (settings);
    }
}
