# This class should only be used when running in the CI environment of CoreOS
# On the CI, this class is ihnerited by adding
# > COREOS_IMAGE_EXTRACLASSES += "coreos-image-ci"
# in auto.conf (or local.conf)

inherit kernel-artifact-names

def get_coreos_ci_artifacts(d):
    artifacts = []

    # common variable
    machine = d.getVar('MACHINE')

    # Container handling
    # ==========================================================================

    if bb.utils.contains('IMAGE_FSTYPES', 'oci', True, False, d):

        artifacts.append(d.getVar('IMAGE_LINK_NAME') + '.rootfs-oci.tar')

        # Special case for container, we just need the OCI tarball
        return " ".join(artifacts)

    # SDCard image and Swupdate image
    # ==========================================================================

    if bb.utils.contains('IMAGE_FSTYPES', 'wic.xz', True, False, d):
        artifacts.append(d.getVar('IMAGE_LINK_NAME') + '.wic.xz')

    if bb.utils.contains('IMAGE_FSTYPES', 'wic.bmap', True, False, d):
        artifacts.append(d.getVar('IMAGE_LINK_NAME') + '.wic.bmap')

    # This is used for qemu-coreos-arm64
    if bb.utils.contains('IMAGE_FSTYPES', 'wic.qcow2', True, False, d):
        artifacts.append(d.getVar('IMAGE_LINK_NAME') + '.wic.qcow2')

    if d.getVar('COREOS_IMAGE_GENERATE_SWU') == '1':
        artifacts.append(d.getVar('IMAGE_LINK_NAME') + '.swu')

    # CoreOS Installer
    # ==========================================================================

    if d.getVar('COREOS_IMAGE_GENERATE_INSTALLER') == '1':
        artifacts.append('coreos-installer-' + d.getVar('MACHINE') + '.efi')

    # Kernel
    # ==========================================================================

    kernel_imagetype = d.getVar('KERNEL_IMAGETYPE', None)
    kernel_image_bin_ext = d.getVar('KERNEL_IMAGE_BIN_EXT') or ''
    artifacts.append(kernel_imagetype + '-' + machine + kernel_image_bin_ext)

    if d.getVar('COREOS_IMAGE_GENERATE_UKI') == '1':
        artifacts.append(d.getVar('COREOS_KERNEL_FILENAME'))

    # Bootloaders
    # ==========================================================================
    if 'efi' in d.getVar('MACHINE_FEATURES', ''):
        if d.getVar('EFI_PROVIDER', "") == 'efibootguard':
            efi_arch = d.getVar('EFI_ARCH')
            artifacts.append('efibootguard' + efi_arch + '.efi')

    # Machine specific files
    # ==========================================================================
    # Add file from IMAGE_BOOT_FILES IMAGE_EFI_BOOT_FILES
    # More machine spefic file can be added inside the machine configuration
    # file using:
    # > COREOS_CI_DEPLOY_ARTIFACTS += "filename"

    def remove_suffix(f):
        # IMAGE_BOOT_FILES and IMAGE_EFI_BOOT_FILES support to have ; in the
        # name to rename the file inside the boot partition, so we need
        # to remove this suffis
        return f.split(";", 1)[0]

    image_boot_files = d.getVar('IMAGE_BOOT_FILES') or ''
    artifacts += map(remove_suffix, image_boot_files.split(' '))

    image_efi_boot_files = d.getVar('IMAGE_EFI_BOOT_FILES') or ''
    artifacts += map(remove_suffix, image_efi_boot_files.split(' '))

    return " ".join(artifacts)

# Using a variable to store the list of file allow machine configuration to add
# machine specific item before generating the file. This also allow the use of
# :remove to remove an element from the list
COREOS_CI_DEPLOY_ARTIFACTS += "${@get_coreos_ci_artifacts(d)}"

do_deploy_ci() {
    # Create the .ci-artifacts file inside the deploy directory
    cd "${DEPLOY_DIR_IMAGE}"
    output="${IMAGE_LINK_NAME}.ci-artifacts"

    rm -f "${output}"
    for file in ${COREOS_CI_DEPLOY_ARTIFACTS}; do
        echo $file >> $output
    done
}
addtask deploy_ci after do_image before do_build