#!/usr/bin/python
# Author: Jamie Strandboge <jamie@canonical.com>
# Copyright (C) 2009 Canonical, Ltd.
# License: GPLv3

import lpl_common
import optparse
import sys

lp = lpl_common.connect()

parser = optparse.OptionParser()
parser.add_option("-n", "--not-security", help="Unmark as security bug", action="store_true")
parser.add_option("-P", "--is-private", help="Mark as private", action="store_true")
parser.add_option("-w", "--comment", help="Comment to add to bug", metavar="COMMENT")
parser.add_option("-s", "--status", help="Set status", metavar="STATUS")

(opt, args) = parser.parse_args()

def subscribed(bug, person):
    '''This looks for a person in the bug subscription list.
       Sometimes, the API is ugly'''
    for subscriber in bug.subscriptions:
        if str(subscriber.person) == str(person):
            return True
    return False

subject = ''
comment = ''
if opt.comment:
    subject = ''
    comment = opt.comment

valid_status = ['New', 'Incomplete', 'Invalid', "Won't Fix", 'Confirmed', 'Triaged', 'In Progress', 'Fix Committed', 'Fix Release', 'Unknown']
if opt.status:
    if opt.status not in valid_status:
        print >> sys.stderr ("Invalid status '%s'. Aborting") % (opt.status)
        sys.exit(1)

for num in args:
    unsub_self = False
    bug = lp.bugs[num]
    print "%s..." % (num)

    # Set privacy
    if opt.is_private and not bug.private:
        print "\tmarking private..."
        bug.private = True
        if not lpl_common.save(bug):
            print >>sys.stderr, "aborting update of %s" % (num)
            continue

    # reset flags if not security
    if opt.not_security:
        do_save = False
        if not opt.is_private and bug.private:
            print "\tmarking public..."
            bug.private = False
            do_save = True

        if bug.security_related:
            print "\tmarking non-security..."
            bug.security_related = False
            do_save = True

        if do_save and not lpl_common.save(bug):
            print >>sys.stderr, "aborting update of %s" % (num)
            continue

        # reset subscribers if not private
        if not opt.is_private:
            print "\tsubscribing ubuntu-bugs..."
            bug.subscribe(person=lp.people["ubuntu-bugs"])

        # hack to get around unsubscribing a team that is giving you perms
        # to see a private bug -- you must be subscribed yourself first.
        if bug.private and not subscribed(bug, lp.me):
            print "\ttemporarily subscribing self..."
            bug.subscribe(person=lp.me)
            unsub_self = True

    # Set status and comment before potentially unsubscribing...
    if opt.status:
        print "\tmarking %s..." % (opt.status)
        for task in bug.bug_tasks:
            task.transitionToStatus(status=opt.status)

    # optionally add comment
    if comment != '':
        print "\tadding comment..."
        bug.newMessage(content=comment, subject=subject)

    if opt.not_security:
        print "\tunsubscribing ubuntu-security..."
        bug.unsubscribe(person=lp.people["ubuntu-security"])
        if unsub_self:
            print "\tunsubscribing self..."
            try:
                bug.unsubscribe(person=lp.me)
            except:
                # This should die since we're not allowed to see it anymore
                pass

