---
- name: superd | Create services directory
  file:
    path: '{{ xdg_dir.config_home }}/services'
    state: directory
    mode: '755'

- name: superd | Copy service templates
  template:
    src: superd/{{ item }}.j2
    dest: '{{ xdg_dir.config_home }}/services/{{ item }}'
    mode: '644'
  loop:
    - buildkitd.service
    - clipboard.service
    - containerd.service
    - mpd.service
    - notification.service
    - wallpaper.service

- name: superd | Copy service files
  copy:
    src: superd/{{ item }}
    dest: '{{ xdg_dir.config_home }}/services/{{ item }}'
    mode: '644'
  loop:
    - fcitx5.service
    - mpDris2.service
    - pipewire.service
    - pipewire-pulse.service
    - waybar.service
    - wireplumber.service

# These services are simple so they look pretty much the same
- name: superd | Create service files for xdg-desktop-portal backends
  copy:
    content: |
      [Unit]
      Description=XDG Desktop Portal ({{ item }} backend)

      [Service]
      Type=simple
      Restart=on-failure
      ExecStart=/usr/libexec/xdg-desktop-portal-{{ item }}
    dest: '{{ xdg_dir.config_home }}/services/xdg-desktop-portal-{{ item }}.service'
    mode: '644'
  loop:
    - gtk
    - wlr

- name: superd | Configure buildkitd along the way
  when: buildkit.containerd_worker
  tags: buildkit
  block:
    - name: buildkitd | Create configuration directory
      file:
        path: '{{ xdg_dir.config_home }}/buildkit'
        state: directory
        mode: '755'

    - name: buildkitd | Create configuration file
      copy:
        content: |
          [worker.oci]
            enabled = false

          [worker.containerd]
            enabled = true
            rootless = true
        dest: '{{ xdg_dir.config_home }}/buildkit/buildkitd.toml'
        mode: '644'
