from distutils.core import setup, Extension
from distutils.command.install import install as _install
from distutils.command.build import build as _build
import sys
import os

possible_include_dirs = ["/usr/local/include", "/usr/include"]

try:
  python_library_name = "python%d.%d" % (sys.version_info.major, sys.version_info.minor)
except:
  python_library_name = "python%d.%d" % (sys.version_info[0], sys.version_info[1])

#
# TODO: Figure out why Pypi is chaning the perms of the files when unpacking...  Is the
# umask set to 022 or something?
#
def _pre_build(dir):
  ret = os.system('(cd c_eclib-0.2 && chmod 755 ./configure && chmod 755 ./install-sh && ./configure && make install)')
  if ret != 0:
    sys.exit(2)

class build(_build):
    def run(self):
        self.execute(_pre_build, (self.build_lib,),
                     msg="Running pre build task(s)")
        _build.run(self)


module = Extension('pyeclib_c',
                   define_macros = [('MAJOR VERSION', '0'),
                                    ('MINOR VERSION', '1')],
                   include_dirs = ['/usr/include/%s' % python_library_name,
                                   'src/c/pyeclib_c',
                                   'c_eclib-0.2/include',
                                   '/usr/local/include'],
                   library_dirs = ['/usr/lib', '/usr/local/lib'],
                   libraries = [python_library_name, 'Jerasure', 'Xorcode'],
                   # The extra arguments are for debugging
                   #extra_compile_args = ['-g', '-O0']
                   extra_link_args=['-Bstatic'],
                   sources = ['src/c/pyeclib_c/pyeclib_c.c'])

setup (name = 'PyECLib',
       version = '0.1.14',
       author = 'Kevin Greenan',
       author_email = 'kmgreen2@gmail.com',
       maintainer = 'Kevin Greenan and Tushar Gohad',
       maintainer_email = 'kmgreen2@gmail.com',
       url = 'https://bitbucket.org/kmgreen2/pyeclib',
       description = 'This library provides a simple Python interface for \
                      implementing erasure codes.  To obtain the best possible performance, the \
                      underlying erasure code algorithms are written in C.',
       platforms = 'Linux',
       license = 'BSD',
       ext_modules = [module],
       packages=['pyeclib'],
       package_dir={'pyeclib': 'src/python/pyeclib'},
       cmdclass={'build': build},
       py_modules = ['pyeclib.ec_iface', 'pyeclib.core'])


@@ -167,16 +167,32 @@
 mport os
+%0Aimport platform
 %0A%0Apossib
@@ -249,16 +249,349 @@
 lude%22%5D%0A%0A
+autoconf_arguments=%22%22%0A%0A#%0A# Fuck Apple and their universal binaries!%0A# I am not supporting powerpc, so ignoring %0A# it%0A#%0Aplatform_str = platform.platform()%0Aif platform_str.find(%22Darwin%22) %3E -1:%0A  if platform_str.find(%22x86_64%22) %3E -1 and platform_str.find(%22i386%22): %0A    autoconf_arguments='CC=%22gcc -arch i386 -arch x86_64%22 CPP=%22gcc -E%22'%0A%0A
 try:%0A  p
@@ -1015,16 +1015,19 @@
 nfigure 
+%25s 
 && make 
@@ -1035,16 +1035,37 @@
 nstall)'
+ %25 autoconf_arguments
 )%0A  if r
@@ -1764,29 +1764,8 @@
  = %5B
-python_library_name, 
 'Jer
@@ -1902,57 +1902,8 @@
 0'%5D%0A
-                   extra_link_args=%5B'-Bstatic'%5D,%0A
     

Support Apple's universal binaries...