
import os

from conan import ConanFile
from conan.tools.gnu import AutotoolsToolchain, Autotools, PkgConfigDeps
from conan.tools.layout import basic_layout
from conan.tools.files import copy,get,rmdir


class FfmpegConan(ConanFile):
    name = "ffmpeg"
    user = "aurora"
    package_type = "library"

    # Optional metadata
    license = "LGPL-2.1-or-later"
    url = "https://gitverse.ru/auroraos/mirror_ffmpeg"
    description = "A toolbox to manipulate, convert and stream multimedia content"

    # Binary configuration
    settings = "os", "compiler", "build_type", "arch"
    options = {
        "shared": [True],
        "with_openh264": [True, False],
        "with_opus": [True, False],
        "with_libvpx": [True, False],
    }
    default_options = {
        "with_openh264": True,
        "with_opus": True,
        "with_libvpx": True,
    }

    def requirements(self):
        if self.options.with_openh264:
            self.requires("openh264/2.5.0@aurora")
        if self.options.with_opus:
            self.requires("opus/1.5.2@aurora")
        if self.options.with_libvpx:
            self.requires("libvpx/1.15.2@aurora")

    def source(self):
        get(self, **self.conan_data["sources"][self.version], strip_root=True)

    def layout(self):
        basic_layout(self)

    @property
    def _target_arch(self):
        # Taken from acceptable values https://github.com/FFmpeg/FFmpeg/blob/0684e58886881a998f1a7b510d73600ff1df2b90/configure#L5010
        if str(self.settings.arch).startswith("armv8"):
            return "aarch64"
        elif self.settings.arch == "x86":
            return "i686"
        return str(self.settings.arch)

    def generate(self):
        deps = PkgConfigDeps(self)
        deps.generate()
        at_toolchain = AutotoolsToolchain(self)
        at_toolchain.update_configure_args(
            {
                # Some conan directives are not supported by the configure script of ffmpeg
                "--sbindir": None,
                "--includedir": None,
                "--oldincludedir": None,
                "--datarootdir": None,
                "--build": None,
                "--host": None,
                "--target": None,
                # Disabling documentation, static files, programs
                "--disable-static": "",
                "--disable-doc": "",
                "--disable-x86asm": "",
                "--disable-programs": "",
                # Disabling library that provides access to the devices as it is not allowed
                "--disable-avdevice": "",
                # Disable access to libdrm as it is prohibited by validator
                "--disable-libdrm": "",
                # Enabling build of shared libraries
                "--enable-shared": "",
                "--arch": self._target_arch,
            }
        )
        # Enable more codecs
        if self.options.with_openh264:
            at_toolchain.update_configure_args(
                {
                    "--enable-libopenh264": "",
                }
            )
        if self.options.with_opus:
            at_toolchain.update_configure_args(
                {
                    "--enable-libopus": "",
                    "--enable-encoder": "libopus",
                }
            )
        if self.options.with_libvpx:
            at_toolchain.update_configure_args(
                {
                    "--enable-libvpx": "",
                }
            )
        at_toolchain.generate()

    def build(self):
        autotools = Autotools(self)
        autotools.configure()
        autotools.make()

    def package(self):
        copy(self, "LICENSE.md", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
        autotools = Autotools(self)
        autotools.install()
        rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
        rmdir(self, os.path.join(self.package_folder, "share"))

    def package_info(self):
        self.cpp_info.libs = [
            "avcodec",
            "avfilter",
            "avformat",
            "avutil",
            "swresample",
            "swscale",
        ]

