---
- name: Check whether SDDM is the active runit display manager
  tags: [packages, services, kde, display-manager]
  ansible.builtin.stat:
    path: /var/service/sddm
  register: profile_desktop_kde_sddm_service
  when: (desktop_environment | default('minimal')) == 'kde'

- name: Copy KDE MIME application defaults
  tags: [dotfiles, dotfiles:desktop, kde]
  ansible.builtin.copy:
    src: "{{ playbook_dir }}/../dotfiles/desktop/.config/mimeapps.kde.list"
    dest: "{{ user_home }}/.config/mimeapps.list"
    owner: "{{ username }}"
    group: "{{ user_group }}"
    mode: "0644"
  when: (desktop_environment | default('minimal')) == 'kde'

- name: Remove minimal desktop user configuration managed by Ansible
  tags: [dotfiles, dotfiles:desktop, kde]
  ansible.builtin.file:
    path: "{{ user_home }}/{{ item }}"
    state: absent
  loop: "{{ desktop_minimal_managed_paths | default([]) }}"
  loop_control:
    label: "{{ item }}"
  when:
    - (desktop_environment | default('minimal')) == 'kde'
    - profile_desktop_kde_sddm_service.stat.exists

- name: Remove minimal desktop portal preferences
  tags: [packages, services, kde, portal]
  ansible.builtin.file:
    path: "{{ item }}"
    state: absent
  loop:
    - /etc/xdg/xdg-desktop-portal/hyprland-portals.conf
    - /etc/xdg/xdg-desktop-portal/sway-portals.conf
  when:
    - (desktop_environment | default('minimal')) == 'kde'
    - profile_desktop_kde_sddm_service.stat.exists

- name: Remove inactive emptty and minimal session launchers
  tags: [packages, services, kde, display-manager]
  ansible.builtin.file:
    path: "{{ item }}"
    state: absent
  loop:
    - /etc/emptty
    - /usr/local/bin/start-hyprland-session
    - /usr/local/bin/start-sway
  when:
    - (desktop_environment | default('minimal')) == 'kde'
    - profile_desktop_kde_sddm_service.stat.exists
