""" Cython-based Python Module for Python 3 """
from buildutils import *

Import('env', 'build', 'install')

localenv = env.Clone()

libDirs = ('../../build/lib', localenv['sundials_libdir'],
           localenv['boost_lib_dir'])
localenv['py_cantera_libs'] = repr(localenv['cantera_libs'])
localenv['py_libdirs'] = repr([x for x in libDirs if x])

if localenv['CC'] == 'cl':
    localenv['py_extra_compiler_args'] = repr(['/EHsc'])
else:
    localenv['py_extra_compiler_args'] = repr([])

dataFiles = localenv.RecursiveInstall('#interfaces/cython/cantera/data',
                                      '#build/data')
build(dataFiles)

testFiles = localenv.RecursiveInstall('#interfaces/cython/cantera/test/data',
                                      '#test/data')
build(testFiles)

if localenv['python3_package'] == 'y':
    info = getCommandOutput(localenv['python3_cmd'], '-c',
                            '\n'.join(("from distutils.sysconfig import *",
                                       "import numpy",
                                       "print(get_config_var('SO'))",
                                       "print(get_python_version())",
                                       "print(numpy.get_include())")))

    module_ext, py3_version, numpy_include = info.splitlines()

    incDirs = (".", "../../include", numpy_include,
               localenv['sundials_include'], localenv['boost_inc_dir'])
    localenv['py_include_dirs'] = repr([x for x in incDirs if x])

    make_setup = localenv.SubstFile('#interfaces/cython/setup3.py',
                                    '#interfaces/cython/setup.py.in')
    build(make_setup)

    build_base = ('cd interfaces/cython &&'
                 ' $python3_cmd setup3.py %s'
                 ' --build-lib=../../build/python3'
                 ' --build-temp=../../build/temp-py3')

    build_cmd = build_base % 'build'
    build_ext_cmd = build_base % 'build_ext'

    ext = build(localenv.Command('#build/python3/cantera/_cantera%s' % module_ext,
                                 'setup3.py',
                                 build_ext_cmd))
    mod = build(localenv.Command('#build/python3/cantera/__init__.py',
                                 'setup3.py',
                                 build_cmd))
    env['python3_module'] = mod


    localenv.AddPreAction(ext, Delete('interfaces/cython/cantera/_cantera.cpp'))

    localenv.Depends(mod, ext)
    localenv.Depends(ext, localenv['cantera_staticlib'])
    localenv.Depends(mod, dataFiles + testFiles)
    for f in mglob(localenv, 'cantera', 'pyx', 'pxd'):
        localenv.Depends(ext, f)

    for f in mglob(localenv, 'cantera/test', 'py'):
        localenv.Depends(mod, f)

    # Install the Python module
    if localenv['python3_prefix']:
        # A specific location for the Cantera python module has been specified
        extra = '--prefix="%s"' % localenv['python3_prefix']
    else:
        # Install Python module in the default location
        extra = ''

    if localenv['PYTHON_INSTALLER'] == 'direct':
        install(localenv.Command, 'dummy3', mod,
                build_cmd + ' install --record ../../build/python3-installed-files.txt %s' % extra)
    elif localenv['PYTHON_INSTALLER'] == 'debian':
        install(localenv.Command, 'dummy3', mod,
                build_cmd + ' install --install-layout=deb --no-compile %s' % extra)
    elif localenv['PYTHON_INSTALLER'] == 'binary':
        install(localenv.Command, 'dummy3', mod,
                build_cmd + ' bdist_msi --dist-dir=../..' +
                ' --target-version=%s' + py3_version)


if localenv['python_package'] == 'new':
    info = getCommandOutput(localenv['python_cmd'], '-c',
                            '\n'.join(("from distutils.sysconfig import *",
                                       "import numpy",
                                       "print get_config_var('SO')",
                                       "print get_python_version()",
                                       "print numpy.get_include()")))


    module_ext, py2_version, numpy_include = info.splitlines()

    incDirs = (".", "../../include", numpy_include,
               localenv['sundials_include'], localenv['boost_inc_dir'])
    localenv['py_include_dirs'] = repr([x for x in incDirs if x])

    make_setup = localenv.SubstFile('#interfaces/cython/setup2.py',
                                    '#interfaces/cython/setup.py.in')
    build(make_setup)

    build_base = ('cd interfaces/cython &&'
                 ' $python_cmd setup2.py %s'
                 ' --build-lib=../../build/python2'
                 ' --build-temp=../../build/temp-py2')
    build_cmd = build_base % 'build'
    build_ext_cmd = build_base % 'build_ext'

    ext = build(localenv.Command('#build/python2/cantera/_cantera%s' % module_ext,
                                 'setup2.py',
                                 build_ext_cmd))
    mod = build(localenv.Command('#build/python2/cantera/__init__.py',
                                 'setup2.py',
                                 build_cmd))
    env['python2_module'] = mod

    localenv.AddPreAction(ext, Delete('interfaces/cython/cantera/_cantera.cpp'))

    localenv.Depends(mod, ext)
    localenv.Depends(ext, localenv['cantera_staticlib'])
    localenv.Depends(mod, dataFiles + testFiles)
    for f in mglob(localenv, 'cantera', 'pyx', 'pxd'):
        localenv.Depends(ext, f)

    for f in mglob(localenv, 'cantera/test', 'py'):
        localenv.Depends(mod ,f)

    # Install the Python module
    if localenv['python_prefix']:
        # A specific location for the Cantera python module has been specified
        extra = '--prefix="%s"' % localenv['python_prefix']
    else:
        # Install Python module in the default location
        extra = ''

    if localenv['PYTHON_INSTALLER'] == 'direct':
        install(localenv.Command, 'dummy2', mod,
                build_cmd + ' install --record ../../build/python2-installed-files.txt %s' % extra)
    elif localenv['PYTHON_INSTALLER'] == 'debian':
        install(localenv.Command, 'dummy2', mod,
                build_cmd + ' install --install-layout=deb --no-compile %s' % extra)
    elif localenv['PYTHON_INSTALLER'] == 'binary':
        install(localenv.Command, 'dummy2', mod,
                build_cmd + ' bdist_msi --dist-dir=../..' +
                ' --target-version=%s' % py2_version)
