# vidsrc/setup.py

"""Vidsrc package Setuptools script."""

import re
import sys

import numpy
from setuptools import Extension, setup

buildnumber = ''


def search(pattern: str, string: str, flags: int = 0) -> str:
    """Return first match of pattern in string."""
    match = re.search(pattern, string, flags)
    if match is None:
        raise ValueError(f'{pattern!r} not found')
    return match.groups()[0]


def fix_docstring_examples(docstring: str) -> str:
    """Return docstring with examples fixed for GitHub."""
    start = True
    indent = False
    lines = ['..', '  This file is generated by setup.py', '']
    for line in docstring.splitlines():
        if not line.strip():
            start = True
            indent = False
        if line.startswith('>>> '):
            indent = True
            if start:
                lines.extend(['.. code-block:: python', ''])
                start = False
        lines.append(('    ' if indent else '') + line)
    return '\n'.join(lines)


with open('vidsrc/vidsrc.cpp', encoding='utf-8') as fh:
    code = fh.read()

version = search(r'#define _VERSION_ "(.*?)"', code).replace('.x.x', '.dev0')
version += ('.' + buildnumber) if buildnumber else ''

readme = search(
    r'(?:\r\n|\r|\n)\"(.*)\"(?:\r\n|\r|\n){2}\#define _VERSION_',
    code,
    re.MULTILINE | re.DOTALL,
).replace('\\n\\', '')

description = readme.splitlines()[0][:-1]

readme = '\n'.join(
    [description, '=' * len(description)] + readme.splitlines()[1:]
)

if 'sdist' in sys.argv:

    with open('README.rst', 'w', encoding='utf-8') as fh:
        fh.write(fix_docstring_examples(readme))

    license = search(
        r'(Copyright.*)\*/(?:\r\n|\r|\n){2}\#define _DOC_',
        code,
        re.MULTILINE | re.DOTALL,
    )
    license = license.replace('# ', '').replace('#', '')

    with open('LICENSE', 'w', encoding='utf-8') as fh:
        fh.write('BSD-3-Clause license\n\n')
        fh.write(license)

DIRECTX_DIR = 'D:/Programs/DirectX9/include'
STRMBASE_DIR = 'D:/Programs/DirectX9/Samples/C++/DirectShow/BaseClasses'

setup(
    name='vidsrc',
    version=version,
    license='BSD-3-Clause',
    description=description,
    long_description=readme,
    long_description_content_type='text/x-rst',
    author='Christoph Gohlke',
    author_email='cgohlke@cgohlke.com',
    url='https://www.cgohlke.com',
    project_urls={
        'Bug Tracker': 'https://github.com/cgohlke/vidsrc/issues',
        'Source Code': 'https://github.com/cgohlke/vidsrc',
        # 'Documentation': 'https://',
    },
    python_requires='>=3.11',
    install_requires=['numpy'],
    packages=['vidsrc'],
    ext_modules=[
        Extension(
            'vidsrc.vidsrc',
            ['vidsrc/vidsrc.cpp'],
            include_dirs=[DIRECTX_DIR, STRMBASE_DIR, numpy.get_include()],
            library_dirs=[STRMBASE_DIR + '/libraries'],
            libraries=['STRMBASE', 'Ole32', 'OleAut32', 'strmiids'],
        )
    ],
    zip_safe=False,
    platforms=['Windows'],
    classifiers=[
        'Development Status :: 7 - Inactive',
        'Intended Audience :: Science/Research',
        'Intended Audience :: Developers',
        'Topic :: Multimedia :: Video',
        'Operating System :: Microsoft :: Windows',
        'Programming Language :: C++',
        'Programming Language :: Python :: 3 :: Only',
        'Programming Language :: Python :: 3.11',
        'Programming Language :: Python :: 3.12',
        'Programming Language :: Python :: 3.13',
        'Programming Language :: Python :: 3.14',
    ],
)
