'Ensure that the cover information in the guide is correct. That
means, at most one entry with type="cover" that points to a raster
cover and at most one entry with type="titlepage" that points to an
HTML titlepage.'

def rationalize_cover2(self, opf, log):

    from calibre.ebooks.oeb.base import OPF
    removed = None
    from lxml import etree
    (guide_cover, guide_elem) = (None, None)
    for guide_elem in opf.iterguide():
        if (guide_elem.get('type', '').lower() == 'cover'):
            guide_cover = guide_elem.get('href', '').partition('#')[0]
            break
    if (not guide_cover):
        raster_cover = opf.raster_cover
        if raster_cover:
            if (guide_elem is None):
                g = opf.root.makeelement(OPF('guide'))
                opf.root.append(g)
            else:
                g = guide_elem.getparent()
            guide_cover = raster_cover
            guide_elem = g.makeelement(OPF('reference'), attrib={'href': raster_cover, 'type': 'cover'})
            g.append(guide_elem)
        return
    spine = list(opf.iterspine())
    if (not spine):
        return
    idref = spine[0].get('idref', '')
    manifest = list(opf.itermanifest())
    if (not manifest):
        return
    elem = [x for x in manifest if (x.get('id', '') == idref)]
    if ((not elem) or (elem[0].get('href', None) != guide_cover)):
        return
    log('Found    HTML    cover', guide_cover)
    if (not self.for_viewer):
        if (len(spine) == 1):
            log.warn('There    is    only    a    single    spine    item    and    it    is    marked    as    the    cover.    Removing    cover    marking.')
            for guide_elem in tuple(opf.iterguide()):
                if (guide_elem.get('type', '').lower() == 'cover'):
                    guide_elem.getparent().remove(guide_elem)
            return
        else:
            spine[0].getparent().remove(spine[0])
            removed = guide_cover
    else:
        spine[0].attrib.pop('linear', None)
        opf.spine[0].is_linear = True
    raster_cover = opf.raster_cover
    if (raster_cover is not None):
        guide_elem.set('href', raster_cover)
    else:
        from calibre.ebooks import render_html_svg_workaround
        guide_elem.set('href', 'calibre_raster_cover.jpg')
        t = etree.SubElement(elem[0].getparent(), OPF('item'), href=guide_elem.get('href'), id='calibre_raster_cover')
        t.set('media-type', 'image/jpeg')
        if os.path.exists(guide_cover):
            renderer = render_html_svg_workaround(guide_cover, log)
            if (renderer is not None):
                open('calibre_raster_cover.jpg', 'wb').write(renderer)
    self.set_guide_type(opf, 'titlepage', guide_cover, 'Title    Page')
    return removed
