---
- name: Install user bashrc
  become: true
  ansible.builtin.copy:
    src: bashrc.sh
    dest: ~{{ item }}/.bashrc_ansible
    owner: "{{ ansible_user }}"
    group: "{{ item }}"
    mode: 0644
  loop: "{{ _local_human_users }}"

- name: Configure user bashrc loading
  become: true
  ansible.builtin.lineinfile:
    path: ~{{ item }}/.bashrc
    line: source ~/.bashrc_ansible
    state: present
  loop: "{{ _local_human_users }}"

- name: Configure bash completions
  become: true
  ansible.builtin.blockinfile:
    path: ~{{ item }}/.config/bash_completions
    create: true
    block: >-
      function _gg_completion() {
          local cur=${COMP_WORDS[COMP_CWORD]};
          COMPREPLY=( $(compgen -W "$(command ls $PROJECT_DIR)" -- $cur) );
      }

      complete -F _gg_completion gg
    owner: "{{ ansible_user }}"
    group: "{{ item }}"
    mode: 0664
  loop: "{{ _local_human_users }}"

- name: Enforce ownership of the SSH keys
  become: true
  ansible.builtin.file:
    path: ~{{ item.0 }}/.ssh/id_ed25519{{ item.1 }}
    state: file
    owner: "{{ item.0 }}"
    group: "{{ item.0 }}"
  loop: "{{ _local_human_users | product(['', '.pub']) }}"

- name: Configure dconf setting
  become: true
  block:
    - name: Create dconf config directories
      ansible.builtin.file:
        path: "{{ item }}"
        state: directory
        owner: root
        group: "{{ ansible_user }}"
        mode: 0755
      loop:
        - /etc/dconf/profile
        - /etc/dconf/db/gdm.d

    - name: Create global dconf config
      ansible.builtin.copy:
        src: gdm-system
        dest: /etc/dconf/profile/gdm
        owner: root
        group: "{{ ansible_user }}"
        mode: 0644
      notify:
        - dconf-update

    - name: Create user dconf config
      ansible.builtin.copy:
        src: gdm-user
        dest: /etc/dconf/profile/user
        owner: root
        group: "{{ ansible_user }}"
        mode: 0644
      notify:
        - dconf-update

    - name: Disable user list
      ansible.builtin.copy:
        src: 00-disable-user-list
        dest: /etc/dconf/db/gdm.d/00-disable-user-list
        owner: root
        group: "{{ ansible_user }}"
        mode: 0644
      notify:
        - dconf-update

    - name: Enable fractional scaling
      ansible.builtin.copy:
        src: 00-enable-fractional-scaling
        dest: /etc/dconf/db/local.d/00-enable-fractional-scaling
        owner: root
        group: "{{ ansible_user }}"
        mode: 0644
      notify:
        - dconf-update

- name: Install themes
  become: true
  block:
    - name: Create local themes directory
      ansible.builtin.file:
        path: ~{{ item }}/.themes
        state: directory
        owner: "{{ item }}"
        group: "{{ item }}"
        mode: 0750
      loop: "{{ _local_human_users }}"

    - name: Unarchive LightningBug into local directory
      ansible.builtin.unarchive:
        src: lightningbug-dark.tar.gz
        dest: ~{{ item }}/.themes
        owner: "{{ item }}"
        group: "{{ item }}"
      loop: "{{ _local_human_users }}"

- name: Install wallpaper
  become: true
  ansible.builtin.copy:
    src: wallpaper-{{ inventory_hostname }}.jpg
    dest: ~{{ item }}/Pictures/wallpaper.jpg
    owner: "{{ item }}"
    group: "{{ item }}"
  loop: "{{ _local_human_users }}"
