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

"""
from numpy.distutils.core import setup, Extension
import os
import glob
import sys
# List of all directories for sources
#os.listdir(os.path.join('ParMePy','New_ParmePy'))

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

# Enable this to get debug info
# DISTUTILS_DEBUG=1
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)
parmes_libdir = ['@CMAKE_BINARY_DIR@/src']
parmeslib = ['@PARMES_LIBRARY_NAME@']
f2py_options = ['--no-lower']

## scales_src = glob.glob('@CMAKE_SOURCE_DIR@/parmepy/scales2py/*.f90')
## scalesModule = Extension(name='parmepy.scales2py',
##                          f2py_options=f2py_options,
##                          sources=scales_src,
##                          include_dirs=inc_dir,
##                          library_dirs=parmes_libdir,
##                          libraries=parmeslib,
##                          define_macros=[('F2PY_REPORT_ON_ARRAY_COPY', '1')])

## ext_modules=[scalesModule]

ext_modules =[]
fortran_dir = '@CMAKE_SOURCE_DIR@/parmepy/f2py/'
enable_fortran = False
withfftw = "@WITH_FFTW@"
if(withfftw is "ON"):
    #fortran_src = glob.glob('@CMAKE_SOURCE_DIR@/parmepy/f2py/*.f90')
    
    fortran_src = [fortran_dir+'parameters.f90',fortran_dir+'fftw2py.f90']
    enable_fortran = True
    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"):
    fortran_src.append(fortran_dir+'scales2py.f90')
    enable_fortran = True

if(enable_fortran):
#    rep = '@CMAKE_SOURCE_DIR@/parmepy/f2py/'
#   fortran_src.insert(0,rep+'/parameters.f90')
    parpyModule = Extension(name='parmepy.f2py',
                            f2py_options=f2py_options,
                            sources=fortran_src,
                            include_dirs=inc_dir,
                            library_dirs=parmes_libdir,
                            libraries=parmeslib,
                            define_macros=[('F2PY_REPORT_ON_ARRAY_COPY', '1')])
    ext_modules.append(parpyModule)

setup(name=name,
      version='1.0.0',
      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=[('./parmepy/particular_solvers', ['@CMAKE_SOURCE_DIR@/parmepy/particular_solvers/gpu_src.cl'])]
)
