import os
from setuptools import setup, find_packages
from distutils.command.build import build
from distutils.dir_util import copy_tree
from subprocess import call


class SnowboyBuild(build):

    def run(self):

        cmd = ['make']

        def compile():
            call(cmd, cwd='swig/Python')

        self.execute(compile, [], 'Compiling snowboy...')

        # copy generated .so to build folder
        self.mkpath(self.build_lib)
        snowboy_build_lib = os.path.join(self.build_lib, 'snowboy')
        self.mkpath(snowboy_build_lib)
        target_file = 'swig/Python/_snowboydetect.so'
        if not self.dry_run:
            self.copy_file(target_file,
                           snowboy_build_lib)

            # copy resources too since it is a symlink
            resources_dir = 'resources'
            resources_dir_on_build = os.path.join(snowboy_build_lib,
                                                  'resources')
            copy_tree(resources_dir, resources_dir_on_build)

        build.run(self)


setup(
    name='snowboy',
    version='1.2.0b1',
    description='Snowboy is a customizable hotword detection engine',
    maintainer='KITT.AI',
    maintainer_email='snowboy@kitt.ai',
    license='Apache-2.0',
    url='https://snowboy.kitt.ai',
    packages=find_packages('examples/Python/'),
    package_dir={'snowboy': 'examples/Python/'},
    py_modules=['snowboy.snowboydecoder', 'snowboy.snowboydetect'],
    package_data={'snowboy': ['resources/*']},
    zip_safe=False,
    long_description="",
    classifiers=[],
    install_requires=[
        'PyAudio',
    ],
    cmdclass={
        'build': SnowboyBuild
    }
)
