#
# DO NOT EDIT THIS FILE!
# This file is generated by `scargo update`.
#
import os

from conan import ConanFile
from conan.tools.cmake import CMake, cmake_layout
from conan.tools.files import copy, get
from conan.tools.scm import Git

class {{ config.project.name|capitalize|replace("-", "") }}Conan(ConanFile):  # type: ignore[misc, no-any-unimported]
    name = "{{ config.project.name }}"
    version = "{{ config.project.version }}"
    license = "MIT"
    description = "{{ config.project.description }}"
    settings = "os", "compiler", "build_type", "arch"
    url = "{{ config.project.homepage_url }}"
    generators = "CMakeToolchain", "CMakeDeps"
    exports_sources = ["{{ config.source_dir_path.relative_to(config.project_root) }}/*", "CMakeLists.txt", "include/*", "config/*"]

    {% if config.dependencies.build or config.dependencies.tool %}
    def build_requirements(self) -> None:
        {% for dep in config.dependencies.build %}
        self.requires("{{ dep }}")
        {% endfor %}
        {% for tool_dep in config.dependencies.tool %}
        self.tool_requires("{{ tool_dep }}")
        {% endfor %}

    {% endif %}
    {% if config.dependencies.general %}
    def requirements(self) -> None:
        {% for dep in config.dependencies.general %}
        self.requires("{{ dep }}")
        {% endfor %}

    {% endif %}
    def source(self) -> None:
    {% if config.project.is_stm32() %}
        self._source_stm32()
    {% endif %}
    {% if config.project.is_atsam() %}
        self._source_atsam()
    {% endif %}
    {% if config.project.is_x86() or config.project.is_esp32() %}
        pass
    {% endif %}

    def layout(self) -> None:
        cmake_layout(self)

    def build(self) -> None:
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

    def package(self) -> None:
    {% if config.project.lib_name %}
        cmake = CMake(self)
        cmake.install()
    {% else %}
        copy("*.bin")
        copy("*.elf")
    {% endif %}
    {% if config.project.lib_name %}
    def package_info(self) -> None:
        self.cpp_info.libs = ["{{ config.project.name }}"]
    {% endif %}

    {% if config.project.is_stm32() %}
    def _source_stm32(self) -> None:
        target_dir = "third-party/stm32-cmake"
        if not os.path.isdir(target_dir):
            self.output.info("Sourcing for stm32")
            git = Git(self)
            git.clone("https://github.com/ObKo/stm32-cmake.git", target=target_dir)
    {% endif %}

    {% if config.project.is_atsam() %}
    def _source_atsam(self) -> None:
        import requests
        import re
        from pathlib import Path

        chip_series = "{{ config.atsam.chip_series }}"
        dfp_outdir = Path(f"{chip_series}_DFP")
        cmsis_outdir = Path("CMSIS")
        if dfp_outdir.is_dir() and cmsis_outdir.is_dir():
            return

        self.output.info("Sourcing for atsam")
        packs_url = "http://packs.download.atmel.com"

        if not dfp_outdir.is_dir():
            packs_web_content = requests.get(packs_url).text
            match_atpack = re.search(
                f'data-link="(Atmel\.{chip_series}\\S*\\.atpack)"',
                packs_web_content,
                re.MULTILINE | re.DOTALL,
            )
            dfp_filename = match_atpack.group(1)
            get(self, f"{packs_url}/{dfp_filename}", destination=dfp_outdir)

        if not cmsis_outdir.is_dir():
            cmsis_filename = "ARM.CMSIS.5.4.0.atpack"
            get(self, f"{packs_url}/{cmsis_filename}", destination=cmsis_outdir)
    {% endif %}
