#!/usr/bin/python3

import os
import sys

import codecs
import urllib.request
import json

import textwrap

sys.stdin = codecs.getreader("utf-8")(sys.stdin.detach())
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())

entries = []
def add_entry(entry):
    if entry and 'ignore' not in entry:
        combo = []
        for bug in entry.get('bugs', []):
            combo.append(bug)
        for cve in entry.get('cves', []):
            combo.append(cve)
        combo = sorted(combo)

        if len(combo) == 0:
            if entry.get('title', "").startswith('UBUNTU'):
                combo = '__packaging__'
            else:
                combo = '__mainline__'
        else:
            if combo not in keys:
                keys.append(combo)

        entry['key'] = combo
        entries.append(entry)


# Suck up the git log output and extract the information we need.
keys = []
entry = None
subject_wait = False
for line in sys.stdin:
    if line.startswith('commit '):
        add_entry(entry)
        entry = {}
        subject_wait = True

    elif line.startswith('Author: '):
        bits = line.strip().split(maxsplit=1)
        entry['author'] = bits[1]

    elif subject_wait and line.startswith('    '):
        subject_wait = False
        entry['subject'] = line.strip()

    elif line.startswith('    BugLink: ') and 'launchpad.net' in line:
        bits = line.strip().split(maxsplit=1)
        bits = bits[1].split('/')
        entry.setdefault('bugs', []).append(bits[-1])

    elif line.startswith('    CVE-'):
        entry.setdefault('cves', []).append(line.strip())

    elif line.startswith('    Ignore:'):
        entry['ignore'] = True

add_entry(entry)

entries.reverse()

# Go through the entries and clear out authors for upstream commits.
for entry in entries:
    if entry['subject'].startswith('UBUNTU:'):
        entry['subject'] = entry['subject'][7:].strip()
    else:
        del entry['author']

# Lump everything without a bug at the bottom.
keys.append('__packaging__')
keys.append('__mainline__')

emit_nl = False
for key in keys:
    if key == '__packaging__':
        title_set = [ 'Miscellaneous Ubuntu changes' ]
    elif key == '__mainline__':
        title_set = [ 'Miscellaneous upstream changes' ]
    else:
        title_set = []
        for bug in key:
            if bug.startswith('CVE-'):
                title_set.append(bug)
            else:
                bug_info = None

                try:
                    #urllib.request.urlcleanup()
                    request = urllib.request.Request('https://api.launchpad.net/devel/bugs/' + bug)
                    request.add_header('Cache-Control', 'max-age=0')
                    with urllib.request.urlopen(request) as response:
                        data = response.read()
                        bug_info = json.loads(data.decode('utf-8'))

                    title = bug_info['title']
                    if 'description' in bug_info:
                        for line in bug_info['description'].split('\n'):
                            if line.startswith('Kernel-Description:'):
                                title = line.split(' ', 1)[1]

                except urllib.error.HTTPError:
                    title = 'INVALID or PRIVATE BUG'

                title += ' (LP###' + bug + ')'
                title_set.append(title)


    emit_title = True
    for entry in entries:
        if entry['key'] != key:
            continue

        if emit_title:
            if emit_nl:
                print('')
            emit_nl = True

            title_lines = textwrap.wrap('#// '.join(title_set), 76)
            print('  * ' + title_lines[0].replace('LP###', 'LP: #').replace('#//', ' //'))
            for line in title_lines[1:]:
                line = line.replace('LP###', 'LP: #').replace('#//', ' //')
                print('    ' + line)

            emit_title = False

        title_lines = textwrap.wrap(entry['subject'], 76)
        print('    - ' + title_lines[0])
        for line in title_lines[1:]:
            line = line.replace('LP###', 'LP: #')
            print('      ' + line)
