"""Adapted from:
http://wiki.cython.org/PackageHierarchy
"""

import os
import sys
from distutils.core import setup, Extension

import numpy

import cython_gsl

# Check if cython is installed.
try:
    from Cython.Distutils import build_ext
except:
    print "You don't seem to have Cython installed. Please get a"
    print "copy from www.cython.org and install it"
    sys.exit(1)

# Find all pyx files, convert to extension names in dotted notation.
def scandir(dir, files=[]):
    for file in os.listdir(dir):
        path = os.path.join(dir, file)
        if os.path.isfile(path) and path.endswith(".pyx"):
            files.append(path.replace(os.path.sep, ".")[:-4])
        elif os.path.isdir(path):
            scandir(path, files)
    return files

# generate an Extension object from its dotted name
def makeExtension(extName):
    extPath = extName.replace(".", os.path.sep) + ".pyx"
    return Extension(
        extName,
        [extPath],
        libraries=cython_gsl.get_libraries(),
        library_dirs=[cython_gsl.get_library_dir(),
                      './lib/svd']
        )

# get the list of extensions
extNames = scandir("cryoem")

# and build up the set of Extension objects
extensions = [makeExtension(name) for name in extNames]

extensions.append(Extension(
    'cryoem.model.svd',
    sources=['./lib/svd/svd.pyx',
             './lib/svd/svd_adaptor.cpp'],
    language='c++',
    extra_compile_args=['-msse'],
    extra_link_args=['-msse']))


setup(
    name='cryoem',
    cmdclass={'build_ext': build_ext},
    version='1.0',
    packages=['cryoem',
              'cryoem.model',
              'cryoem.data',
              'cryoem.sampling',
              'cryoem.program',
              ],
    ext_modules=extensions,
    scripts=['cryoem/program/gmm-rec.py',
             'cryoem/program/gmm-plot-stack.py',
             'cryoem/program/gmm-plot-stats.py']
)

