#!/usr/bin/env python

# Written by Henrik Nilsen Omma
# (C) Canonical, Ltd. Licensed under the GPL

import sys

try: 
    import connector as Connector
    from commandLine import commandLine
    import utils as utils
except:
    from bugHelper.commandLine import commandLine
    import launchpadbugs.connector as Connector
    import launchpadBugs.utils as utils

def main():
    cl = commandLine()
    
    Bug = Connector.ConnectBug()
    cookie = None
    try:
        cookie = cl.options.cookie
    except:
        try:
            cookie = os.path.expanduser(config.cookie)
        except:
            print >> sys.stderr, "No cookie-file found"
    if cookie:
        Bug.authentication=cookie
    
    if not cl.options.bugnr:
        cl.parser.print_usage()
        sys.exit(1)

    bug= Bug(int(cl.options.bugnr))

    if cl.options.comments:
        comments = bug.comments
        print "Comments:", len(comments)
        print comments
    if cl.options.reporter:
        print bug.reporter
    if cl.options.title:
        print bug.title
    if cl.options.tags:
        print bug.tags
    if cl.options.countcomments:
        comments = bug.comments
        print len(comments)
    if cl.options.comment or cl.options.lastcomment:
        comments = bug.comments
        cnr = -1 if cl.options.lastcomment else cl.options.comment
        comment = comments[int(cnr)]
        if comment:
            if cl.options.author:
                print comment.user
            if cl.options.date:
                print comment.date
            if not cl.options.author and not cl.options.date:
                print comment
        elif cl.options.verbose:
            text = ''
            for c in comments:
                text += c.number + ', '
            print "available comments: %s" % text


if __name__ == "__main__":
    main()

