---
- name: flatpak | Create policy override directory
  file:
    path: '{{ xdg_dir.data_home }}/flatpak/overrides'
    state: directory
    mode: '755'

- name: flatpak | Copy global override policy
  template:
    src: flatpak/overrides/global.j2
    dest: '{{ xdg_dir.data_home }}/flatpak/overrides/global'
    mode: '644'

- name: flatpak | Copy override policies for common apps
  copy:
    src: 'flatpak/overrides/{{ item }}'
    dest: '{{ xdg_dir.data_home }}/flatpak/overrides/{{ item }}'
    mode: '644'
  loop: '{{ flatpak_applications }}'

- name: flatpak | Copy wrapper binaries
  copy:
    src: 'flatpak/bin/{{ item }}'
    dest: '{{ xdg_dir.bin_home }}/{{ item }}'
    mode: '755'
  loop:
    - ungoogled-chromium

- name: flatpak | Create user desktop directory
  file:
    path: '{{ xdg_dir.data_home }}/applications'
    state: directory
    mode: '755'

- name: flatpak | Override application desktop files
  template:
    src: 'flatpak/applications/{{ item }}.j2'
    dest: '{{ xdg_dir.data_home }}/applications/{{ item }}'
    mode: '644'
  loop:
    - com.github.Eloston.UngoogledChromium.desktop

- name: flatpak | Add remotes
  community.general.flatpak_remote:
    name: '{{ item.key }}'
    flatpakrepo_url: '{{ item.value }}'
    method: user
    state: present
  loop: '{{ flatpak_remotes | dict2items }}'

- name: flatpak | Install applications
  community.general.flatpak:
    name: '{{ flatpak_applications }}'
    method: user
    state: present
  tags: flatpak-install
