#cloud-config
autoinstall:
  interactive-sections:
    - storage
    - identity
  version: 1
  refresh-installer:
  update: yes
  package_update: true
  package_upgrade: true
  power_state:
    delay: "now"
    mode: reboot
    message: Install successful
    timeout: 30
    condition: true
  mirror:
    mirror: "http://fr.archive.ubuntu.com/ubuntu"
  apt:
    preserve_sources_list: false
    primary:
      - arches: [amd64]
        uri: "http://fr.archive.ubuntu.com/ubuntu"
  ssh:
    allow-pw: true
    install-server: yes
  locale: fr_FR.UTF-8
  keyboard:
    layout: fr
    toggle: null
    variant: ''
  user-data:
    timezone: Europe/Paris
  packages:
    - ubuntu-desktop
    - network-manager
    - cifs-utils
    - dnsutils
    - git
    - hplip
    - htop
    - iotop
    - iftop
    - mc
    - nfs-common
    - nmap
    - ntp
    - screen
    - screenfetch
    - ssh
    - tree
    - vim
    - zip
    - ansible
    - wireguard
    - whois
    - wget  
    - curl
  snaps:
    - name: firefox
      channel: stable
      classic: false
    - name: code
      channel: stable
      classic: true
    - name: thunderbird
      channel: stable
      classic: false
    - name: chromium
      channel: stable
      classic: false
    - name: authd-msentraid
      channel: stable
        
  late-commands:

    # Fix Grub
    - curtin in-target --target=/target -- sed -i 's/GRUB_CMDLINE_LINUX_DEFAULT=""/GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"/' /etc/default/grub
    - curtin in-target --target /target update-grub2
    
    # Update OS
    - curtin in-target --target=/target -- apt update
    - curtin in-target --target=/target -- apt upgrade -y

    # Install Desktop Apps
    - curtin in-target --target=/target -- apt-get install -y filezilla gparted keepassxc vlc build-essential hardinfo
    - curtin in-target --target=/target -- apt-get install -y language-pack-fr language-pack-fr-base language-pack-gnome-fr language-pack-gnome-fr-base
    - curtin in-target --target=/target -- apt-get install -y $(check-language-support -l fr)
    - curtin in-target --target=/target -- locale-gen
    - curtin in-target --target=/target -- update-locale LANG=fr_FR.UTF-8 

    # GLPI-Agent
    - curtin in-target --target=/target -- wget -O /tmp/glpi-agent_1.11-1_all.deb https://github.com/glpi-project/glpi-agent/releases/download/1.11/glpi-agent_1.11-1_all.deb
    - curtin in-target --target=/target -- dpkg -i /tmp/glpi-agent_1.11-1_all.deb || apt-get install -f -y
    - curtin in-target --target=/target -- apt-get install -f -y
    - curtin in-target --target=/target -- mkdir -p /etc/glpi-agent/conf.d/
    - curtin in-target --target=/target -- sh -c "echo 'server = https://glpi.delupay.lan/' > /etc/glpi-agent/conf.d/delupay.cfg"
    - curtin in-target --target=/target -- sh -c "echo 'no-ssl-check = 1' >> /etc/glpi-agent/conf.d/delupay.cfg"

    # Azure Auth
    - curtin in-target --target=/target -- add-apt-repository ppa:ubuntu-enterprise-desktop/authd -y
    - curtin in-target --target=/target -- apt update
    - curtin in-target --target=/target -- apt install -y authd gnome-shell yaru-theme-gnome-shell
    - curtin in-target --target=/target -- mkdir -p /etc/authd/brokers.d/
    - curtin in-target --target=/target -- mkdir -p /etc/delupay/templates/
    - curtin in-target --target=/target -- sh -c "echo 'UsePAM yes' > /etc/ssh/sshd_config.d/authd.conf"
    - curtin in-target --target=/target -- sh -c "echo 'KbdInteractiveAuthentication yes' >> /etc/ssh/sshd_config.d/authd.conf"
    
    ## Sudoers 
    - curtin in-target --target=/target -- sh -c "echo '%administrateur_linux ALL=(ALL:ALL) ALL' > /etc/sudoers.d/delupay"


    - |
      cat <<EOF > /target/etc/authd/brokers.d/msentraid.conf
      # This section is used by authd to identify and communicate with the broker.
      # It should not be edited.
      [authd]
      name = Microsoft Entra ID
      brand_icon = /snap/authd-msentraid/current/broker_icon.png
      dbus_name = com.ubuntu.authd.MSEntraID
      dbus_object = /com/ubuntu/authd/MSEntraID
      EOF

    - |
      cat <<EOF > /target/etc/delupay/templates/broker.conf
      [oidc]
      issuer = https://login.microsoftonline.com/fcd52dc1-e462-4930-aadb-9945320a6ca3/v2.0
      client_id = 51a8ef63-f14b-4025-9be6-8ae47075504b

      [users]
      home_base_dir = /home
      ssh_allowed_suffixes = @delupay.com
      EOF


    # Create a new netplan and enable it
    - |
      cat <<EOF > /target/etc/netplan/01-network-manager-all.yaml
      network:
        version: 2
        renderer: NetworkManager
      EOF
    - curtin in-target --target=/target systemctl enable NetworkManager
    - curtin in-target --target=/target systemctl start NetworkManager
    - curtin in-target --target /target netplan generate
    - curtin in-target --target /target netplan apply
    - curtin in-target --target=/target systemctl disable systemd-networkd-wait-online.service

    # Rename if Dell
    - |
      MANUFACTURER=$(cat /sys/class/dmi/id/sys_vendor)
      if [ "$MANUFACTURER" = "Dell Inc." ]; then
        SERIAL_NUMBER=$(cat /sys/class/dmi/id/product_serial)
        TARGET_HOSTNAME=$(cat /target/etc/hostname)
        echo "XPS-$SERIAL_NUMBER" > /target/etc/hostname
        sed -i "s/$TARGET_HOSTNAME/XPS-$SERIAL_NUMBER/g" /target/etc/hosts
      else
        echo "Non-Dell system detected, keeping user-defined hostname"
      fi

