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

#sourcepackagedata = sys.argv[1] + '/' + sys.argv[1] + '.data' 
main_r_data = '''main-restricted/main-restricted.data'''
universe_data = '''universe/universe.data'''
multiverse_data = '''multiverse/multiverse.data'''

print '''
set xdata time
set timefmt "%s"
set xtics rotate by 90
set xlabel "Date/Time" 0,-1
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' % main_r_data ).read().splitlines().pop().split(",")[0])

column = int(open('%s' % main_r_data).read().splitlines().pop().split(",")[1:])
# max only works on ints but int won't work on a list
largest = max(column)

print now
print largest

for day in days.keys():
    mrfile  = main_r_data
    unifile = universe_data
    multifile = multiverse_data
    xmin = now - days[day]['span']
    dstep  = days[day]['step']

    for stat in statinfo.keys():
        title  = statinfo[stat]['title']
        column = statinfo[stat]['column']
        # make a dictionary of all the values in column, sort it and find the max?
#        print '''set yrange [0:%s]''' % (
#        print '''set format x "%s"''' % (days[day]['format'])
#        print '''set output "combined-%s-%s.svg"''' \
#              % (day, stat)
#        print '''plot ["%d":] "%s" using 1:%d every %d title "%s" with linespoints linecolor 'red' pointtype 7 pointsize 0.6, "< tail -n 1 %s" using 1:%d notitle with points pointtype 6''' \
#              % (xmin, mrfile, column, dstep, title, mrfile, column)
#        print '''plot ["%d":] "%s" using 1:%d every %d title "%s" with linespoints linecolor 'blue' pointtype 7 pointsize 0.6, "< tail -n 1 %s" using 1:%d notitle with points pointtype 6''' \
#              % (xmin, unifile, column, dstep, title, unifile, column)
#        print '''plot ["%d":] "%s" using 1:%d every %d title "%s" with linespoints linecolor 'yellow' pointtype 7 pointsize 0.6, "< tail -n 1 %s" using 1:%d notitle with points pointtype 6''' \
#              % (xmin, multifile, column, dstep, title, multifile, column)
#        print
#
#    Open bugs
#    print '''set format x "%s"''' % (days[day]['format'])
#    print '''set output "combined-%s-%s.svg"''' \
#        % (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, mrfile, dstep, 'Open', mrfile)
#    print
