#!/usr/bin/env python
# Copyright 2007 Kees Cook <kees@ubuntu.com>
# License: GPLv2
import sys

sourcepackage = sys.argv[1]
sourcepackagedata = sys.argv[1] + '/' + sys.argv[1] + '.data' 

print '''
set xdata time
set timefmt "%s"
set xtics rotate by 90
set xlabel "Date/Time" 0,-1
set yrange [0:]
set ylabel "Number of Bugs"
set offsets 0, 0, 1, 1

set term svg
set grid
set key top left
'''

statinfo = {
    'total': { 'title':'Total', 'column':2 },
    'new': { 'title':'New', 'column':3 },
    'incomplete': { 'title':'Incomplete', 'column':4 },
    'confirmed': { 'title':'Confirmed', 'column':5 },
    'triaged': { 'title':'Triaged', 'column':6 },
    'inprogress': { 'title':'In Progress', 'column':7 },
    'fixcommitted': { 'title':'Fix Committed', 'column':8 },
    'fixreleased': { 'title':'Fix Released', 'column':9 },
    'invalid': { 'title':'Invalid', 'column':10 },
    'wontfix': { 'title':'''Won't Fix''', 'column':11 },
    'undecided': { 'title':'Undecided', 'column':12 },
    'wishlist': { 'title':'Wishlist', 'column':13 },
    'low': { 'title':'Low', 'column':14 },
    'medium': { 'title':'Medium', 'column':15 },
    'high': { 'title':'High', 'column':16 },
    'critical': { 'title':'Critical', 'column':17 },
    }

days = {
    'fullyear': { 'step': 350, 'span': 3600*24*365, 'format': '%b %Y' },
    'halfyear': { 'step': 350, 'span': 3600*24*180, 'format': '%b %Y' },
    'month': { 'step': 24, 'span': 3600*24*30, 'format': '%d %b' },
    'week': { 'step': 6, 'span': 3600*24*7, 'format': '%d %b' },
    '1day': { 'step': 1, 'span': 3600*24, 'format': '%m/%d %H:%M' },
    }

now = int(open('%s' % sourcepackagedata ).read().splitlines().pop().split(",")[0])

for day in days.keys():
    dfile  = sourcepackagedata
    xmin = now - days[day]['span']
    dstep  = days[day]['step']

    for stat in statinfo.keys():
        title  = statinfo[stat]['title']
        column = statinfo[stat]['column']
        print '''set format x "%s"''' % (days[day]['format'])
        print '''set output "%s/plots/%s-%s-%s.svg"''' \
              % (sourcepackage, sourcepackage, day, stat)
        print '''plot ["%d":] "%s" using 1:%d every %d title "%s" with linespoints pointtype 7 pointsize 0.6, "< tail -n 1 %s" using 1:%d notitle with points pointtype 6''' \
              % (xmin, dfile, column, dstep, title, dfile, column)
        print

    # Open bugs
    print '''set format x "%s"''' % (days[day]['format'])
    print '''set output "%s/plots/%s-%s-%s.svg"''' \
        % (sourcepackage, sourcepackage, day, 'open')
    print '''plot ["%d":] "%s" using 1:($2-$9-$10-$11) every %d title "%s" with linespoints pointtype 7 pointsize 0.6, "< tail -n 1 %s" using 1:($2-$9-$10-$11) notitle with points pointtype 6''' \
              % (xmin, dfile, dstep, 'Open', dfile)
    print

