---
- name: install packages
  apt:
    name:
      - live-build
    state: latest # noqa package-latest

- name: prepare live-build directory
  file:
    path: "{{ build_dir }}"
    state: directory
    mode: 0755

- name: copy build script
  template:
    src: livebuilder.sh
    dest: /usr/local/sbin/
    mode: 0755

- name: provide service and timer for livebuilder
  copy:
    src: "{{ item }}"
    dest: "/etc/systemd/system/{{ item }}"
    mode: 0644
  with_items:
    - livebuilder.service
    - livebuilder.timer
  notify: "enable livebuilder.timer"

- name: copy live-build configuration
  copy:
    src: "{{ item }}"
    dest: "{{ build_dir }}"
    local_follow: false
    mode: preserve
  loop: "{{ build_images }}"

- name: prepare debian live nfs boot entry title
  lineinfile:
    path: /etc/di-netboot-assistant/ipxemenu.HEAD
    insertafter: '-- Customized Boot Entries --'
    line: "item {{ item }} Debian GNU/Linux {{ item }} NFS"
  loop:
    "{{ build_images }}"
  notify: rebuild di-netboot menu

- name: prepare debian live boot loader ipxe
  blockinfile:
    path: /etc/di-netboot-assistant/ipxemenu.HEAD
    marker: "# {mark} ANSIBLE MANAGED BLOCK {{ item }}"
    block: |
      :{{ item }}
          echo Booting Debian GNU/Linux EDU LIVE NFS
          kernel ${210:string}d-i/n-live/{{ item }}/live/vmlinuz \
          initrd=initrd.img {{ boot_params|join(' ') }} \
          nfsroot={{ ansible_default_ipv4.address }}:/var/lib/tftpboot/d-i/n-live/{{ item }}/
          initrd ${210:string}d-i/n-live/{{ item }}/live/initrd.img
          boot
  loop: "{{ build_images }}"
  notify: rebuild di-netboot menu

- name: prepare debian live boot loader grub
  blockinfile:
    path: /etc/di-netboot-assistant/grub.cfg.HEAD
    insertbefore: "^menuentry 'Boot from local disk..'"
    marker: "# {mark} ANSIBLE MANAGED BLOCK {{ item }}"
    block: |
      menuentry 'Debian GNU/Linux Live {{ item }} NFS' {
          linux (http,{{ ansible_default_ipv4.address }})/d-i/n-live/{{ item }}/live/vmlinuz \
          {{ boot_params|join(' ') }} \
          nfsroot={{ ansible_default_ipv4.address }}:/var/lib/tftpboot/d-i/n-live/{{ item }}/
          initrd (http,{{ ansible_default_ipv4.address }})/d-i/n-live/{{ item }}/live/initrd.img
      }
  loop: "{{ build_images }}"
  notify: rebuild di-netboot menu
