def get_or_create_group(groupname, gid_preset, system=False, id_dependent=True):
    """
    Returns the id for the given group, and creates it first in case it does not exist.

    :param groupname: Group name.
    :type groupname: unicode
    :param gid_preset: Group id to set if a new group is created.
    :type gid_preset: int or unicode
    :param system: Create a system group.
    :type system: bool
    :param id_dependent: If the group exists, but its id does not match `gid_preset`, an error is thrown.
    :type id_dependent: bool
    :return: Group id of the existing or new group.
    :rtype: int
    """
    gid = get_group_id(groupname)
    if gid is None:
        create_group(groupname, gid_preset, system)
        return gid_preset
    elif id_dependent and gid != gid_preset:
        error("Present group id '{0}' does not match the required id of the environment '{1}'.".format(gid, gid_preset))
    return gid