from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
from Cython.Build import cythonize
import os

exclude_files = {"kare", "kare-dbus", "kareAgent", "build-upgrade", "kare-watch"}
exclude_dirs = {"__pycache__", ".git", "venv"}

class CustomBuildExt(build_ext):
    def get_ext_fullpath(self, ext_name):
        original_path = super().get_ext_fullpath(ext_name)
        parts = original_path.split(os.sep)
        build_dir = parts[0]
        platform_dir = parts[1]
        new_parts = [build_dir, platform_dir] + parts[2:]
        return os.path.join(*new_parts)

def find_py_files():
    py_files = []
    for root, dirs, files in os.walk("src", topdown=True):
        dirs[:] = [d for d in dirs if d not in exclude_dirs]
        for file in files:
            if file in exclude_files or not file.endswith(".py"):
                continue
            full_path = os.path.join(root, file)
            rel_path = os.path.relpath(full_path, start="src").replace(".py", "")
            module_name = rel_path.replace(os.path.sep, ".")
            py_files.append((module_name, full_path))
    return py_files

extensions = []
for module_name, src_file in find_py_files():
    extensions.append(Extension(module_name, [src_file]))

setup(
    name="kare",
    ext_modules=cythonize(
        extensions,
        compiler_directives={"language_level": "3"},
        nthreads=4,
    ),
    cmdclass={'build_ext': CustomBuildExt}
)