'Adds new header to the provided message.
WARNING: This function is a bit tricky, and modifying it in the wrong way,
may easily lead to data-loss.
Arguments:
- content: message content, headers and body as a single string
- linebreak: string that carries line ending
- headername: name of the header to add
- headervalue: value of the header to add
.. note::
The following documentation will not get displayed correctly after being
processed by Sphinx. View the source of this method to read it.
This has to deal with strange corner cases where the header is
missing or empty.  Here are illustrations for all the cases,
showing where the header gets inserted and what the end result
is.  In each illustration, \'+\' means the added contents.  Note
that these examples assume LF for linebreak, not CRLF, so \'
denotes a linebreak and \'
\' corresponds to the transition
between header and body.  However if the linebreak parameter
is set to \'
\' then you would have to substitute \'
\' for
\' in the below examples.
* Case 1: No \'
\', leading \'
+X-Flying-Pig-Header: i am here
This is the body
next line
* Case 2: \'
\' at position 0
+X-Flying-Pig-Header: i am here
This is the body
next line
* Case 3: No \'
\', no leading \'
+X-Flying-Pig-Header: i am here
This is the body
next line
* Case 4: \'
\' at non-zero position
Subject: Something wrong with OI
From: some@person.at
X-Flying-Pig-Header: i am here
This is the body
next line'

def addmessageheader(self, content, linebreak, headername, headervalue):

    self.ui.debug('', ('addmessageheader:    called    to    add    %s:    %s' % (headername, headervalue)))
    insertionpoint = content.find((linebreak * 2))
    if (insertionpoint == (-1)):
        self.ui.debug('', 'addmessageheader:    headers    were    missing')
    else:
        self.ui.debug('', ('addmessageheader:    headers    end    at    position    %d' % insertionpoint))
        mark = '==>EOH<=='
        contextstart = max(0, (insertionpoint - 100))
        contextend = min(len(content), (insertionpoint + 100))
        self.ui.debug('', ('addmessageheader:    header/body    transition    "                                                                    "context    (marked    by    %s):    %s%s%s' % (mark, repr(content[contextstart:insertionpoint]), mark, repr(content[insertionpoint:contextend]))))
    prefix = linebreak
    suffix = ''
    if (insertionpoint == 0):
        prefix = ''
        suffix = ''
    elif (insertionpoint == (-1)):
        prefix = ''
        suffix = linebreak
        insertionpoint = 0
        if (content[0:len(linebreak)] != linebreak):
            suffix = (suffix + linebreak)
    self.ui.debug('', ('addmessageheader:    insertionpoint    =    %d' % insertionpoint))
    headers = content[0:insertionpoint]
    self.ui.debug('', ('addmessageheader:    headers    =    %s' % repr(headers)))
    new_header = ((prefix + ('%s:    %s' % (headername, headervalue))) + suffix)
    self.ui.debug('', ('addmessageheader:    new_header    =    %s' % repr(new_header)))
    return ((headers + new_header) + content[insertionpoint:])
