#!/usr/bin/env python
# F. Pierfederici (fpierfed@lsst.org)
import os
from numpy.distutils.core import setup
from numpy.distutils.core import Extension


def _getGccVersion():
    """
    This is a bit of a hack: we do a system call to gcc --version and parse the
    result. It will fail if there is no gcc installed and it is useless if we
    are not using gcc to compile the extension. We need a better way!!!
    """
    import subprocess as sub

    cmd = sub.Popen(args=['gcc', '--version'], stderr=sub.PIPE)
    cmd.wait()
    try:
        versionStr = cmd.stdout.read()
        version = float(versionStr.split('(GCC)', maxsplit=1)[1].split()[0])
    except:
        version = None
    return(version)




MOPS_HOME = os.environ['MOPS_HOME']
cflags=[]

gccVersion = _getGccVersion()
if(gccVersion and gccVersion > 4):
    cflags=['-fno-stack-protector', ]

_ephem = Extension(name = 'ssd._ssd',
                   extra_compile_args=cflags,
                   sources=['ephemwrap.f90', ],
                   include_dirs=['../',
                                 os.path.join(MOPS_HOME, 'include')],
                   library_dirs=['../',
                                 os.path.join(MOPS_HOME, 'lib')],
                   libraries=['ssd'],
                   f2py_options=[])


if __name__ == "__main__": 
    setup(name = 'ssd', 
          description = "JPL SSD module", 
          author = "Francesco Pierfederici", 
          author_email = "fpierfed@lsst.org",
          version='1.0a1',
          package_dir={'ssd': '.'},
          packages=['ssd', ],
          ext_modules = [_ephem, ]
) 
