#!/usr/bin/python3

"""Split the completed kernel/initramfs and seal the native composefs image."""

import argparse
import os
from pathlib import Path
import shutil
import subprocess


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("root", type=Path)
    parser.add_argument("kver", type=Path)
    args = parser.parse_args()

    root = args.root.resolve()
    boot = root / "boot"
    modules = root / "usr/lib/modules"
    kernel_version = args.kver

    # The kernel's LZ4 decoder needs at least four NUL bytes to stop before
    # archives appended by systemd-stub. Also align to four bytes, as rpm-ostree does:
    # https://github.com/coreos/rpm-ostree/commit/e662e07bb043ec3b3dd026c217fc900b0d92267e
    with (modules / kernel_version / "initramfs.img").open("r+b") as initramfs:
        initramfs.seek(0, os.SEEK_END)
        initramfs.write(b"\0" * (4 + (-initramfs.tell() % 4)))

    # This must follow dracut: splitting removes its inputs from the rootfs.
    subprocess.run(
        [
            "bootc",
            "container",
            "split-kernel-and-rootfs",
            "--rootfs",
            root,
            "--output",
            boot,
        ],
        check=True,
    )
    (boot / "EFI/Linux").mkdir(parents=True, exist_ok=True)

    # Complete all rootfs changes before aboot computes the composefs digest.
    try:
        subprocess.run(
            [
                "bootc",
                "container",
                "aboot",
                "--rootfs",
                root,
                "--kernel-dir",
                boot / kernel_version,
                "--write-dumpfile-to",
                boot / "pre-oci.dump",
            ],
            check=True,
        )
    finally:
        shutil.rmtree(boot / kernel_version)


if __name__ == "__main__":
    main()
