/**
 * File:	modules/Hostname.ycp
 * Package:	yast2
 * Summary:	Hostname manipulation routines
 * Authors:	Michal Svec <msvec@suse.cz>
 *
 * $Id: Hostname.ycp 21390 2005-02-08 09:27:01Z jsuchome $
 */

{

module "Hostname";
textdomain "base";

import "IP";

global string ValidChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-";
global string ValidCharsDomain = ValidChars + ".";
global string ValidCharsFQ = ValidCharsDomain;

/**
 * describe a valid domain name
 * @return description
 */
global string ValidDomain () {
    //Translators: dot: ".", hyphen: "-"
    return _("A valid domain name consists of components separated by dots.
Each component contains letters, digits, and hyphens. A hyphen may not
start or end a component and the last component may not begin with a digit.");
}

/**
 * describe a valid host name
 * @return description
 */
global string ValidHost () {
    //Translators: hyphen: "-"
    return _("A valid host name consists of letters, digits, and hyphens.
A host name may not begin or end with a hyphen.
");
}

/**
 * describe a valid FQ host name
 * @return describe a valid FQ host name
 */
global string ValidFQ() {
    return ValidDomain();
}

/**
 * Check syntax of hostname entry
 * (that is a domain name component, unqualified, without dots)
 * @see rfc1123, rfc2396 and obsoleted rfc1034
 * @param host hostname
 * @return true if correct
 */
global define boolean Check(string host) ``{
    if (host == nil || host == "" || size(host) > 63) return false;
    return regexpmatch(host, "^[[:alnum:]]([[:alnum:]-]*[[:alnum:]])?$");
}

/**
 * Check syntax of domain entry
 * @param domain domain name
 * @return true if correct
 */
global define boolean CheckDomain(string domain) ``{
    if (domain == nil || domain == "") return false;
    list <string> l = splitstring(domain, ".");
    if (contains (maplist(string h, l, ``(Check(h))), false)) return false;
    return !regexpmatch (domain, "\\.[[:digit:]][^.]*$");
}

/**
 * Check syntax of fully qualified hostname
 * @param host hostname
 * @return true if correct
 */
global define boolean CheckFQ(string host) ``{
    return CheckDomain(host);
}

/**
 * Split FQ hostname to hostname and domain name
 * @param fqhostname FQ hostname
 * @return list of hostname and domain name
 * @example Hostname::SplitFQ("ftp.suse.cz") -> ["ftp", "suse.cz"]
 * @example Hostname::SplitFQ("ftp") -> ["ftp"]
 */
global define list<string> SplitFQ(string fqhostname) ``{
    if (fqhostname == "" || fqhostname == nil) {
	y2error("Bad FQ hostname: %1", fqhostname);
	return [];
    }

    string hn = "";
    string dn = "";

    integer dot = findfirstof(fqhostname, ".");
    if (dot != nil) {
	hn = substring(fqhostname, 0, dot);
	dn = substring(fqhostname, dot+1);
	return [ hn, dn ];
    }
    else {
	hn = fqhostname;
	return [ hn ];
    }

    return [ hn, dn ];
}

/**
 * Merge short hostname and domain to full-qualified host name
 * @param hostname short host name
 * @param domain domain name
 * @return FQ hostname
 */
global define string MergeFQ(string hostname, string domain) {
    if(domain == "" || domain == nil) return hostname;
    return hostname + "." + domain;
}

/* EOF */
}
