#!/usr/bin/python3

# Copyright © 2009 James Westby <james.westby@ubuntu.com>,
#             2010, 2011 Stefano Rivera <stefanor@ubuntu.com>
#
# ##################################################################
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# ##################################################################

# pylint: disable=invalid-name
# pylint: enable=invalid-name

import argparse
import logging
import re
import sys
import webbrowser

import debianbts
from launchpadlib.launchpad import Launchpad

from ubuntutools import getLogger
from ubuntutools.config import UDTConfig

Logger = getLogger()


def main():
    bug_re = re.compile(r"bug=(\d+)")

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-b",
        "--browserless",
        action="store_true",
        help="Don't open the bug in the browser at the end",
    )
    parser.add_argument(
        "-l",
        "--lpinstance",
        metavar="INSTANCE",
        help="LP instance to connect to (default: production)",
    )
    parser.add_argument(
        "-v", "--verbose", action="store_true", help="Print info about the bug being imported"
    )
    parser.add_argument(
        "-n",
        "--dry-run",
        action="store_true",
        help="Don't actually open a bug (also sets verbose)",
    )
    parser.add_argument(
        "-p", "--package", help="Launchpad package to file bug against (default: Same as Debian)"
    )
    parser.add_argument(
        "--no-conf", action="store_true", help="Don't read config files or environment variables."
    )
    parser.add_argument("bugs", nargs="+", help="Bug number(s) or URL(s)")
    options = parser.parse_args()

    config = UDTConfig(options.no_conf)
    if options.lpinstance is None:
        options.lpinstance = config.get_value("LPINSTANCE")

    if options.dry_run:
        launchpad = Launchpad.login_anonymously("ubuntu-dev-tools")
        options.verbose = True
    else:
        launchpad = Launchpad.login_with("ubuntu-dev-tools", options.lpinstance)

    if options.verbose:
        Logger.setLevel(logging.DEBUG)

    debian = launchpad.distributions["debian"]
    ubuntu = launchpad.distributions["ubuntu"]
    lp_debbugs = launchpad.bug_trackers.getByName(name="debbugs")

    bug_nums = []

    for bug_num in options.bugs:
        if bug_num.startswith("http"):
            # bug URL
            match = bug_re.search(bug_num)
            if match is None:
                Logger.error("Can't determine bug number from %s", bug_num)
                sys.exit(1)
            bug_num = match.groups()[0]
        bug_num = bug_num.lstrip("#")
        bug_num = int(bug_num)
        bug_nums.append(bug_num)

    bugs = debianbts.get_status(bug_nums)

    if not bugs:
        Logger.error("Cannot find any of the listed bugs")
        sys.exit(1)

    err = False
    for bug in bugs:
        ubupackage = package = bug.source
        if options.package:
            ubupackage = options.package
        bug_num = bug.bug_num
        subject = bug.subject
        log = debianbts.get_bug_log(bug_num)
        summary = log[0]["message"].get_payload()
        target = ubuntu.getSourcePackage(name=ubupackage)
        if target is None:
            Logger.error(
                "Source package '%s' is not in Ubuntu. Please specify "
                "the destination source package with --package",
                ubupackage,
            )
            err = True
            continue

        description = f"Imported from Debian bug http://bugs.debian.org/{bug_num}:\n\n{summary}"
        # LP limits descriptions to 50K chars
        description = (description[:49994] + " [...]") if len(description) > 50000 else description

        Logger.debug("Target: %s", target)
        Logger.debug("Subject: %s", subject)
        Logger.debug("Description: ")
        Logger.debug(description)

        if options.dry_run:
            Logger.info("Dry-Run: not creating Ubuntu bug.")
            continue

        u_bug = launchpad.bugs.createBug(target=target, title=subject, description=description)
        d_sp = debian.getSourcePackage(name=package)
        if d_sp is None and options.package:
            d_sp = debian.getSourcePackage(name=options.package)
        d_task = u_bug.addTask(target=d_sp)
        d_watch = u_bug.addWatch(remote_bug=bug_num, bug_tracker=lp_debbugs)
        d_task.bug_watch = d_watch
        d_task.lp_save()
        Logger.info("Opened %s", u_bug.web_link)
        if not options.browserless:
            webbrowser.open(u_bug.web_link)

    if err:
        sys.exit(1)


if __name__ == "__main__":
    main()
