#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
setup.py file for @PYPACKAGE_NAME@

"""
from numpy.distutils.core import setup, Extension
from numpy.distutils.misc_util import Configuration
import os
import glob

# Full package name
name = '@PYPACKAGE_NAME@'
# List of modules (directories) to be included
packages = ['parmepy',
            'parmepy.domain',
            'parmepy.fields',
            'parmepy.operator',
            'parmepy.operator.monitors',
            'parmepy.problem',
            'parmepy.tools',
            'parmepy.numerics',
            'parmepy.numerics.integrators',
            'parmepy.numerics.remeshing',
            ]

if("@USE_MPI@" is "ON"):
    packages.append('parmepy.mpi')

if("@WITH_GPU@" is "ON"):
    packages.append('parmepy.gpu')

# Enable this to get debug info
DISTUTILS_DEBUG=1

ext_modules =[]

# Check if libparmes was created
enable_fortran = "@WITH_LIB_FORTRAN@"

if(enable_fortran is "ON"):
    inc_dir = '@MPI_Fortran_INCLUDE_PATH@'.split(';')
    while inc_dir.count('') >0:
        inc_dir.remove('') # To avoid -I -I in compiler call. Result in a bug.

    parmes_dir = '@CMAKE_BINARY_DIR@/Modules'
    inc_dir.append(parmes_dir)
    fortran_dir = '@CMAKE_SOURCE_DIR@/parmepy/f2py/'
    parmes_libdir = ['@CMAKE_BINARY_DIR@/src']
    parmeslib = ['@PARMES_LIBRARY_NAME@']
    f2py_options = ['--no-lower']
    fortran_src = []
    withfftw = "@WITH_FFTW@"
    if(withfftw is "ON"):
        fortran_src.append(fortran_dir+'parameters.f90')
        fortran_src.append(fortran_dir+'fftw2py.f90')
        fftwdir = '@FFTWLIB@'
        fftwdir = os.path.split(fftwdir)[0]
        parmeslib.append('fftw3')
        parmeslib.append('fftw3_mpi')
        parmes_libdir.append(fftwdir)

    withscales = '@WITH_SCALES@'
    if(withscales is "ON"):
        if(withfftw is "OFF"):
            fortran_src.append(fortran_dir+'parameters.f90')
        fortran_src.append(fortran_dir+'scales2py.f90')

    options = [('F2PY_REPORT_ON_ARRAY_COPY', '1')]
    if(os.uname()[0] == 'Linux'):
        options.append(('F2PY_REPORT_ATEXIT','1'))

    parpyModule = Extension(name='parmepy.f2py',
                            f2py_options=f2py_options,
                            sources=fortran_src,
                            include_dirs=inc_dir,
                            library_dirs=parmes_libdir,
                            libraries=parmeslib,
                            define_macros=options)
    ext_modules.append(parpyModule)
else:
    packages.append('parmepy.f2py')
    packages.append('parmepy.f2py.scales2py')
    packages.append('parmepy.f2py.fftw2py')

data_files = []

if("@WITH_GPU@" is "ON"):
    data_files.append(('./parmepy/gpu/cl_src',
                       ['@CMAKE_SOURCE_DIR@/parmepy/gpu/cl_src/' + cl_file
                        for cl_file in os.listdir('@CMAKE_SOURCE_DIR@/parmepy/gpu/cl_src/')
                        if cl_file[0]!='.' and cl_file[-3:]=='.cl']))

config = Configuration(name=name,
      version='@PYPACKAGE_VERSION@',
      description='Particular Methods implementation in Python',
      author='Jean-Matthieu Etancelin, Franck Pérignon, Christophe Picard',
      author_email='parmes-devel@lists.forge.imag.fr',
      url='https://forge.imag.fr/projects/parmes/',
      license='GNU public license',
      package_dir={'': '@CMAKE_SOURCE_DIR@'},
      ext_modules=ext_modules,
      packages=packages,
      data_files=data_files,
)

setup(**config.todict())
