#cloud-config
hostname: rpios-like-arm64
manage_etc_hosts: true

write_files:
  - path: /usr/local/sbin/setup-raspi-repo-and-sys-mods-arm64.sh
    permissions: "0755"
    content: |
      #!/usr/bin/env bash
      set -euo pipefail

      arch="$(dpkg --print-architecture)"
      if [[ "$arch" != "arm64" ]]; then
        echo "arm64-only; detected '$arch' -> skipping" >&2
        exit 0
      fi

      export DEBIAN_FRONTEND=noninteractive

      apt-get update
      apt-get install -y --no-install-recommends ca-certificates curl gnupg util-linux

      install -d -m 0755 /usr/share/keyrings /etc/apt/preferences.d

      if [[ ! -s /usr/share/keyrings/raspberrypi-archive-keyring.gpg ]]; then
        curl -fsSL https://archive.raspberrypi.org/debian/raspberrypi.gpg.key \
          | gpg --dearmor -o /usr/share/keyrings/raspberrypi-archive-keyring.gpg
        chmod 0644 /usr/share/keyrings/raspberrypi-archive-keyring.gpg
      fi

      #cat >/etc/apt/preferences.d/raspi.pref <<'EOF'
      #Package: *
      #Pin: origin "archive.raspberrypi.org"
      #Pin-Priority: 100
      #EOF

      if [[ ! -f /etc/apt/sources.list.d/raspi.sources ]]; then
        cat >/etc/apt/sources.list.d/raspi.sources <<'EOF'
      Types: deb
      URIs: https://archive.raspberrypi.org/debian/
      Suites: trixie
      Components: main
      Signed-By: /usr/share/keyrings/raspberrypi-archive-keyring.gpg
      EOF
      fi

      apt-get update

      # Add custom /etc/rpi-issue for "rpios-like"
      if [[ ! -s /etc/rpi-issue ]]; then
        {
          echo "Raspberry Pi reference trixie-latest $(date -R)"
          echo "Emulated using debian-13-genericcloud"
        } >/etc/rpi-issue
      fi

      echo "---- /etc/rpi-issue ----"
      cat /etc/rpi-issue || true
      echo "------------------------"

runcmd:
  - [ bash, -lc, "/usr/local/sbin/setup-raspi-repo-and-sys-mods-arm64.sh" ]
