#!/usr/bin/env python
"""
Attempt to figure out what options need to be added to the Exim
Makefile to embed a Python interpreter for your particular platform.

Most of the info seems to be available in the Python distutils.sysconfig
module, cross your fingers.

  2002-10-19 Barry Pederson <bp@barryp.org>

"""
import distutils.sysconfig, os.path

def patch_makefile(old_makefile, new_makefile):
    makefile = open(old_makefile, 'r').readlines()

    cfg = distutils.sysconfig.get_config_var
    cflags = '-I%s %s' % (cfg('INCLUDEPY'), cfg('CFLAGSFORSHARED'))
    lib = os.path.join(cfg('LIBPL'), cfg('LIBRARY'))
    extralibs =  ' '.join([cfg('LIBM'), cfg('LIBS'), cfg('LDFLAGS'), lib, cfg('LINKFORSHARED')])
    source = 'Local/expy_local_scan.c'

    #
    # Look for existing CFLAGS and EXTRALIBS lines, and append info.  
    # Note if this has been done by setting cflags and extralibs to None
    #
    for i in range(len(makefile)):
        if makefile[i].startswith('CFLAGS=') and cflags:
            makefile[i] = makefile[i].rstrip() + ' ' + cflags + '\n'
            cflags = None
        if makefile[i].startswith('EXTRALIBS=') and extralibs:
            makefile[i] = makefile[i].rstrip() + ' ' + extralibs + '\n'
            extralibs = None
        if makefile[i].startswith('LOCAL_SCAN_SOURCE='):
            makefile[i] = 'LOCAL_SCAN_SOURCE=' + source + '\n'
            source = None

    #
    # Didn't update existing lines? append new ones
    #
    if cflags:
        makefile.append('CFLAGS=%s\n' % cflags)
    if extralibs:
        makefile.append('EXTRALIBS=%s\n' % extralibs)
    if source:
        makefile.append('LOCAL_SCAN_SOURCE=%s\n' % source)

    #
    # Write out updated makefile
    #
    makefile = ''.join(makefile)
    open(new_makefile, 'w').write(makefile)    


if __name__ == '__main__':
    import sys
    if len(sys.argv) < 3:
        print 'Attempt to patch Exim makefile to support Python local_scan'
        print '    Usage: %s <original-makefile> <new-makefile>' % sys.argv[0]
        print ''
        print 'Suggested path for your local_scan module:'
        print '    ', os.path.join(distutils.sysconfig.get_python_lib(), 'exim_local_scan.py')
        sys.exit(1)

    patch_makefile(sys.argv[1], sys.argv[2])

@@ -331,16 +331,20 @@
 sconfig,
+ os,
  os.path
@@ -349,55 +349,157 @@
 th%0A%0A
-def patch_makefile(old_makefile, new_makefile):
+%0ASOURCE_FILE = 'expy_local_scan.c'%0A%0A%0Adef patch_makefile(source_dir, build_dir):%0A    makefile_name = os.path.join(build_dir, 'Local', 'Makefile')%0A    
 %0A   
@@ -519,20 +519,16 @@
 pen(
-old_
 makefile
 , 'r
@@ -523,16 +523,21 @@
 makefile
+_name
 , 'r').r
@@ -828,33 +828,42 @@
 e = 
-'Local/expy_local_scan.c'
+os.path.join('Local', SOURCE_FILE)
 %0A%0A  
@@ -1879,20 +1879,16 @@
 pen(
-new_
 makefile
 , 'w
@@ -1883,16 +1883,21 @@
 makefile
+_name
 , 'w').w
@@ -1916,16 +1916,164 @@
 e)    %0A%0A
+    #%0A    # Symlink in C sourcefile%0A    #%0A    os.symlink(os.path.join(source_dir, SOURCE_FILE), os.path.join(build_dir, 'Local', SOURCE_FILE))%0A    %0A
 %0Aif __na
@@ -2134,9 +2134,9 @@
 ) %3C 
-3
+2
 :%0A  
@@ -2243,40 +2243,17 @@
 %25s %3C
-original-makefile%3E %3Cnew-makefile
+build_dir
 %3E' %25
@@ -2465,36 +2465,81 @@
     
-patch_makefile(sys.argv%5B1%5D, 
+build_dir = sys.argv%5B1%5D%0A    source_dir = os.path.abspath(os.path.dirname(
 sys.
@@ -2547,8 +2547,53 @@
 rgv%5B
-2
+0
 %5D)
+)%0A%0A    patch_makefile(source_dir, build_dir)%0A
 %0A

Change the patch script so that it actually operates the way the documentation claims.