Install new alternative for defined <name>

    name
        is the master name for this link group
        (e.g. pager)

    link
        is the symlink pointing to /etc/alternatives/<name>.
        (e.g. /usr/bin/pager)

    path
        is the location of the new alternative target.
        NB: This file / directory must already exist.
        (e.g. /usr/bin/less)

    priority
        is an integer; options with higher numbers have higher priority in
        automatic mode.

def install(name, link, path, priority):
    '''
    Install new alternative for defined <name>

    name
        is the master name for this link group
        (e.g. pager)

    link
        is the symlink pointing to /etc/alternatives/<name>.
        (e.g. /usr/bin/pager)

    path
        is the location of the new alternative target.
        NB: This file / directory must already exist.
        (e.g. /usr/bin/less)

    priority
        is an integer; options with higher numbers have higher priority in
        automatic mode.
    '''
    ret = {'name': name,
           'link': link,
           'path': path,
           'priority': priority,
           'result': True,
           'changes': {},
           'comment': ''}

    if __salt__['alternatives.check_exists'](name, path):
        ret['comment'] = 'Alternative {0} for {1} is already registered'.format(path, name)
    else:
        if __opts__['test']:
            ret['comment'] = (
                'Alternative will be set for {0} to {1} with priority {2}'
            ).format(name, path, priority)
            ret['result'] = None
            return ret

        out = __salt__['alternatives.install'](name, link, path, priority)
        if __salt__['alternatives.check_exists'](name, path):
            if __salt__['alternatives.check_installed'](name, path):
                ret['comment'] = (
                    'Alternative for {0} set to path {1} with priority {2}'
                ).format(name, path, priority)
            else:
                ret['comment'] = (
                    'Alternative {0} for {1} registered with priority {2} and not set to default'
                ).format(path, name, priority)
            ret['changes'] = {'name': name,
                              'link': link,
                              'path': path,
                              'priority': priority}
        else:
            ret['result'] = False
            ret['comment'] = (
                'Alternative for {0} not installed: {1}'
            ).format(name, out)

    return ret