#!/usr/bin/env python
#
# Copyright (C) 2001,2002 Jason R. Mastaler <jason@mastaler.com>
#
# This file is part of TMDA.
#
# TMDA is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.  A copy of this license should
# be included in the file COPYING.
#
# TMDA is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with TMDA; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

"""Generate a tagged e-mail address.

Usage:  Usage:  %(program)s OPTIONS

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

       -V
        --version
           Print TMDA version information and exit.

	-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.
           
	-n
        --no-newline
           Do not print a newline after the address.

	-k <keyword>
	--keyword <keyword>
	   Generate a keyword-style tagged address.  keyword is
	   a required keyword string.

	-s <address>
	--sender <address>
	   Generate a sender-style tagged address.  address is
	   a required sender e-mail address.

	-d [timeout]
	--dated [timeout]
	   Generate a dated-style tagged address.  timeout is an
	   optional timeout interval to override your default.
"""

import getopt
import os
import sys

try:
    import paths
except ImportError:
    pass

from TMDA import Version

program = sys.argv[0]
print_newline = 1
tag = None

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

try:
    opts, args = getopt.getopt(sys.argv[1:],
                                   'c:a:dk:s:hVn', ['config-file=',
                                                    'address=',
                                                    'dated',
                                                    'keyword=',
                                                    'sender=',
                                                    'help',
                                                    'version',
                                                    'no-newline'])
except getopt.error, msg:
    usage(1, msg)

address = None

for opt, arg in opts:
    if opt in ('-h', '--help'):
        usage(0)
    if opt == '-V':
        print Version.ALL
        sys.exit()
    if opt == '--version':
        print Version.TMDA
        sys.exit()
    elif opt in ('-c', '--config-file'):
        os.environ['TMDARC'] = arg
    elif opt in ('-a', '--address'):
	address = arg
    elif opt in ('-d', '--dated'):
        tag = 'dated'
        try:                            # check for timeout override
            os.environ['TMDA_TIMEOUT'] = args[0]
        except IndexError:
            pass
    elif opt in ('-k', '--keyword'):
        tag = 'keyword'
        keyword = arg
    elif opt in ('-s', '--sender'):
        tag = 'sender'
        sender = arg
    elif opt in ('-n', '--no-newline'):
        print_newline = 0


from TMDA import Cookie
from TMDA import Defaults


def main():
    global address
    if not address:
	address = Defaults.USERNAME + '@' + Defaults.HOSTNAME
    if tag == 'dated':
        tagged_address = Cookie.make_dated_address(address)
    elif tag == 'keyword':
        tagged_address = Cookie.make_keyword_address(address, keyword)
    elif tag == 'sender':
        tagged_address = Cookie.make_sender_address(address, sender)
    else:
        usage(0)
    sys.stdout.write(tagged_address)
    if print_newline:
        sys.stdout.write("\n")

# This is the end my friend.
if __name__ == '__main__':
    main()
