# Class used to generate image based on Belden CoreOS

# We don't ihnerit from core-image, so not all core-image specific IMAGE_FEATURE
# are available. Only theses one are supported:
FEATURE_PACKAGES_tools-debug = "packagegroup-core-tools-debug"
FEATURE_PACKAGES_tools-profile = "packagegroup-core-tools-profile"
FEATURE_PACKAGES_hwcodecs = "${MACHINE_HWCODECS}"

FEATURE_PACKAGES_podman = "podman"
FEATURE_PACKAGES_podman-dev-tools = "podman-tui"
FEATURE_PACKAGES_podman-cockpit = "cockpit-podman"

FEATURE_PACKAGES_networkmanager = "networkmanager networkmanager-nmcli"
FEATURE_PACKAGES_networkmanager-dev-tools = "networkmanager-nmtui"
FEATURE_PACKAGES_networkmanager-cockpit = "cockpit-networkmanager"

FEATURE_PACKAGES_swupdate = "packagegroup-coreos-swupdate"

# The cockpit feature automatically install the corresponding
# *-cockpit FEATURES_PACKAGES for any image features
FEATURE_PACKAGES_cockpit = "packagegroup-coreos-cockpit ${@get_feature_packages_with_suffix('cockpit', d)}"

# The dev-tools feature automatically install the corresponding
# *-dev-tools FEATURES_PACKAGES for any image features
FEATURE_PACKAGES_dev-tools = "${@get_feature_packages_with_suffix('dev-tools', d)}"


def get_feature_packages_with_suffix(suffix, d):
    """
    For each feature inside IMAGE_FEATURES, look if a FEATURE_PACKAGE variable
    exist for {feature}-{suffix}.

    Return a list of all the value of the corresponding FEATURE_PACKAGE founded.
    """
    images_features = d.getVar('IMAGE_FEATURES').split()
    result = ""

    for feature in images_features:
        tools = d.getVar(f'FEATURE_PACKAGES_{feature}-{suffix}')
        if tools:
            result += f" {tools}"    
    return result

MACHINE_HWCODECS ??= ""

# These image features are CoreOS specifics:
#
# ssh-server: provide a ssh server, on core-image, two ssh server are supported
#             and the features ssh-server-dropbear and ssh-server-openssh are 
#             available. For CoreOS, we only support dropbear
FEATURE_PACKAGES_ssh-server = "packagegroup-core-ssh-dropbear"

# The ptest distro feature want to install openssh-ptest by default, that conflict with dropbear
# Do not install openssh complementary packages if either packagegroup-core-ssh-dropbear or dropbear
# is installed # to avoid openssh-dropbear conflict
# see [Yocto #14858] for more information
PACKAGE_EXCLUDE_COMPLEMENTARY:append = "${@bb.utils.contains_any('PACKAGE_INSTALL', 'packagegroup-core-ssh-dropbear dropbear', 'openssh', '' , d)}"


# We can handle feature that conflicts with either:

# IMAGE_FEATURES_REPLACES_foo = 'bar1 bar2'
# Including image feature foo would replace the image features bar1 and bar2

# IMAGE_FEATURES_CONFLICTS_foo = 'bar1 bar2'
# An error exception would be raised if both image features foo and bar1(or bar2) are included

COREOS_IMAGE_BASE_INSTALL = "\
    packagegroup-coreos-boot \
    packagegroup-coreos-base \
    secure-storage \
    "

COREOS_IMAGE_EXTRA_INSTALL ?= ""

IMAGE_INSTALL ?= "${COREOS_IMAGE_BASE_INSTALL} ${COREOS_IMAGE_EXTRA_INSTALL}"

# IMAGE_LINGUAS default value is set in image.bbclass, so we need to change it
# before ihneriting the image class, as we don't need to install custom locales
IMAGE_LINGUAS ?= " "

inherit image

# Enable some feature by default
IMAGE_FEATURES:append = " ${@bb.utils.contains('EXTRA_IMAGE_FEATURES', 'debug-tweaks', '', ' read-only-rootfs', d)}"

# Add some extra space, as done in core-image-minimal
IMAGE_ROOTFS_EXTRA_SPACE:append = " ${@bb.utils.contains('DISTRO_FEATURES', 'systemd', ' + 4096', '', d)}"

# Unified kernel image and swupdate support
# ==============================================================================

# Support for Unified Kernel Image and Swupdate are optional
COREOS_IMAGE_GENERATE_INSTALLER ?= "${@"1" if "efi" in d.getVar('COMBINED_FEATURES') and "swupdate" in d.getVar("DISTRO_FEATURES") else "0"}"
COREOS_IMAGE_GENERATE_UKI ?= "${@bb.utils.contains("COMBINED_FEATURES", "efi", "1", "0", d)}"
COREOS_IMAGE_GENERATE_SWU ?= "${@"1" if "efi" in d.getVar('COMBINED_FEATURES') and "swupdate" in d.getVar("DISTRO_FEATURES") else "0"}"

# Generate the installer image if needed
do_build[depends] += "${@'coreos-image-installer:do_build' if d.getVar('COREOS_IMAGE_GENERATE_INSTALLER') == '1' else ''}"

COREOS_IMAGE_EXTRACLASSES += "${@'coreos-image-uki' if d.getVar('COREOS_IMAGE_GENERATE_UKI') == '1' else ''}"
COREOS_IMAGE_EXTRACLASSES += "${@'coreos-image-swupdate' if d.getVar('COREOS_IMAGE_GENERATE_SWU') == '1' else ''}"

inherit ${COREOS_IMAGE_EXTRACLASSES}
