#cloud-config

hostname: debian1

users:
  - default
  - name: vsoc-01
    shell: /bin/bash
    sudo: ALL=(ALL) NOPASSWD:ALL
    groups: users, video, cdrom, floppy, dialout, audio
    lock_passwd: false
    passwd: "$6$rounds=4096$OoOhTi2nlaaRIPU4$53NOQih8JJdmyNg3hvEo41jtxpz94QtPG9IXbBmsyFgU6Fl0NLT8WlRv3kXP86jgp.1D3rXbKtG88YZ9V7dvF0"

packages:
  - git
  - lvm2

growpart:
  mode: auto
  devices:
    - /dev/vda1
    - /dev/sda1

write_files:
  - path: /etc/apt/sources.list.d/debian-snapshot.list
    permissions: '0644'
    owner: root:root
    content: |
      # Frozen snapshot: pins the kernel and NVIDIA versions installed by
      # after_install_1.sh; bump the timestamp and those versions together.
      deb [check-valid-until=no] https://snapshot.debian.org/archive/debian/20260315T022902Z/ trixie-backports main contrib non-free non-free-firmware
  - path: /root/after_install_1.sh
    permissions: '0755'
    owner: root:root
    content: |
      #!/bin/sh

      apt-get update

      # Install necessary packages
      apt-get install -y debconf-utils
      apt-get install -y ca-certificates
      apt-get install -y wget
      apt-get install -y git
      apt-get install -y python3
      apt-get install -y p7zip-full unzip
      apt-get install -y iptables ebtables
      apt-get install -y curl
      apt-get install -y lsb-release
      apt-get install -y gpg

      # Adjust user groups
      adduser vsoc-01 kvm
      adduser vsoc-01 render
      adduser vsoc-01 video

      # Detect distribution
      DEBIAN_DISTRIBUTION="$(lsb_release -c -s)"
      DEBIAN_ARCH="$(dpkg --print-architecture)"

      apt -o Apt::Get::Assume-Yes=true -o APT::Color=0 -o DPkgPM::Progress-Fancy=0 update

      # Install kernel
      DEBIAN_DISTRIBUTION="$(lsb_release -c -s)"
      KERNEL_ABI="6.18.15+deb13-arm64"
      KERNEL_DEB_VERSION="6.18.15-1~bpo13+1"
      NVIDIA_DEB_VERSION="550.163.01-4~bpo13+1"
      apt-get -o Acquire::Retries=5 update
      apt-get install -y -o Acquire::Retries=5 \
        "linux-image-${KERNEL_ABI}=${KERNEL_DEB_VERSION}" \
        "linux-headers-${KERNEL_ABI}=${KERNEL_DEB_VERSION}" || exit 1

      # Install nVidia or AMD GPU driver
      nvidia_gpu=$(lspci | grep -i nvidia)
      amd_gpu=$(lspci | grep VGA | grep AMD)
      if [ x"$amd_gpu" != x"" ]; then
        # # Install amd firmware
        # apt-get install -y firmware-amd-graphics
        # sed -i 's/GRUB_CMDLINE_LINUX_DEFAULT=\"\(.*\)\"/GRUB_CMDLINE_LINUX_DEFAULT=\"\1 amdgpu.runpm=0 amdgpu.dc=0\"/' /etc/default/grub
        # dpkg-reconfigure -fnoninteractive "grub-efi-${DEBIAN_ARCH}"
        echo "AMD GPU detected — skipping GPU driver installation."
      elif [ x"$nvidia_gpu" != x"" ]; then
        # Install nvidia driver, pinned to the version validated against the
        # pinned kernel.
        DEBIAN_FRONTEND=noninteractive apt-get install -y -q -o Acquire::Retries=5 \
          -t "${DEBIAN_DISTRIBUTION}-backports" \
          "nvidia-open-kernel-dkms=${NVIDIA_DEB_VERSION}" \
          "nvidia-driver=${NVIDIA_DEB_VERSION}" || exit 1
        DEBIAN_FRONTEND=noninteractive apt-get install -y -q firmware-misc-nonfree
        apt-mark hold nvidia-open-kernel-dkms nvidia-driver
      fi
      rm -f /etc/apt/sources.list.d/debian-snapshot.list
      # End of Install kernel

      # Install android cuttlefish packages
      curl -fsSL --retry 7 --retry-all-errors https://us-apt.pkg.dev/doc/repo-signing-key.gpg -o /etc/apt/trusted.gpg.d/artifact-registry.asc
      chmod a+r /etc/apt/trusted.gpg.d/artifact-registry.asc
      echo "deb https://us-apt.pkg.dev/projects/android-cuttlefish-artifacts android-cuttlefish main" | tee -a /etc/apt/sources.list.d/artifact-registry.list
      apt-get update
      apt-get install -y cuttlefish-base cuttlefish-user cuttlefish-orchestration -t android-cuttlefish
      adduser vsoc-01 cvdnetwork

      # Install gigabyte package
      apt -o Apt::Get::Assume-Yes=true -o APT::Color=0 -o DPkgPM::Progress-Fancy=0 -o Acquire::Retries=5 install cuttlefish-integration-gigabyte-arm64

      # Extra tools
      cd /root
      git clone https://github.com/matthuisman/gdrivedl.git
      cd -

      # Use iptables-legacy
      update-alternatives --set iptables /usr/sbin/iptables-legacy

      # Install network manager
      apt-get install -y network-manager

      # Network-manager workaround
      rm -f '/etc/NetworkManager/system-connections/Wired connection 1'

      # Install Docker container
      # Add Docker's official GPG key:
      apt-get update
      curl -fsSL --retry 7 --retry-all-errors https://download.docker.com/linux/debian/gpg -o /etc/apt/trusted.gpg.d/docker.asc
      chmod a+r /etc/apt/trusted.gpg.d/docker.asc

      # Add the repository to Apt sources:
      echo "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/debian $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
      apt-get update
      DEBIAN_FRONTEND=noninteractive apt-get install -y -q docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
      usermod -aG docker vsoc-01

      # Inastall nvidia-container-toolkit
      curl -fsSL --retry 7 --retry-all-errors https://nvidia.github.io/libnvidia-container/gpgkey | gpg --dearmor -o /etc/apt/trusted.gpg.d/nvidia-container-toolkit-keyring.gpg && curl -s -L --retry 7 --retry-all-errors https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
      apt-get update
      DEBIAN_FRONTEND=noninteractive apt-get install -y  -q --force-yes nvidia-container-toolkit

      # restart NTP server
      systemctl restart ntpsec
  - path: /etc/ssh/sshd_config.d/99-cloud-init-cuttlefish-gigamp.conf
    permissions: '0644'
    content: |
      PasswordAuthentication yes

runcmd:
  - /root/after_install_1.sh >> /var/log/custom-startup.log
  - echo "VM successful" >> /var/log/custom-startup.log
