#!/usr/bin/python
import sys

#data = ['%d' % int(time.time())]

package = sys.argv[1]

status_url = 'https://bugs.launchpad.net/ubuntu/+source/%s/+bugs?field.searchtext=&orderby=-importance&assignee_option=any&field.assignee=&field.bug_reporter=&field.bug_contact=&field.bug_commenter=&field.subscriber=&field.component-empty-marker=1&field.status_upstream-empty-marker=1&field.omit_dupes.used=&field.omit_dupes=on&field.has_patch.used=&field.tag=&field.has_cve.used=&field.has_no_package.used=&search=Search' % (package)

status_relationship =  { 'new':'New', 'incomplete':'INCOMPLETE_WITH_RESPONSE&field.status:list=INCOMPLETE_WITHOUT_RESPONSE', 'confirmed':'Confirmed', 'triaged':'Triaged', 'inprogress':'In+Progress', 'fixcommitted':'Fix+Committed', 'open':'New&field.status:list=INCOMPLETE_WITH_RESPONSE&field.status:list=INCOMPLETE_WITHOUT_RESPONSE&field.status:list=Confirmed&field.status:list=Triaged&field.status:list=In+Progress&field.status:list=Fix+Committed', 'fixreleased':'Fix+Released', 'invalid':'Invalid', 'wontfix':'''Won't+Fix''', 'total':'''New&field.status:list=INCOMPLETE_WITH_RESPONSE&field.status:list=INCOMPLETE_WITHOUT_RESPONSE&field.status:list=Confirmed&field.status:list=Triaged&field.status:list=In+Progress&field.status:list=Fix+Committed&field.status:list=Fix+Released&field.status:list=Invalid&field.status:list=Won't+Fix''' }

sort_status_relationship = [ 'new', 'incomplete', 'confirmed', 'triaged', 'inprogress', 'fixcommitted', 'open', 'fixreleased', 'invalid', 'wontfix', 'total' ]

importance_url = 'https://bugs.launchpad.net/ubuntu/+source/%s/+bugs?field.searchtext=&orderby=-importance&assignee_option=any&field.assignee=&field.bug_reporter=&field.bug_contact=&field.bug_commenter=&field.subscriber=&field.component-empty-marker=1&field.status_upstream-empty-marker=1&field.omit_dupes.used=&field.omit_dupes=on&field.has_patch.used=&field.tag=&field.has_cve.used=&field.has_no_package.used=&search=Search' % (package)

importances = [ 'Undecided', 'Wishlist', 'Low', 'Medium', 'High', 'Critical' ]

period_nice =  { '1day':'a day', 'week':'week', 'month':'month', 'halfyear':'half a year', 'fullyear':'year' }

periods = [ '1day', 'week', 'month', 'halfyear', 'fullyear' ]

header = '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n<html lang="en">\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">\n<title>Graphs for %s</title>\n</head>\n<body>\n<h1>Graphs of bugs in %s</h1>''' % (package, package)

footer = '''</body></html>'''

print '''%s''' % (header)
for period in periods:
    print '''<h2>Status for %s</h2>''' % ( period_nice[period] )
    for status in sort_status_relationship:
        print '''<p><a title="%s in %s" href="%s&field.status:list=%s"><img src=plots/%s-%s-%s.png></a></p>''' % ( status, package.lower(), status_url, status_relationship[status], package.lower(), period, status ) 
    print '''<h2>Importance for %s</h2>''' % ( period_nice[period] )
    for importance in importances:
        print '''<p><a title="%s in %s" href="%s&field.importance:list=%s"><img src=plots/%s-%s-%s.png></a></p>''' % ( importance, package.lower(), importance_url, importance, package.lower(), period, importance.lower() ) 
print '''%s''' % (footer)

#print ', '.join(data)
