# -*- coding: utf-8 -*-

import sys
import os
import codecs
import numpy

from setuptools import setup, Extension

if len(sys.argv) > 2 and sys.argv[1] == '--build-docs':
    import subprocess
    from setuptools import Distribution
    from setuptools.command.build import build

    dist = Distribution()
    cmd = build(dist)
    cmd.finalize_options()
    #print(cmd.build_platlib)

    os.environ['PYTHONPATH'] = os.path.join(os.getcwd(), cmd.build_platlib)
    os.environ['LD_LIBRARY_PATH'] = os.getcwd()

    ret = subprocess.call(sys.argv[2:])
    sys.exit(ret)


# the following line is not working due to https://bugs.python.org/issue9023
#source_directory = os.path.relpath('@PYTHON_SOURCE_DIRECTORY@')
source_directory = '@PYTHON_SOURCE_DIRECTORY@'

extra_args = []

if sys.platform == 'win32':
    extra_args.append('/EHsc')
    extra_args.append('/DAUD_BUILD_SHARED_LIBRARY')
else:
    extra_args.append('-std=c++11')

audaspace = Extension(
                      'aud',
                      include_dirs = ['@CMAKE_CURRENT_BINARY_DIR@', '@FFTW_INCLUDE_DIR@', os.path.join(source_directory, '../../include'), numpy.get_include()],
                      libraries = ['audaspace'],
                      library_dirs = ['.', 'Release', 'Debug'],
                      language = 'c++',
                      extra_compile_args = extra_args,
                      define_macros = [('WITH_CONVOLUTION', None)] if '@WITH_FFTW@' == 'ON' else [],
                      sources = [os.path.join(source_directory, file) for file in ['PyAPI.cpp', 'PyDevice.cpp', 'PyHandle.cpp', 'PySound.cpp', 'PySequenceEntry.cpp', 'PySequence.cpp', 'PyPlaybackManager.cpp', 'PyDynamicMusic.cpp', 'PyThreadPool.cpp', 'PySource.cpp'] + (['PyImpulseResponse.cpp', 'PyHRTF.cpp'] if '@WITH_FFTW@' == 'ON' else [])]
)

setup(
      name = 'audaspace',
      version = '@AUDASPACE_LONG_VERSION@',
      description = 'Audaspace is a high level audio library.',
      author = 'Jörg Müller',
      author_email = 'nexyon@gmail.com',
      url = 'https://github.com/audaspace/audaspace',
      license = 'Apache License 2.0',
      long_description = codecs.open(os.path.join(source_directory, '../../README.md'), 'r', 'utf-8').read(),
      ext_modules = [audaspace],
      headers = [os.path.join(source_directory, file) for file in ['PyAPI.h', 'PyDevice.h', 'PyHandle.h', 'PySound.h', 'PySequenceEntry.h', 'PySequence.h', 'PyPlaybackManager.h', 'PyDynamicMusic.h', 'PyThreadPool.h', 'PySource.h'] + (['PyImpulseResponse.h', 'PyHRTF.h'] if '@WITH_FFTW@' == 'ON' else [])] + ['Audaspace.h']
)

