import textwrap


def wrap_headlines(dbhead, width=75):
    """
    wraps lines of a restraint header to prevent too long lines in
    SHELXL. wrapping is done with = at the end of a line and ' ' at
    start of the next line
    :param dbhead: header with restraints
    :param width: wrap after width characters
    >>> line = ['foo bar this is text to wrap. blah bub']
    >>> wrap_headlines(line, 15)
    ['foo bar this is =\\n   text to wrap. =\\n   blah bub\\n']
    >>> wrap_headlines(['SADI C1 C2 C3 C4'], 10)
    ['SADI C1 C2 =\\n   C3 C4\\n']
    """
    for num, line in enumerate(dbhead):
        line = textwrap.wrap(line, width, subsequent_indent='  ')
        if len(line) > 1:
            newline = []
            for n, l in enumerate(line):
                if n < len(line) - 1:
                    l += ' =\n'
                newline.append(l)
            dbhead[num] = ' '.join(newline)
    for num, line in enumerate(dbhead):
        line = ' '.join(line.strip().split(' '))
        dbhead[num] = line + '\n'
    return dbhead