# This class add some sanity checks to ensure that distribution based on
# CoreOS only use the subset of openembedded-core that is supported by the
# CoreOS team.
#
# This is also used to add some warnings to ensure to ensure a more common
# CoreOS developer Experience, by ensuring that some default distro and machine
# are set correctly


SANITY_COREOS_COMPATIBLE ??= "0"

addhandler check_coreos_sanity_eventhandler
check_coreos_sanity_eventhandler[eventmask] = "bb.event.SanityCheck"
python check_coreos_sanity_eventhandler() {

    # Checks related to the distribution configuration files
    # ==========================================================================

    if e.data.getVar('SANITY_COREOS_COMPATIBLE') != "1":
            bb.fatal(
            "The CoreOS layer is only compatible with distribution based on "
            "conf/distro/belden-coreos.conf.\n"
            "Please ensure that your distribution configuration file contains "
            " `require conf/distro/belden-coreos.conf`"
        )

    if e.data.getVar('INIT_MANAGER') != "systemd":
        bb.fatal(
            "systemd is not set as `INIT_MANAGER`. "
            "Using SystemD is mandatory on CoreOS based distribution"
        )
    
    if e.data.getVar("TCLIBC") != "glibc":
        bb.fatal(
            "glibc is not set as `TCLIBC`. "
            "Using glibc is mandatory on CoreOS based distribution"
        )
    
    # Checks related to the machine configuration files
    # ==========================================================================

    # Ensure that compressed image and bmap file are generated if wic is used
    fs_types = e.data.getVar("IMAGE_FSTYPES").split()
    if any(map(lambda x: x.startswith("wic"), fs_types)):
        if not "wic.xz" in fs_types:
            bb.warn(
                "CoreOS recommands to use compressed wic image, please add "
                "`wic.xz` to your machine `IMAGE_FSTYPES` variables"
            )
        
        if not "wic.bmap":
            bb.warn(
                "wic image should be flashed with bmaptools, but this require "
                "to add `wic.bmap` to your machine `IMAGE_FSTYPES` variables"
            )

    return
}
