.. versionchanged:: 2017.7.0

    Add a job to queue.

    job : string
        Command to run.

    timespec : string
        The 'timespec' follows the format documented in the at(1) manpage.

    tag : string
        Make a tag for the job.

    user : string
        The user to run the at job
        .. versionadded:: 2014.1.4

    unique_tag : boolean
        If set to True job will not be added if a job with the tag exists.
        .. versionadded:: 2017.7.0

    .. code-block:: yaml

        rose:
          at.present:
            - job: 'echo "I love saltstack" > love'
            - timespec: '9:09 11/09/13'
            - tag: love
            - user: jam

def present(name, timespec, tag=None, user=None, job=None, unique_tag=False):
    '''
    .. versionchanged:: 2017.7.0

    Add a job to queue.

    job : string
        Command to run.

    timespec : string
        The 'timespec' follows the format documented in the at(1) manpage.

    tag : string
        Make a tag for the job.

    user : string
        The user to run the at job
        .. versionadded:: 2014.1.4

    unique_tag : boolean
        If set to True job will not be added if a job with the tag exists.
        .. versionadded:: 2017.7.0

    .. code-block:: yaml

        rose:
          at.present:
            - job: 'echo "I love saltstack" > love'
            - timespec: '9:09 11/09/13'
            - tag: love
            - user: jam

    '''
    ret = {'name': name,
           'changes': {},
           'result': True,
           'comment': ''}

    # if job is missing, use name
    if not job:
        job = name

    # quick return on test=True
    if __opts__['test']:
        ret['result'] = None
        ret['comment'] = 'job {0} added and will run on {1}'.format(
            job,
            timespec,
        )
        return ret

    # quick return if unique_tag and job exists
    if unique_tag:
        if not tag:
            ret['result'] = False
            ret['comment'] = 'no tag provided and unique_tag is set to True'
            return ret
        elif __salt__['at.jobcheck'](tag=tag)['jobs']:
            ret['comment'] = 'atleast one job with tag {tag} exists.'.format(
                tag=tag
            )
            return ret

    # create job
    if user:
        luser = __salt__['user.info'](user)
        if not luser:
            ret['result'] = False
            ret['comment'] = 'user {0} does not exists'.format(user)
            return ret
        ret['comment'] = 'job {0} added and will run as {1} on {2}'.format(
            job,
            user,
            timespec,
        )
        res = __salt__['at.at'](
            timespec,
            job,
            tag=tag,
            runas=user,
        )
    else:
        ret['comment'] = 'job {0} added and will run on {1}'.format(
            job,
            timespec,
        )
        res = __salt__['at.at'](
            timespec,
            job,
            tag=tag,
        )

    # set ret['changes']
    if res.get('jobs'):
        ret['changes'] = res['jobs'][0]
    if 'error' in res:
        ret['result'] = False
        ret['comment'] = res['error']

    return ret