#!/bin/sh
export DEBIAN_FRONTEND="noninteractive"
for i in $(echo "${1}" | tr ',' ' '); do
case "$i" in
      default)
        echo "===> No kernel changes needed"
        exit 0
      ;;

      cgroup1)
        echo "===> CGroup1"

        # shellcheck disable=SC2016
        echo 'GRUB_CMDLINE_LINUX="${GRUB_CMDLINE_LINUX} systemd.unified_cgroup_hierarchy=0"' > /etc/default/grub.d/99-cgroup.cfg
        update-grub
      ;;

      cgroup2)
        echo "===> CGroup2"

        # shellcheck disable=SC2016
        echo 'GRUB_CMDLINE_LINUX="${GRUB_CMDLINE_LINUX} systemd.unified_cgroup_hierarchy=1"' > /etc/default/grub.d/99-cgroup.cfg
        update-grub
      ;;

      swapaccount)
        echo "===> CGroup swap accounting"

        # shellcheck disable=SC2016
        echo 'GRUB_CMDLINE_LINUX="${GRUB_CMDLINE_LINUX} swapaccount=1"' > /etc/default/grub.d/99-cgroup-swap.cfg
        update-grub
      ;;

      iommu)
        echo "===> Enabling iommu"

        # shellcheck disable=SC2016
        echo 'GRUB_CMDLINE_LINUX="${GRUB_CMDLINE_LINUX} iommu=pt intel_iommu=on amd_iommu=on"' > /etc/default/grub.d/99-iommu.cfg
        update-grub
      ;;

      isolcpus)
        echo "===> Isolate CPUs from the kernel scheduler"

        # shellcheck disable=SC2016
        echo 'GRUB_CMDLINE_LINUX="${GRUB_CMDLINE_LINUX} isolcpus=6,7,8,9,10,11,18,19,20,21,22,23"' > /etc/default/grub.d/99-isolcpus.cfg
        update-grub
      ;;

      nvidia)
        echo "===> Installing the NVIDIA driver"

        echo "MODULES=dep" > /etc/initramfs-tools/conf.d/modules.conf
        apt-get update
        apt-get dist-upgrade --yes
        apt-get install --no-install-recommends --yes linux-generic
        apt-get remove --purge --yes linux.*hwe.* --yes
        apt-get install --no-install-recommends --yes nvidia-utils-470 linux-modules-nvidia-470-generic libnvidia-compute-470

        echo "blacklist nouveau" > /etc/modprobe.d/blacklist-nouveau.conf
        echo "options nouveau modeset=0" >> /etc/modprobe.d/blacklist-nouveau.conf
        update-initramfs -u
      ;;

      nvidia-mig)
        echo "===> Installing the NVIDIA MIG driver"

        echo "MODULES=dep" > /etc/initramfs-tools/conf.d/modules.conf
        apt-get update
        apt-get dist-upgrade --yes
        apt-get install --no-install-recommends --yes linux-generic
        apt-get remove --purge --yes linux.*hwe.* --yes
        apt-get install --no-install-recommends --yes nvidia-utils-460 nvidia-headless-460 libnvidia-compute-460

        echo "blacklist nouveau" > /etc/modprobe.d/blacklist-nouveau.conf
        echo "options nouveau modeset=0" >> /etc/modprobe.d/blacklist-nouveau.conf
        update-initramfs -u
      ;;

      amd-vgpu)
        echo "===> Installing the AMD vGPU driver"

        echo "MODULES=dep" > /etc/initramfs-tools/conf.d/modules.conf
        apt-get update
        apt-get dist-upgrade --yes
        apt-get install --no-install-recommends --yes build-essential
        git clone https://github.com/kasperlewau/MxGPU-Virtualization
        (cd MxGPU-Virtualization && ./gim.sh)
        depmod -a

        echo "blacklist amdgpu" > /etc/modprobe.d/blacklist-amdgpu.conf
        update-initramfs -u
      ;;

      nvidia-vgpu)
        echo "===> Installing the NVIDIA vGPU driver"

        echo "MODULES=dep" > /etc/initramfs-tools/conf.d/modules.conf
        apt-get update
        apt-get dist-upgrade --yes
        apt-get install --no-install-recommends --yes build-essential
        curl http://lab.linuxcontainers.org/nvidia/v14.0/nvidia-host.deb -o /root/nvidia-host.deb
        apt-get install --yes /root/nvidia-host.deb

        echo "blacklist nouveau" > /etc/modprobe.d/blacklist-nouveau.conf
        echo "options nouveau modeset=0" >> /etc/modprobe.d/blacklist-nouveau.conf
        update-initramfs -u
      ;;

      ubuntu)
        echo "===> Ensuring default kernel is up to date"

        echo "MODULES=dep" > /etc/initramfs-tools/conf.d/modules.conf
        apt-get update
        apt-get dist-upgrade --yes
      ;;

      ubuntu-proposed)
        echo "===> Installing the current proposed kernel"

        # shellcheck disable=SC1091
        . /etc/os-release
        echo "deb http://us.archive.ubuntu.com/ubuntu ${UBUNTU_CODENAME}-proposed main restricted universe multiverse" > /etc/apt/sources.list.d/proposed.list
        apt-get update
        apt-get install --no-install-recommends --yes linux-generic
        rm /etc/apt/sources.list.d/proposed.list
        apt-get update
      ;;

      ubuntu-bootstrap)
        echo "===> Installing the current bootstrap kernel"

        echo "MODULES=dep" > /etc/initramfs-tools/conf.d/modules.conf
        apt-get update
        apt-get install --no-install-recommends --yes software-properties-common
        apt-add-repository ppa:canonical-kernel-team/bootstrap --yes
        apt-get dist-upgrade --yes
        apt-get install --no-install-recommends --yes linux-generic-wip
      ;;

      ubuntu-unstable)
        echo "===> Installing the current unstable kernel"

        echo "MODULES=dep" > /etc/initramfs-tools/conf.d/modules.conf
        apt-get update
        apt-get install --no-install-recommends --yes software-properties-common
        apt-add-repository ppa:canonical-kernel-team/unstable --yes
        apt-get dist-upgrade --yes
        apt-get install --no-install-recommends --yes linux-generic-wip
      ;;

      daily)
        echo "===> Installing a mainline daily build"

        wget -e robots=off -r --no-parent -A '*all*.deb' -R '*lpae*' -R '*lowlatency*' https://kernel.ubuntu.com/~kernel-ppa/mainline/daily/current/
        wget -e robots=off -r --no-parent -A '*amd64*.deb' -R '*lpae*' -R '*lowlatency*' https://kernel.ubuntu.com/~kernel-ppa/mainline/daily/current/
        echo "MODULES=dep" > /etc/initramfs-tools/conf.d/modules.conf
        apt-get update
        apt-get install ./kernel.ubuntu.com/~kernel-ppa/mainline/daily/current/amd64/*.deb --yes
        apt-get dist-upgrade --yes
      ;;

      zabbly)
        echo "===> Installing the current zabbly kernel"

        curl -sL https://pkgs.zabbly.com/get/kernel-stable | sh
      ;;

      zabbly-zfs)
        echo "===> Installing the current zabbly kernel"

        curl -sL https://pkgs.zabbly.com/get/zfs-stable | sh
      ;;

      *)
        echo "Unsupported kernel requested: ${1}"
        exit 1
      ;;
    esac
done

reboot
