#!/usr/bin/env python3
import os
from setuptools import setup, find_packages

# Read version from pyproject.toml or fallback to default
# We'll read the version directly from the pyproject.toml
# Cache the parsed metadata to avoid reading the file multiple times
_cached_metadata = None

def get_project_metadata():
    global _cached_metadata
    if _cached_metadata is not None:
        return _cached_metadata

    # Read project metadata from pyproject.toml manually to avoid toml dependency
    with open("pyproject.toml") as f:
        content = f.read()
        import re

        # Extract version
        version_match = re.search(r'version\s*=\s*["\']([^"\']+)["\']', content)
        version = version_match.group(1) if version_match else "0.1.9"

        # Extract description
        desc_match = re.search(r'description\s*=\s*["\']([^"\']+)["\']', content)
        description = desc_match.group(1) if desc_match else "A project to rewrite PortProton (PortWINE) using PySide"

        # Return just version and description (like in the example)
        _cached_metadata = (version, description, [])
        return _cached_metadata


def data_files_list():
    data_files = [
        (
            "share/applications",
            ["build-aux/usr/share/applications/ru.linux_gaming.PortProtonQt.desktop"],
        ),
        (
            "share/icons/hicolor/scalable/apps",
            ["build-aux/usr/share/icons/hicolor/scalable/apps/ru.linux_gaming.PortProtonQt.svg"],
        ),
        (
            "share/metainfo",
            ["build-aux/usr/share/metainfo/ru.linux_gaming.PortProtonQt.metainfo.xml"],
        ),
        (
            "share/bash-completion/completions",
            ["build-aux/usr/share/bash-completion/completions/portprotonqt"],
        ),
        (
            "lib/udev/rules.d",
            ["build-aux/usr/lib/udev/rules.d/60-portprotonqt.rules"],
        ),
    ]
    return data_files


setup(
    name="portprotonqt",
    version=get_project_metadata()[0],  # version
    author="Boris Yumankulov",
    author_email="boria138@altlinux.org",
    description=get_project_metadata()[1],  # description
    long_description=open("README.md").read() if os.path.exists("README.md") else "",
    long_description_content_type="text/markdown",
    url="https://git.linux-gaming.ru/Boria138/PortProtonQt",
    packages=find_packages(exclude=["build-aux", "dev-scripts", "documentation", "data"]),
    data_files=data_files_list(),
    entry_points={
        'console_scripts': [
            'portprotonqt = portprotonqt.app:main',
        ]
    },
    classifiers=[
        "Development Status :: 3 - Alpha",
        "Programming Language :: Python",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.10",
        "Programming Language :: Python :: 3.11",
        "Programming Language :: Python :: 3.12",
        "Programming Language :: Python :: 3.13",
        "Operating System :: POSIX :: Linux",
        "License :: OSI Approved :: GPL-3.0",
    ],
    python_requires=">=3.10",
    package_data={
        'portprotonqt': ['themes/**/*', 'locales/**/*'],
    },
)
