import os
import shutil
from collections import defaultdict

from setuptools import setup, find_packages

HERE = os.path.abspath(os.path.dirname(__file__))
VERSION = "0.8.1"

with open(os.path.join(HERE, "README.md"), encoding="utf-8") as f:
    long_description = f.read()

requirements = defaultdict(list)
for name in os.listdir(os.path.join(HERE, "requirements")):
    if name not in ("base.in", "pkg.in", "test.in"):
        continue
    reqs = requirements[name.rpartition(".")[0]]
    with open(os.path.join(HERE, "requirements", name)) as f:
        for line in f:
            line = line.strip()
            if line.startswith("#") or line.startswith("-r"):
                continue
            reqs.append(line)
install_requirements = requirements.pop("base")


def _package_data_files(pkg: str):
    root = os.path.join(HERE, pkg)
    for parent, _, files in os.walk(root):
        for file in files:
            if file.endswith(".py") or file.endswith(".pyc"):
                continue
            yield os.path.relpath(os.path.join(parent, file), root)


package_data = {
    "clarabot_nano": sorted(
        x for x in _package_data_files("clarabot_nano") if not x.endswith(".pot") and not x.endswith(".po")
    ),
    # FIXME: Include the test suite, once it is possible to run it outside of the internal development system.
}
if package_data != {
    "clarabot_nano": [
        "resource/image/clarabot.ico",
        "resource/image/clarabot_nano.icns",
        "resource/image/clarabot_nano.ico",
        "resource/image/clarabot_nano_256.png",
        "resource/image/wiz.png",
        "resource/locale/hu/LC_MESSAGES/clarabot-nano.mo",
        "resource/spawn/tika/log4j.properties",
        "resource/spawn/tika/log4j2.xml",
        "resource/spawn/tika/security.policy",
        "resource/template/local.yml",
        "resource/template/remote.yml",
        "resource/template/solr/configsets/nano/conf/managed-schema",
        "resource/template/solr/configsets/nano/conf/solrconfig.xml",
        "resource/template/solr/configsets/nano/lang/en/snippet.xml",
        "resource/template/solr/configsets/nano/lang/hu/snippet.xml",
        "resource/template/solr/solr.xml",
    ]
}:
    raise Exception(f"Package data has changed\n{str(package_data)}")


# Manually cleaning before build is required.
for p in [os.path.join(HERE, "build"), os.path.join(HERE, "dist"), os.path.join(HERE, "clarabot_nano.egg-info")]:
    if os.path.exists(p):
        shutil.rmtree(p)

setup(
    name="clarabot-nano",
    version=VERSION,
    description="Clarabot Nano client application",
    long_description=long_description,
    url="clarabot.com",
    author="Nándor Mátravölgyi",
    author_email="nmatravolgyi@clarabot.com",
    # license="",
    classifiers=["License :: Other/Proprietary License"],
    packages=[p for p in find_packages(where=HERE, include=["clarabot_nano*"]) if not p.startswith("clarabot_nano_")],
    package_data=package_data,
    install_requires=install_requirements,
    # entry_points={"console_scripts": console_scripts},
    extras_require=dict(requirements),
    python_requires=">=3.9",
)
