#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright © 2008 Canonical Ltd.
# Author: Scott James Remnant <scott at ubuntu.com>.
# Hacked up by: Bryce Harrington <bryce at ubuntu.com>
# Change merge_changelog to merge-changelog: Ryan Kavanagh
#                                            <ryanakca@kubuntu.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of version 3 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import re
import sys

from debian.debian_support import Version


def usage(exit_code=1):
    print('''Usage: merge-changelog <left changelog> <right changelog>

merge-changelog takes two changelogs that once shared a common source,
merges them back together, and prints the merged result to stdout.  This
is useful if you need to manually merge a ubuntu package with a new
Debian release of the package.
''')
    sys.exit(exit_code)

########################################################################
# Changelog Management
########################################################################


# Regular expression for top of debian/changelog
CL_RE = re.compile(r'^(\w[-+0-9a-z.]*) \(([^\(\) \t]+)\)((\s+[-0-9a-z]+)+)\;',
                   re.IGNORECASE)


def merge_changelog(left_changelog, right_changelog):
    """Merge a changelog file."""

    left_cl = read_changelog(left_changelog)
    right_cl = read_changelog(right_changelog)

    for right_ver, right_text in right_cl:
        while len(left_cl) and left_cl[0][0] > right_ver:
            (left_ver, left_text) = left_cl.pop(0)
            print(left_text)

        while len(left_cl) and left_cl[0][0] == right_ver:
            (left_ver, left_text) = left_cl.pop(0)

        print(right_text)

    for _, left_text in left_cl:
        print(left_text)

    return False


def read_changelog(filename):
    """Return a parsed changelog file."""
    entries = []

    changelog_file = open(filename)
    try:
        (ver, text) = (None, "")
        for line in changelog_file:
            match = CL_RE.search(line)
            if match:
                try:
                    ver = Version(match.group(2))
                except ValueError:
                    ver = None

                text += line
            elif line.startswith(" -- "):
                if ver is None:
                    ver = Version("0")

                text += line
                entries.append((ver, text))
                (ver, text) = (None, "")
            elif len(line.strip()) or ver is not None:
                text += line
    finally:
        changelog_file.close()

    if len(text):
        entries.append((ver, text))

    return entries


def main():
    if len(sys.argv) > 1 and sys.argv[1] in ('-h', '--help'):
        usage(0)
    if len(sys.argv) != 3:
        usage(1)

    left_changelog = sys.argv[1]
    right_changelog = sys.argv[2]

    merge_changelog(left_changelog, right_changelog)
    sys.exit(0)


if __name__ == '__main__':
    main()
