---
- name: grub | Copy Miku wallpaper
  copy:
    src: miku.png
    dest: /boot/grub/background.png
    owner: root
    group: root
    mode: 0644

- name: grub | Ensure fonts directory exists
  file:
    path: /boot/grub/fonts
    state: directory
    owner: root
    group: root
    mode: 0755

# Download a release from https://github.com/source-foundry/Hack
# Decompress the archive
# Run: grub-mkfont -o hack.pf2 -s 24 Hack-Regular.ttf
- name: grub | Copy Hack font
  copy:
    src: hack.pf2
    dest: /boot/grub/fonts/hack.pf2
    owner: root
    group: root
    mode: 0644

- name: grub | Adjust GRUB_TIMEOUT to 10s
  lineinfile:
    path: /etc/default/grub
    regexp: '^GRUB_TIMEOUT='
    line: 'GRUB_TIMEOUT=10'
    owner: root
    group: root
    mode: 0644

- name: grub | Add font and background configurations
  lineinfile:
    path: /etc/default/grub
    line: '{{ item }}'
    search_string: '{{ item | regex_replace("=.*", "") }}'
    owner: root
    group: root
    mode: 0644
  loop:
    - GRUB_DISABLE_OS_PROBER=true
    - GRUB_GFXMODE=1920x1080x32,1280x720x32,auto
    - GRUB_GFXPAYLOAD_LINUX=keep
    - GRUB_BACKGROUND=/boot/grub/background.png
    - GRUB_FONT=/boot/grub/fonts/hack.pf2
  notify: Update grub config

- name: grub | Check whether additional kernel parameters is presented
  lineinfile:
    backup: yes
    path: /etc/default/grub
    regexp: '^GRUB_CMDLINE_LINUX_DEFAULT=".*{{ item | regex_replace("=.*", "=") }}.*'
    state: absent
  check_mode: yes
  register: grub_cmdline_check
  changed_when: no
  loop: '{{ kernel_parameters }}'

- name: apparmor | Add defined kernel parameters to grub boot command-line
  lineinfile:
    backrefs: yes
    path: /etc/default/grub
    regexp: '^(GRUB_CMDLINE_LINUX_DEFAULT=".*)"$'
    line: '\1 {{ item.item }}"'
    owner: root
    group: root
    mode: 0644
  when: item.found == 0
  notify: Update grub config
  loop: '{{ grub_cmdline_check.results }}'
