# -*- coding: utf-8 -*-
'''
Manage groups on Linux and OpenBSD
'''

# Import python libs
try:
    import grp
except ImportError:
    pass

from salt.modules import state_std

# Define the module's virtual name
__virtualname__ = 'group'


def __virtual__():
    '''
    Set the user module if the kernel is Linux or OpenBSD
    '''
    if __grains__['kernel'] in ('Linux', 'OpenBSD', 'NetBSD'):
        return __virtualname__
    return False


def add(name, gid=None, system=False, **kwargs):
    '''
    Add the specified group

    CLI Example:

    .. code-block:: bash

        salt '*' group.add foo 3456
    '''
    cmd = 'groupadd '
    if gid:
        cmd += '-g {0} '.format(gid)
    if system:
        cmd += '-r '
    cmd += name

    ret = __salt__['cmd.run_stdall'](cmd)
    state_std(kwargs, ret)

    return not ret['retcode']


def delete(name, **kwargs):
    '''
    Remove the named group

    CLI Example:

    .. code-block:: bash

        salt '*' group.delete foo
    '''
    ret = __salt__['cmd.run_stdall']('groupdel {0}'.format(name))
    state_std(kwargs, ret)

    return not ret['retcode']


def info(name):
    '''
    Return information about a group

    CLI Example:

    .. code-block:: bash

        salt '*' group.info foo
    '''
    try:
        grinfo = grp.getgrnam(name)
    except KeyError:
        return {}
    else:
        return _format_info(grinfo)


def _format_info(data):
    '''
    Return formatted information in a pretty way.
    '''
    return {'name': data.gr_name,
            'passwd': data.gr_passwd,
            'gid': data.gr_gid,
            'members': data.gr_mem}


def getent(refresh=False):
    '''
    Return info on all groups

    CLI Example:

    .. code-block:: bash

        salt '*' group.getent
    '''
    if 'group.getent' in __context__ and not refresh:
        return __context__['group.getent']

    ret = []
    for grinfo in grp.getgrall():
        ret.append(_format_info(grinfo))
    __context__['group.getent'] = ret
    return ret


def chgid(name, gid, **kwargs):
    '''
    Change the gid for a named group

    CLI Example:

    .. code-block:: bash

        salt '*' group.chgid foo 4376
    '''
    pre_gid = __salt__['file.group_to_gid'](name)
    if gid == pre_gid:
        return True
    cmd = 'groupmod -g {0} {1}'.format(gid, name)
    result = __salt__['cmd.run_stdall'](cmd)
    state_std(kwargs, result)
    post_gid = __salt__['file.group_to_gid'](name)
    if post_gid != pre_gid:
        return str(post_gid) == gid
    return False

@@ -2496,20 +2496,16 @@
 urn 
-str(
 post_gid
 ) ==
@@ -2500,17 +2500,16 @@
 post_gid
-)
  == gid%0A

reset groupadd gid type bug