/**
 * File:
 *      include/bootloader/routines/routines.ycp
 *
 * Module:
 *      Bootloader installation and configuration
 *
 * Summary:
 *      Several routines not using any variable of any module
 *
 * Authors:
 *      Jiri Srain <jsrain@suse.cz>
 *
 * $Id: routines.ycp 15168 2004-03-02 10:16:45Z jsrain $
 *
 */



{

    textdomain "bootloader";

    /**
      * Get value of some key in bootloader section
      * @param section list representating bootloader section
      * @param key string key of requested parameter
      * @param default any default value to be returned if key not present
      * @return any found key
      */
    define any lookup_value (list<map> section, string key,
	any default)
    ``{
	any ret = default;
	foreach (map m, section, ``{
	    if (m["key"]:"" == key)
		ret = m["value"]:default;
	});
	return ret;
    }

    /**
      * Get map describing value from section
      * @param section list representating bootloader section
      * @param key string key of requested parameter
      * @return map of option, empty map if not found
      */
    define map getValueMap (list<map> section, string key) ``{
	map ret = $[];
        foreach (map m, section, ``{
            if (m["key"]:"" == key)
                ret = m;
        });
        return ret;
    }

    /**
      * Append key with appropriate value to the end of the section
      * @param section list section list
      * @param key string key name
      * @param value any value to specified key
      * @return list modified section
      */
    define list<map> appendValue (list<map> section, string key, any value) ``{
	map m = $[ "key" : key, "value" : value, "changed" : true];
	return add (section, m);
    }

    /**
      * Lookup at value of entry with specified index in the section
      * @param section list section list
      * @param index integer index of entry in section
      * @param default any to be returned if specified entry not present
      * @return any value of specified entry of PPD file
      */
    define any lookupValueByIndex (list<map> section, integer index,
	any default)
    ``{
	return section[index, "value"]:default;
    }

    /**
      * Replace first occurence of option with specified value,
      *   remove other occurences, add if no occurence present
      * @param section list of section to be changed
      * @param key string key of requested parameter
      * @param value any value to be set, nil for removing key
      * @return list modified section
      */
    define list<map> modifySection (list<map> section, string key, any value)``{
	boolean found = false;
	section = (list<map>)maplist (map c, section, ``{
	    if (c["key"]:"" == key)
	    {
		found = true;
		if (value == nil)
		    return $[];
		else
		{
		    c["value"] = value;
		    c["changed"] = true;
		    value = nil;
		    return c;
		}
	    }
	    return c;
	});
        section = (list<map>)filter (map cc, section, ``(
	    cc != nil && cc["value"]:nil != nil
	));
	if (! found && value != nil)
	{
	    section = add (section, $[ "key" : key, "value" : value,
		"changed" : true]);
	}
	return section;
    }

    /**
      * Set value to the section
      * @param section list representing bootloader section
      * @param index index of the value, -1 = unknown, -2 = new option
      * @param key string option key
      * @param value any option value
      * @return list updated bootloader section
      */
    define list<map> setValueByIndex (list<map> section, integer index,
	string key, any value)
    ``{
	if (index < 0)
	{
	    if (index == -1)
	    {
		return modifySection (section, key, value);
	    }
	    else if (index == -2)
	    {
		return appendValue (section, key, value);
	    }
	    y2error ("Incorrect index");
	    return section;
	}
	else
	{
	    section[index, "value"] = value;
	    section[index, "changed"] = true;
	    return section;
	}
    }

    /**
      * Get creator type of the section
      * @param section map section description structure
      * @return string section type
      */
    define string getCreationType (list<map> section) ``{
	map intern = (map) lookup_value (section, "__intern__", $[]);
	return intern["type"]:"";
    }


}
