# -*- coding: utf-8 -*-

import os
from conans import ConanFile, CMake, tools

class {package_name}Conan(ConanFile):
    python_requires = 'sesame/0.1.0@sesame/stable'
    python_requires_extend = "sesame.SesameBase"

    name = '{name}'
    version = '{version}'
    description = '<Description of {package_name} here>.'
    url = 'https://github.com/birsoyo/sesame'
    homepage = 'https://github.com/original_author/original_lib'
    author = 'Orhun Birsoy <orhunbirsoy@gmail.com>'

    license = '<Indicates License type of the packaged library>'

    exports_sources = ['CMakeLists.txt']
    generators = 'cmake'

    settings = 'os', 'compiler', 'build_type', 'arch'

    source_subfolder = 'source_subfolder'
    build_subfolder = 'build_subfolder'

    def source(self):
        source_url = 'https://github.com/libauthor/{name}'
        tools.get(f'{{source_url}}/archive/v{{self.version}}.tar.gz')
        extracted_dir = f'{{self.name}}-{{self.version}}'

        #Rename to "source_subfolder" is a convention to simplify later steps
        os.rename(extracted_dir, self.source_subfolder)

    def build(self):
        cmake = self._configure_cmake()
        cmake.build()

    def package(self):
        cmake = self._configure_cmake()
        cmake.install()
        # If the CMakeLists.txt has a proper install method, the steps below may be redundant
        # If so, you can just remove the lines below
        include_folder = os.path.join(self.source_subfolder, 'include')
        self.copy(pattern='*', dst='include', src=include_folder)
        self.copy(pattern='*.dll', dst='bin', keep_path=False)
        self.copy(pattern='*.lib', dst='lib', keep_path=False)
        self.copy(pattern='*.a', dst='lib', keep_path=False)
        self.copy(pattern='*.so*', dst='lib', keep_path=False)
        self.copy(pattern='*.dylib', dst='lib', keep_path=False)

    def package_info(self):
        self.cpp_info.libs = tools.collect_libs(self)

    def _configure_cmake(self):
        generator, defs = self._setup_cmake()

        defs['BUILD_TESTS'] = False # example

        cmake = CMake(self, generator=generator)
        cmake.verbose = True

        cmake.configure(defs=defs, build_folder=self.build_subfolder)
        return cmake
