#!/usr/bin/python
import pwd, os, time, sys
from twisted.python import usage

email = os.environ.get('TWISTED_EMAIL', os.environ['USER']+'@twistedmatrix.com')
name = os.environ.get('TWISTED_NAME', 
                      pwd.getpwuid(os.getuid())[4].split(',')[0])
date = '-'.join(map(str, time.gmtime()[:3]))

class Options(usage.Options):

    optParameters = [
                     ['message', 'm', ''],
                     ['name', 'n', name],
                     ['email', 'e', email],
                     ['date', 'd', date],
                    ]

    def parseArgs(self, *args):
        self['files'] = ' '.join(args)+': '

    def postOptions(self):
        self['files'] = self.get('files', '')


conf = Options()
conf.parseOptions()

entry = '''\
%(date)s  %(name)s <%(email)s>

	* %(files)s%(message)s

''' % conf

so_far = open("ChangeLog").read()
open('ChangeLog.tmp', 'w').write(entry+so_far)

if not conf['message']:
    os.system('/usr/bin/sensible-editor ChangeLog.tmp')
if not conf['message'] and open('ChangeLog.tmp').read() == entry+so_far:
    print "changelog not modified, not saving"
    os.remove('ChangeLog.tmp')
else:
    os.rename('ChangeLog.tmp', 'ChangeLog')
