function setSelinux {
    source (loop0)/etc/os-release

    # Disable selinux for all distros. Supporting selinux requires more than
    # just enabling it like this.
    set baseSelinuxCmd="selinux=0"

    #if test $KAIROS_FAMILY == "rhel" -o test $ID == "opensuse-tumbleweed" -o test $ID == "opensuse-leap"; then
    #    set baseSelinuxCmd="selinux=0"
    #else
    #    # if not in recovery
    #    if [ -z "$recoverylabel" ];then
    #        set baseSelinuxCmd="security=selinux selinux=1"
    #    fi
    #fi
}

function setExtraConsole {
    source (loop0)/etc/os-release
    set baseExtraConsole="console=ttyS0"
    # rpi
    if test $KAIROS_MODEL == "rpi3" -o test $KAIROS_MODEL == "rpi4"; then
        set baseExtraConsole="console=ttyS0,115200"
    fi
    # nvidia orin
    if test $KAIROS_MODEL == "nvidia-jetson-agx-orin"; then
        set baseExtraConsole="console=ttyTCU0,115200"
    fi
}

function setExtraArgs {
    source (loop0)/etc/os-release
    set baseExtraArgs=""
    # rpi
    if test $KAIROS_MODEL == "rpi3" -o test $KAIROS_MODEL == "rpi4"; then
        # on rpi we need to enable memory cgroup for docker/k3s to work
        set baseExtraArgs="modprobe.blacklist=vc4 8250.nr_uarts=1 cgroup_enable=memory"
    fi
}

function setKernelCmd {
    # At this point we have the system mounted under (loop0)
    #
    # baseCmd -> Shared between all entries
    # baseRootCmd -> specific bits that immucore uses to mount the boot devices and identify the image to mount
    # baseSelinuxCmd -> selinux enabled/disabled
    # baseExtraConsole -> extra console to set
    # baseExtraArgs -> extra needed args
    set baseCmd="console=tty1 net.ifnames=1 rd.cos.oemlabel=COS_OEM rd.cos.oemtimeout=10 panic=5 rd.emergency=reboot rd.shell=0 systemd.crash_reboot=yes"
    if [ -n "$recoverylabel" ]; then
        set baseRootCmd="root=live:LABEL=$recoverylabel rd.live.dir=/ rd.live.squashimg=$img"
    else
        set baseRootCmd="root=LABEL=$label cos-img/filename=$img"
    fi
    setSelinux
    setExtraConsole
    setExtraArgs
    # finally set the full cmdline
    set kernelcmd="$baseExtraConsole $baseCmd $baseRootCmd $baseSelinuxCmd $baseExtraArgs"
}

# grub.cfg now ships this but during upgrades we do not update the COS_GRUB partition, so no new grub.cfg is copied over there
# We need to keep it for upgrades to work.
# TODO: Deprecate in v2.8-v3.0
set kernel=/boot/vmlinuz
set initramfs=/boot/initrd
# set the kernelcmd dynamically
setKernelCmd
