---
# https://arvanta.net/alpine/libudev-zero/
- name: devd | Specific tasks for non-udev device manager
  when: device_manager != 'udev'
  block:
    - name: devd | Install libudev-zero
      community.general.apk:
        name: libudev-zero
        state: present

    - name: devd | Enable hwdrivers service on runlevel sysinit
      service:
        name: hwdrivers
        runlevel: sysinit
        enabled: true

- name: devd | udev doesn't need hwdrivers service
  service:
    name: hwdrivers
    runlevel: sysinit
    enabled: false
  when: device_manager == 'udev'

# https://github.com/illiliti/libudev-zero/blob/master/contrib/mdev.conf
# mdevd-openrc on Alpine already comes with -O4 flag by default
- name: devd | Specific tasks for busybox's mdev
  when: device_manager == 'mdev'
  block:
    - name: mdev | Install libudev-zero-helper
      community.general.apk:
        name: libudev-zero-helper
        state: present

    - name: mdev | Enable hotplugging for DRM and INPUT uevents
      lineinfile:
        path: /etc/mdev.conf
        line: '{{ item }}'
        state: present
        mode: '644'
        owner: root
        group: root
      loop:
        - 'SUBSYSTEM=drm;.*   root:video 660 */usr/libexec/libudev-zero-helper'
        - 'SUBSYSTEM=input;.* root:input 660 */usr/libexec/libudev-zero-helper'

- name: devd | Run setup-devd script
  command:
    cmd: '/sbin/setup-devd -C {{ device_manager }}'
    creates: '/etc/runlevels/sysinit/{{ device_manager }}'
  failed_when: false
