#!/usr/bin/env python

##################################################################
# Sendmail compatibility wrapper; adapted from qmail's sendmail.c.
##################################################################

"""
Usage:  %(program)s [ -t ] [ -fsender ] [ -Fname ] [ arg ... ]
"""

import getopt
import os
import sys


program = sys.argv[0]
header_recipients = None
execdir = os.path.dirname(os.path.abspath(program))
arglist = ['']

def usage(code, msg=''):
    print __doc__ % globals()
    if msg:
        print msg
    sys.exit(code)
    
try:
    opts, args = getopt.getopt(sys.argv[1:],
                               'hvimte:f:p:o:B:F:EJx', ['help'])
except getopt.error, msg:
    usage(1, msg)

for opt, arg in opts:
    if opt in ('-h', '--help'):
        usage(0)
    elif opt in list('BpvixmeoEJ'):
        pass 
    elif opt == '-t':
        header_recipients = 1
    elif opt == '-f':
        pass
    elif opt == '-F':
        os.environ['NAME'] = arg


def main():
    # If recipients are provided as args, pass them to tmda-inject
    # unless `-t' was specified.
    if args and not header_recipients:
        for a in args:
            arglist.append(a)
    os.execv(os.path.join(execdir,'tmda-inject'), arglist)


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