def make_command(
    command,
    env=None,
    su_user=None,
    sudo=False,
    sudo_user=None,
    preserve_sudo_env=False,
):
    '''
    Builds a shell command with various kwargs.
    '''
    debug_meta = {}
    for key, value in (
        ('sudo', sudo),
        ('sudo_user', sudo_user),
        ('su_user', su_user),
        ('env', env),
    ):
        if value:
            debug_meta[key] = value
    logger.debug('Building command ({0}): {1}'.format(' '.join(
        '{0}: {1}'.format(key, value)
        for key, value in six.iteritems(debug_meta)
    ), command))
    if env:
        env_string = ' '.join([
            '{0}={1}'.format(key, value)
            for key, value in six.iteritems(env)
        ])
        command = 'export {0}; {1}'.format(env_string, command)
    command = shlex_quote(command)
    if su_user:
        command = 'su {0} -c {1}'.format(su_user, command)
    else:
        command = 'sh -c {0}'.format(command)
    if sudo:
        sudo_bits = ['sudo', '-H']
        if preserve_sudo_env:
            sudo_bits.append('-E')
        if sudo_user:
            sudo_bits.extend(('-u', sudo_user))
        command = '{0} {1}'.format(' '.join(sudo_bits), command)
    return command