/**
 * File:	modules/SLP.ycp
 * Package:	SLP Browser / Agent
 * Summary:	Access to SLP Agent functions
 * Authors:	Anas Nashif <nashif@suse.de>
 *
 * $Id: SLP.ycp,v 1.11 2004/09/17 01:23:41 nashif Exp $
 *
 */
{

    module "SLP";
    textdomain "slp";
    import "Summary";
    import "HTML";


    string Regd = "/etc/slp.reg.d";

    /**
      * Issue the query for services
      * @param pcServiceType The Service Type String, including authority string if
      * any, for the request, such as can be discovered using  SLPSrvTypes(). 
      * This could be, for example "service:printer:lpr" or "service:nfs".
      * @param pcScopeList comma separated  list of scope names to search for
      * service types.
      * @return list<map> List of Services
      */
    global define list<map> FindSrvs( string pcServiceType, string pcScopeList)
    {
        list<map> Srvs = (list<map>) SCR::Read(.slp.findsrvs,
                $["pcServiceType":pcServiceType, "pcScopeList": pcScopeList] );
        y2debug("FindSrvs: %1",  Srvs);
        return Srvs;
    }

    /**
      * Issues an SLP service type request for service types in the scopes
      * indicated by the pcScopeList.
      *
      * If the naming authority is "*", then
      * results are returned for all naming authorities.  If the naming
      * authority is the empty string, i.e.  "", then the default naming
      * authority, "IANA", is used.
      *
      * @param pcNamingAuthority The naming authority to search.
      * @param pcScopeList  comma separated  list of scope names to search for
      * service types.
      * @return list<string> Service Types
      */
    global define list<string> FindSrvTypes ( string pcNamingAuthority , string pcScopeList)
    {
        list<string> Types = (list<string>) SCR::Read(.slp.findsrvtypes,
                                $["pcNamingAuthority":pcNamingAuthority ,
                                  "pcScopeList": pcScopeList] );
        return Types;
    }



    /**
      * Find attributes of a service
      * @param pcURLOrServiceType service url or type
      * @return list<string> attributes
      */
    global define list<string> FindAttrs ( string pcURLOrServiceType )
    {
        list<string> Attrs = (list<string>) SCR::Read(.slp.findattrs,
                                $["pcURLOrServiceType":pcURLOrServiceType] );
        return Attrs;
    }


    /**
      * Find attributes of a service and return a map
      * @param pcURLOrServiceType service url or type
      * @return map<string,string> attributes
      */
    global define map<string,string> GetAttrMap(string pcURLOrServiceType)
    {

            list<string> Attrs = (list<string>) SCR::Read(.slp.findattrs,
                    $["pcURLOrServiceType":pcURLOrServiceType] );
            map<string, string> att = (map<string, string>)listmap(string a, Attrs, ``{
                    string s = substring(a, 1, size(a) - 2 );
                    list aa = splitstring(s, "=");
                    return($[aa[0]:"empty":aa[1]:"empty"]);
                    });
            return att;
        }

    /**
      * Attribute summary
      * @param Attrs attribute list
      * @return string summary
      */
    global define string AttrSummary (list<string> Attrs)
    {
       string summary = "";
       summary = Summary::AddHeader(summary, _("Attributes"));
       summary = Summary::OpenList      ( summary );
       foreach(string a, Attrs, ``{
               string s = substring(a, 1, size(a) - 2 );
               list aa = splitstring(s, "=");
               summary = Summary::AddListItem   ( summary, sformat("%1: %2",
                   HTML::Bold(aa[0]:""), aa[1]:"") );
               });

       summary = Summary::CloseList     ( summary );
       return summary;
    }

    /**
      * Register service with SLP
      * @param service Service to be registered
      * @return boolean True on success
      */
    global define boolean Reg(string service)
    {
        boolean ret = (boolean)SCR::Execute(.slp.reg, service);
        return ret;
    }

    /**
      * Deregister service with SLP
      * @param service Service to be deregistered
      * @return boolean True on success
      */
    global define boolean DeReg(string service)
    {

        boolean ret = (boolean)SCR::Execute(.slp.dereg, service);
        return ret;
    }

    /**
      * Register service with SLP using a reg file
      * @param service The service to be registered
      * @param attr Attributes
      * @param regfile Reg File
      * @return boolean True on Success
      */
    global define boolean RegFile(string service, map<string,string> attr, string regfile)
    {
        list<string> slp = [];
        slp=add(slp, service);
        foreach(string k, string v , attr, ``{
                slp=add(slp, sformat("%1=%2", tolower(k), v ));
                });

        string all = mergestring(slp, "\n");
        SCR::Execute(.target.mkdir, Regd );
        boolean ret =(boolean) SCR::Write(.target.string,    sformat("%1/%2", Regd, regfile), all);
        return ret;
    }

    /**
      * De-Register service with SLP by removing the reg file
      * @param regfile The service to be deregistered
      * @return boolean True on success
      */
    global define boolean DeRegFile(string regfile)
    {
        boolean ret = (boolean)SCR::Execute(.target.remove, regfile);
        return ret;
    }
    /**
     * Match Srv Type and return all data
     * @param match match string
     * @return list<map> list of services matching with all relevant data
     */
    global define list<map> MatchType (string match )
    {
        list<string> t = FindSrvTypes("*", "");
        list<map> ret = [];
        foreach(string type , t, {
                if (regexpmatch(type, match)) {
                    list<map> matched = FindSrvs(type, "");
                    ret = (list<map>) union(ret , maplist(map m, matched, ``{
                            m["attr"] = GetAttrMap(m["srvurl"]:"");
                            return(m);
                            }));
                }
                });
       return ret;
    }
}
