#!/usr/bin/env python

"""Generate a tagged (dated-style) e-mail address.

Usage:  %(program)s [-c <file>] [-a <address>] [-h] timeout

Where:
	-c <file>
	--config-file <file>
	   Specify a different configuration file other than ~/.tmdarc.
	   
	-a <address>
	--address <address>
	   Specify a different address as the basis for the tagged address.

	--help
	-h
	   Print this help message and exit.

	timeout is an optional timeout interval to override your default.
"""

import getopt
import os
import sys


program = sys.argv[0]

def usage(code, msg=''):
    print __doc__ % globals()
    if msg:
        print msg
    sys.exit(code)

try:
    opts, args = getopt.getopt(sys.argv[1:],
                                   'c:a:h', ['config-file=','address=','help'])
except getopt.error, msg:
    usage(1, msg)

address = None
for opt, arg in opts:
    if opt in ('-h', '--help'):
        usage(0)
    elif opt in ('-c', '--config-file'):
        os.environ['TMDARC'] = arg
    elif opt in ('-a', '--address'):
	address = arg


try:
    import paths
except ImportError:
    pass

from TMDA import Cookie
from TMDA import Defaults


def main():

    global address
    
    try:                                # check for timeout override
        os.environ['TMDA_TIMEOUT'] = args[0]
    except IndexError:
        pass
    if not address:
	address = Defaults.USERNAME + '@' + Defaults.HOSTNAME
    dated_address = Cookie.make_dated_address(address)
    sys.stdout.write(dated_address)


if __name__ == '__main__':
    main()
