---
- name: Setup Arch Linux extra repo support
  block:
    - name: Install Artix Arch Linux repo compatibility package
      package:
        name: artix-archlinux-support
        state: present

    - name: Add mirrorlist to pacman.conf
      blockinfile:
        path: /etc/pacman.conf
        marker: "# {mark} ANSIBLE MANAGED ARCH REPOS"
        block: |

          [extra]
          Include = /etc/pacman.d/mirrorlist-arch

        insertbefore: EOF

- name: Update all packages
  package:
    update_cache: true

- name: Upgrade all packages
  package:
    upgrade: true

- name: Install packages
  package:
    name: "{{ item }}"
    state: present
  loop: "{{ packages }}"

- name: Install flatpaks
  package:
    name: "{{ item }}"
    state: present
  loop: "{{ flatpaks }}"

- name: Ensure Users are Configured Correctly
  user:
    name: "{{ item.value.name }}"
    group: "{{ item.value.group }}"
    groups: "{{ item.value.groups }}"
    state: "{{ item.value.state }}"
    create_home: "{{ item.value.create_home }}"
    shell: "{{ item.value.shell }}"
  loop: "{{ init_users | dict2items }}"

- name: Create or ensure presence of custom home directories
  file:
    path: /home/opal/{{ item }}
    state: directory
    mode: '0755'
    owner: opal
    group: opal
  loop: "{{ create_directories }}"

- name: Remove default home directories if present
  file:
    path: /home/opal/{{ item }}
    state: absent
  loop: "{{ remove_directories }}"

- name: Create/Ensure ~/.ssh directories
  file:
    path: "/home/{{ item.value.name }}/.ssh"
    state: directory
    mode: 0700
    owner: "{{ item.value.name }}"
    group: "{{ item.value.group }}"
  loop: "{{ init_users | dict2items }}"
