from conan.tools.scm import Git
from conan import ConanFile, tools
from conan.tools.gnu import AutotoolsToolchain, Autotools
from conan.tools.layout import basic_layout


class PostalConan(ConanFile):
    name = "postal"
    package_type = "library"
    user = "aurora"

    # Optional metadata
    license = "MIT license"
    author = "Vladislav Larionov <vladislav.larionov@fruct.org>"
    url = "https://gitverse.ru/auroraos/mirror_libpostal"
    description = "A C library for parsing/normalizing street addresses around the world." \
        "Powered by statistical NLP and open geo data."
    topics = ("map", "nlp", "addresse")

    # Binary configuration
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True]}


    def source(self):
        git = Git(self)
        git.clone(url="https://github.com/openvenues/libpostal.git", target=".")
        branch = self.conan_data["sources"][self.version]["commit"]
        git.checkout(commit=branch)

    def layout(self):
        basic_layout(self)

    def generate(self):
        at_toolchain = AutotoolsToolchain(self)
        at_toolchain.generate()

    def build(self):
        autotools = Autotools(self)
        autotools.autoreconf(args=["-fi", "--warning=no-portability"])
        args = [
            "--disable-sse2",
            "--disable-data-download"
        ]
        if self.settings.arch == "armv7":
            args.append("--with-cflags-scanner-extra=-marm")
        autotools.configure(args=args)
        autotools.make()

    def package(self):
        autotools = Autotools(self)
        autotools.install()

    def package_info(self):
        self.cpp_info.libs = ["postal"]
