#!/usb/bin/env python
import os
import sys

from Cython.Build import cythonize
from numpy import get_include
from setuptools import setup, find_packages, Extension


try:
    from PyQt5.uic import compileUi
except ImportError:
    def compileUi(*args):
        pass

rcc_output = os.path.join('cct', 'resource', 'icons_rc.py')
rcc_input = os.path.join('cct', 'resource', 'icons', 'icons.qrc')
os.system('pyrcc5 {} -o {}'.format(rcc_input, rcc_output))


def compile_uis(packageroot):
    if compileUi is None:
        return
    for dirpath, dirnames, filenames in os.walk(packageroot):
        for fn in [fn_ for fn_ in filenames if fn_.endswith('.ui')]:
            fname = os.path.join(dirpath, fn)
            pyfilename = os.path.splitext(fname)[0] + '_ui.py'
            with open(pyfilename, 'wt', encoding='utf-8') as pyfile:
                compileUi(fname, pyfile, from_imports=True, import_from='cct.resource')
            print('Compiled UI file: {} -> {}.'.format(fname, pyfilename))


compile_uis(os.path.join('cct'))


def getresourcefiles():
    print('Generating resource list', flush=True)
    reslist = []
    for directory, subdirs, files in os.walk(os.path.join('cct', 'resource')):
        reslist.extend([os.path.join(directory, f).split(os.path.sep, 1)[1] for f in files])
    print('Generated resource list:\n  ' + '\n  '.join(x for x in reslist) + '\n', flush=True)
    return reslist


if sys.platform.lower().startswith('win') and sys.maxsize > 2 ** 32:
    krb5_libs = ['krb5_64']
else:
    krb5_libs = ['krb5']

extensions = [Extension("cct.core2.algorithms.radavg",
                        [os.path.join("cct", "core2", "algorithms", "radavg.pyx")],
                        include_dirs=[get_include()]),
              Extension("cct.core2.algorithms.cbfdecompress",
                        [os.path.join("cct", "core2", "algorithms", "cbfdecompress.pyx")],
                        include_dirs=[get_include()]),
              Extension("cct.core2.algorithms.centering.momentofinertia",
                        [os.path.join("cct", "core2", "algorithms", "centering", "momentofinertia.pyx")],
                        include_dirs=[get_include()]),
              Extension("cct.qtgui2.tools.maskeditor.maskoperations",
                        [os.path.join("cct", "qtgui2", "tools", "maskeditor", "maskoperations.pyx")],
                        include_dirs=[get_include()]),
              Extension("cct.core2.instrument.components.auth.user",
                        [os.path.join("cct", "core2", "instrument", "components", "auth", "user.pyx")],
                        include_dirs=[get_include()], libraries=krb5_libs),
              Extension("cct.core2.algorithms.beamweighting",
                        [os.path.join("cct", "core2", "algorithms", "beamweighting.pyx")],
                        include_dirs=[get_include()]),
              Extension("cct.core2.algorithms.correlmatrix",
                        [os.path.join("cct", "core2", "algorithms", "correlmatrix.pyx")],
                        include_dirs=[get_include()]),
              Extension("cct.core2.algorithms.schilling",
                        [os.path.join("cct", "core2", "algorithms", "schilling.pyx")],
                        include_dirs=[get_include()]),
              ]

print(get_include())
# extensions=[]

# update_languagespec()
setup(name='cct', author='Andras Wacha',
      author_email='awacha@gmail.com', url='http://gitlab.com/awacha/cct',
      description='CREDO Control Tool',
      packages=find_packages(),
      use_scm_version=True,
      setup_requires=['setuptools_scm'],
      #      cmdclass = {'build_ext': build_ext},
      ext_modules=cythonize(extensions),
      install_requires=['appdirs', 'numpy>=1.15.0', 'scipy>=1.0.0', 'matplotlib>=3.0.0',
                        'psutil>=4.1.0', 'h5py', 'pillow', 'openpyxl', 'sqlalchemy',
                        'adjusttext', 'imageio', 'click', 'colorlog', 'pyusb'],
      entry_points={'gui_scripts': ['cct4 = cct.__main__:main'],

                    },
      keywords="saxs sans sas small-angle scattering x-ray instrument control",
      license="",
      package_data={'': getresourcefiles()},
      #      include_package_data=True,
      zip_safe=False,
      )
