from codecs import open as codecs_open
from setuptools import setup, find_packages, Extension
from setuptools.command.build_ext import build_ext

import os, pathlib

import pybind11

class CMakeExtension(Extension):
    def __init__(self, name):
        # don't invoke the original build_ext for this special extension
        super().__init__(name, sources=[])

class cmake_build_ext(build_ext):
    def run(self):
        for ext in self.extensions:
            self.build_cmake(ext)
        super().run()

    def build_cmake(self, ext):
        cwd = pathlib.Path().absolute()

        # these dirs will be created in build_py, so if you don't have
        # any python sources to bundle, the dirs will be missing
        build_temp = pathlib.Path(self.build_temp)
        build_temp.mkdir(parents=True, exist_ok=True)
        extdir = pathlib.Path(self.get_ext_fullpath(ext.name))
        # extdir.mkdir(parents=True, exist_ok=True)

        # example of cmake args
        config = 'Debug' if self.debug else 'Release'
        cmake_args = [
            '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + str(extdir.parent.absolute()),
            '-DCMAKE_BUILD_TYPE=' + config,
            '-DCMAKE_PREFIX_PATH=' + pybind11.get_cmake_dir(),
            # '-DHARK_PYBIND11_INCLUDE_PATH=' + pybind11.get_include(),
        ]

        # example of build args
        build_args = [
            '--config', config,
            '--', '-j1'
        ]

        os.chdir(str(build_temp))
        self.spawn(['cmake', str(cwd)] + cmake_args)
        if not self.dry_run:
            self.spawn(['cmake', '--build', '.'] + build_args)
        os.chdir(str(cwd))

# ext_modules = [
#     Extension(
#         'harkmwnative',
#         # Sort input source files to ensure bit-for-bit reproducible builds
#         # (https://github.com/pybind/python_example/pull/53)
#         sources = sorted([
#             'harkmw/src/node.cc',
#             'harkmw/src/libmain.cc',
#             'harkmw/src/proxy.cc',
#             'harkmw/src/type.cc',
#             'harkmw/src/collector.cc',
#             ]),
#         include_dirs=[
#             py_incdir,
#             hb_incdir,
#             hb_incdir[:hb_incdir.rfind('/')],
#             pybind11.get_include(),
#             'harkmw/src'],
#         library_dirs=[hb_libdir],
#         runtime_library_dirs=[hb_libdir] if os.name != 'nt' else None,
#         libraries=[hb_libname],
#         language='c++',
#         extra_compile_args = ["-g", "-O0"],
#         # extra_compile_args=pkgconfig.cflags('hark-base'),
#         # library_dirs=[hb_libdir],
#         # runtime_library_dirs=[hb_libdir] if os.name != 'nt' else None,
#         # libraries=[hb_libname],
#     ),
# ]

# Get the long description from the relevant file
with codecs_open('README.rst', encoding='utf-8') as f:
    long_description = f.read()

#print(find_packages())
setup(name='hark-lib',
      version='0.0.0',
      description=u"HARK library for python",
      long_description=long_description,
      classifiers=[],
      keywords='',
      author=u"HRI Japan",
      license='HARK License',
      packages=find_packages(),
      # include_package_data=True,
      package_data={
        # 'hark.device': ['data/*.xpm'],
      },
      zip_safe=False,
      install_requires=[
        'pybind11>=2.6',
      ],
      ext_modules=[CMakeExtension('harklib')],
      cmdclass={'build_ext': cmake_build_ext},
      extras_require={
      },
      entry_points={
        'console_scripts': [
            'harklib-sample=hark.sample.sample:main'
        ]
      }
)
