# coding=utf-8
{% set min_version = MIN_PYTHON_VERSION.split('.')[1] | int %}
{% set max_version = MAX_PYTHON_VERSION.split('.')[1] | int %}
{% if code_model.license_header %}
{{ code_model.license_header }}
{% endif %}

{% if options["package-mode"] %}

import os
import re
{% endif %}
from setuptools import setup, find_packages

{% set package_name = options["package-name"] or code_model.clients[0].name %}

PACKAGE_NAME = "{{ package_name|lower }}"
{% if options["package-mode"] %}
PACKAGE_PPRINT_NAME = "{{ options["package-pprint-name"] }}"
{% if code_model.is_tsp %}
PACKAGE_NAMESPACE = "{{ code_model.namespace|lower }}"

# a.b.c => a/b/c
package_folder_path = PACKAGE_NAMESPACE.replace(".", "/")
{% else %}

# a-b-c => a/b/c
package_folder_path = PACKAGE_NAME.replace("-", "/")
{% endif %}

# Version extraction inspired from 'requests'
with open(os.path.join(package_folder_path, "_version.py"), "r") as fd:
    version = re.search(
        r'^VERSION\s*=\s*[\'"]([^\'"]*)[\'"]', fd.read(), re.MULTILINE
    ).group(1)

if not version:
    raise RuntimeError("Cannot find version information")
{% set description = "\"" + code_model.company_name + " {} Client Library for Python\".format(PACKAGE_PPRINT_NAME)" %}
{% set author_email = "azpysdkhelp@microsoft.com" %}
{% set url = "https://github.com/Azure/azure-sdk-for-python/tree/main/sdk" %}
{% else %}
version = "{{ options.get("package-version", "unknown") }}"
{% set description = "\"%s\""|format(package_name) %}
{% set long_description = code_model.description %}
{% set author_email = "" %}
{% set url = "" %}
{% endif %}


setup(
    name=PACKAGE_NAME,
    version=version,
    description={{ description }},
    {% if options["package-mode"] %}
    long_description=open("README.md", "r").read(),
    long_description_content_type="text/markdown",
    license="MIT License",
    author="{{ code_model.company_name }}",
    {% endif %}
    {% if code_model.is_azure_flavor %}
    author_email="{{ author_email }}",
    url="{{ url }}",
    keywords="azure, azure sdk",
    {% endif %}
    {% if options["package-mode"] %}
    classifiers=[
        "Development Status :: {{ dev_status }}",
        "Programming Language :: Python",
        "Programming Language :: Python :: 3 :: Only",
        "Programming Language :: Python :: 3",
        {% for version in range(min_version, max_version + 1)  %}
        "Programming Language :: Python :: 3.{{ version }}",
        {% endfor %}
        "License :: OSI Approved :: MIT License",
    ],
    zip_safe=False,
    packages=find_packages(
        exclude=[
            "tests",
            {% if pkgutil_names %}
            # Exclude packages that will be covered by PEP420 or nspkg
            {% endif %}
            {% for pkgutil_name in pkgutil_names %}
            "{{ pkgutil_name }}",
            {% endfor %}
        ]
    ),
    include_package_data=True,
    package_data={
        '{{ code_model.namespace }}': ['py.typed'],
    },
    {% else %}
    packages=find_packages(),
    include_package_data=True,
    {% endif %}
    install_requires=[
        {% if code_model.is_legacy %}
        "msrest>={{ VERSION_MAP["msrest"] }}",
        {% else %}
        "isodate>={{ VERSION_MAP["isodate"] }}",
        {% endif %}
        {% if code_model.options["azure-arm"] %}
        "azure-mgmt-core>={{ VERSION_MAP["azure-mgmt-core"] }}",
        {% elif code_model.is_azure_flavor %}
        "azure-core>={{ VERSION_MAP["azure-core"] }}",
        {% else %}
        "corehttp[requests]>={{ VERSION_MAP["corehttp"] }}",
        {% endif %}
        "typing-extensions>={{ VERSION_MAP['typing-extensions'] }}",
    ],
    {% if options["package-mode"] %}
    python_requires=">={{  MIN_PYTHON_VERSION }}",
    {% else %}
    long_description="""\
    {{ code_model.description }}
    """
    {% endif %}
)
