import re


def add_or_replace_setting(old_buf, variable, new_value, quote_value=True):
    """
    If there is a line setting 'variable' in 'old_buf' (in a "var=value" format),
    change it to set variable to new_value. If there is no such line, add a line
    to the end of buf setting variable to new_value. Return the modified buf.

    If quote_value is True (default), the value is double-quoted first
    """
    if quote_value:
        new_value = '"%s"' % new_value

    new_line = '%s=%s' % (variable, new_value)
    new_buf, count = re.subn(r'(?m)^\s*%s\s*=.*$' % re.escape(variable), new_line, old_buf, 1)
    if count == 0:
        if not new_buf.endswith('\n'):
            new_buf += "\n"
        new_buf += new_line + "\n"
    return new_buf