#! /usr/bin/env python
"""approved [flags] args...

Flags:
    -c text...  Comment to use
    -m          Use email+date as the comment
    -r          Reject not approve
    -h          This help
    -v          List emails as processed

Args is a list of email addresses.
"""

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

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

# Parse JCL
comment = 'From CLI';
reply = 'y'
verbose = 0
opts, args = getopt.getopt(sys.argv[1:], "c:hrvm")
for o,a in opts:
    if o == '-c':
        comment = a
    elif o == '-r':
        reply = 'n'
    elif o == '-v':
        verbose = 1
    elif o == '-m':
        comment = datetime.date.today().strftime('Email %Y-%m-%d')
    else:
        print __doc__
        raise SystemExit

q = 'SELECT uid FROM users WHERE email = %s'
update = ("UPDATE users SET date_replied=%s, reply=%s, comment=%s"
     " WHERE uid=%s")
today = datetime.datetime.today().date()

for email in args:
    cursor.execute(q, (email,))
    for uid in cursor:
        cursor.execute(update, (today, reply, comment, uid[0]))
        conn.commit()
        if verbose:
            print email, uid[0]
