#! /usr/bin/env python
"""get-followups [flags]

Flags:
    -d      List details (commits) per user

Get list of users (in CSV format) who have not been reached.
"""

import mysql.connector
import datetime, os, re, subprocess, sys, string, random
import getopt

dbconfig = {
        'user': 'licensereader',
        'password': open('ropass.txt').read().strip(),
        'database': 'license'
        }
conn = mysql.connector.connect(**dbconfig)
cursor = conn.cursor()

urlbase = 'https://license.openssl.org/cgi-bin/lookup.py?uid='
fmtstring = urlbase + '%d, %s, %d, "%s"'

details = 0
opts, args = getopt.getopt(sys.argv[1:], "dh")
for o,a in opts:
    if o == '-d':
        details = 1
    else:
        print __doc__
        raise SystemExit

rows = []
q = ('SELECT users.uid,email,reply,name,count(log.uid) FROM users'
        ' LEFT JOIN log ON log.uid = users.uid'
        ' WHERE reply = "-" GROUP BY email ORDER BY count(log.uid), email' )
cursor.execute(q)
for row in cursor:
    uid,email,reply,name,count = row
    rows.append((uid, email, reply, name, count))

q = ('SELECT commit,date,descrip FROM commits'
        ' LEFT JOIN log ON log.cid=commits.cid WHERE log.uid=%s')
for row in rows:
    uid,email,reply,name,count = row
    print fmtstring % (uid, email, count, name)
    if details and count > 0:
        cursor.execute(q, (uid,))
        for c in cursor:
            commit,date,descrip = c
            print "\t", commit[0:8],date,descrip
        print
