DESCRIPTION = "CoreOS Installer configuration files"
SECTION = "coreos"
LICENSE = "CLOSED"

# This file can be included by a CoreOS based distro to ship a customized
# version
RPROVIDES:${PN} += "coreos-installer-config"
PROVIDES += "coreos-installer-config"

# This package is intended to ship machine specific configuration file
# See bbappend in BSP layers
PACKAGE_ARCH = "${MACHINE_ARCH}"

# If the BSP doesn't provide a machine specific configuration file, this
# package will be empty
ALLOW_EMPTY:${PN} = "1"

# Don't run default configure and compile task
do_configure[noexex] = "1"

python do_compile() {
    from pathlib import Path

    workdir = Path(d.getVar('WORKDIR', True))
    machine = d.getVar('MACHINE', True)
    b = Path(d.getVar('B', True))
    for input_path in workdir.glob(f'{machine}_*.sfdisk'):
        output_path = b / input_path.relative_to(workdir)
        process_sfdisk_file(d, input_path, output_path)
        
}

def process_sfdisk_file(d, input_path, output_path):
    """
    Read the file from input_path and write it to output_path with bitbake
    variable expanded
    """
    with open(output_path, "w") as output_file:
        with open(input_path, "r") as input_file:
            for line in input_file.readlines():
                output_file.write(d.expand(line))

do_install() {
    install -d ${D}${sysconfdir}

    # Install all files for the current machine but don't fail if no file is
    # found
    install -m 755 ${B}/${MACHINE}_*.sfdisk ${D}${sysconfdir}/ || true
}
