# -*- coding: utf-8 -*-
'''
Support for eselect, Gentoo's configuration and management tool.
'''

# Import salt libs
import salt.utils


def __virtual__():
    '''
    Only work on Gentoo systems with eselect installed
    '''
    if __grains__['os'] == 'Gentoo' and salt.utils.which('eselect'):
        return 'eselect'
    return False


def exec_action(module, action, module_parameter=None, action_parameter=None, parameter=None, state_only=False):
    '''
    Execute an arbitrary action on a module.

    CLI Example:

    .. code-block:: bash

        salt '*' eselect.exec_action <module name> [module_parameter] <action> [action_parameter]
    '''
    if parameter:
        salt.utils.warn_until(
            'Lithium',
            'The \'parameter\' option is deprecated and will be removed in the '
            '\'Lithium\' Salt release. Please use either \'module_parameter\' or '
            '\'action_parameter\' instead.'
        )
        action_parameter=parameter
    out = __salt__['cmd.run'](
        'eselect --brief --colour=no {0} {1} {2} {3}'.format(
            module, module_parameter or '', action, action_parameter or ''
        )
    )
    out = out.strip().split('\n')

    if out[0].startswith('!!! Error'):
        return False

    if state_only:
        return True

    if len(out) < 1:
        return False

    if len(out) == 1 and not out[0].strip():
        return False

    return out


def get_modules():
    '''
    List available ``eselect`` modules.

    CLI Example:

    .. code-block:: bash

        salt '*' eselect.get_modules
    '''
    modules = []
    try:
        module_list = exec_action('modules', 'list', action_parameter='--only-names')
    except:
        return None

    for module in module_list:
        if module not in ['help', 'usage', 'version']:
            modules.append(module)
    return modules

def get_target_list(module):
    '''
    List available targets for the given module.

    module
        name of the module to be queried for its targets

    CLI Example:

    .. code-block:: bash

        salt '*' eselect.get_target_list kernel
    '''
    try:
        exec_output = exec_action(module, 'list')
    except:
        return None

    target_list = []
    if isinstance(exec_output, list):
        for item in exec_output:
            target_list.append(item.split(None, 1)[0])
        return target_list

    return None


def get_current_target(module, module_parameter=None, action_parameter=None):
    '''
    Get the currently selected target for the given module.

    module
        name of the module to be queried for its current target

    module_parameter
        additional params passed to the defined module

    action_parameter
        additional params passed to the 'show' action

    CLI Example (current target of system-wide ``java-vm``):

    .. code-block:: bash

        salt '*' eselect.get_current_target java-vm action_parameter='system'

    CLI Example (current target of ``kernel`` symlink):

    .. code-block:: bash

        salt '*' eselect.get_current_target kernel
    '''
    try:
        return exec_action(module, 'show', module_parameter=module_parameter, action_parameter=action_parameter)[0]
    except:
        return None


def set_target(module, target, module_parameter=None, action_parameter=None):
    '''
    Set the target for the given module.
    Target can be specified by index or name.

    CLI Example:

    .. code-block:: bash

        salt '*' eselect.set_target <module name> <target> module_parameter='optional module params' action_parameter='optional action params'
    '''
    if action_parameter:
        action_parameter = '{0} {1}'.format(action_parameter, target)
    else:
        action_parameter = target
    try:
        return exec_action(module, 'set', module_parameter=module_parameter, action_parameter=action_parameter, state_only=True)
    except:
        return False

@@ -3102,36 +3102,38 @@
  try:%0A        re
-turn
+sult =
  exec_action(mod
@@ -3247,24 +3247,92 @@
 eturn None%0A%0A
+    if result == '(unset)':%0A        return None%0A%0A    return result%0A%0A
 %0Adef set_tar

Handle '(unset)' as return value (when no target is set) in get_current_target().