#! /usr/bin/python
# -*- coding: utf-8 -*-
# bug_comment.py - <description>
# vim:ts=4:sw=4:et:ft=python:
# $Id:$
# $Source:$
# 2007-03-19

import sys, os, os.path, tempfile, getopt
from launchpadBugs.HTMLOperations import Bug

# Defaults
cookiefile="/home/hjmf/.mozilla/firefox/path/to/cookies.txt"

def submitReport(bugnumber, post_title, post_comment, attach_file=None, attachment_description=None):
    file_contents = open(attach_file).read()
    t=tempfile.TemporaryFile()
    t.write(file_contents)
    t.flush()
    t.seek(0)

    bug = Bug(bugnumber, cookie_file=cookiefile)
    bug.add_comment(post_title, post_comment, attachment=t, description=attachment_description)

if __name__ == "__main__":
    shortopts = "b:t:c:a:d:"
    longopts = ["bugnumber", "title", "comment", "attach", "attachment_description"]

    try:
        opts, args = getopt.getopt(sys.argv[1:], shortopts, longopts)
    except getopt.GetoptError:
        print """Usage: 
            %s -b bugnumber -t "post title" -c "post comment" [ -a "file to attach" -d "attachment description" ]
            %s --bugnumber bugnumber --titlle "post title" [ --attach "file" --attachment_description "attachment description" ]
            """ % (sys.argv[0], sys.argv[0])
        sys.exit(1)

    for o, v in opts:
        if o in ("-b", "--bugnumber"):
            BUGNUMBER = v
        elif o in ("-t", "--title"):
            POSTTITLE = v
        elif o in ("-c", "--comment"):
            POSTCOMMENT = v
        elif o in ("-a", "--attach"):
            ATTACHMENT = v # handle errors (TODO)
        elif o in ("-d", "--attachment_description"):
            ATTACHMENTDESC = v 

    submitReport(BUGNUMBER, POSTTITLE, POSTCOMMENT, ATTACHMENT, ATTACHMENTDESC)
