- name: Enable copr
  community.general.copr:
    state: enabled
    name: szydell/system76

- name: Add repository
  ansible.builtin.yum_repository:
    name: "{{ item.name }}"
    description: "Ansible repositories"
    baseurl: "{{ item.baseurl }}"
    async: yes
    gpgkey: "{{ item.gpgkey }}"
  with_items:  "{{ required_repository }}"  
  when: required_repository is defined   

- name: Update packages
  yum: 
    name: '*'
    state: latest
    update_cache: yes
    update_only: yes
  retries: 3
  delay: 10  

- name: Remove packages not needed anymore
  yum:
    autoremove: yes   
  retries: 3
  delay: 10

- name: Install required packages
  yum: 
    state: latest 
    update_cache: yes
    pkg: "{{ item }}"
  with_items:  "{{ required_packages }}" 
  retries: 3
  delay: 10    
  when: required_packages is defined

- name: configure flatpak
  community.general.flatpak_remote:
    name: flathub
    state: present
    flatpakrepo_url: https://dl.flathub.org/repo/flathub.flatpakrepo

- name: update flatpaks
  shell:
    cmd: flatpak update --noninteractive    

- name: install flatpak
  become_user: user
  community.general.flatpak:
    name: "{{ item }}"
  with_items:  "{{ required_flatpak_packages }}"     
  when: required_flatpak_packages is defined

- name: Manual install packages
  ansible.builtin.yum:
    name: "{{ item }}"
    state: present
    disable_gpg_check: yes
  with_items:  "{{ required_manual_packages }}"    
  when: required_manual_packages is defined

- name: Manual install files
  ansible.builtin.get_url:
    url: https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/posh-linux-amd64
    dest: /usr/local/bin/oh-my-posh
    mode: 'a+x'

- name: set gui
  shell:
    cmd: systemctl set-default graphical.target 

- name: ensure fonts directory
  become_user: user
  file:
    path: "~/.fonts"
    state: directory

- name: FiraCode exists
  become_user: user
  shell: "ls ~/.fonts/*{{ item.name }}*"
  register: FiraCode_exists
  ignore_errors: yes
  with_items:  "{{ fonts }}"   

- name: Download FiraCode
  when: FiraCode_exists is failed
  become_user: user
  ansible.builtin.unarchive:
    src: "{{ item.url }}" 
    dest: "~/.fonts/"
    remote_src: yes
  with_items:  "{{ fonts }}"     

- name: Start and enable services
  ansible.builtin.systemd:
    state: started
    name: "{{ item }}"  
    enabled: yes 
  with_items:  "{{ services_to_enable }}"     

- name: Mask power profiles
  ansible.builtin.systemd:
    masked: yes
    name: power-profiles-daemon  

- name: Add user to groups
  ansible.builtin.user:
    name: user
    groups: "{{ item }}"  
    append: yes    
  with_items:  "{{ groups_to_add }}"       

- name: Update sleep mode
  shell: 'grubby --args="mem_sleep_default=s2idle" --update-kernel=ALL'

#- name: Set bluetooth script
#  become_user: user
#  copy:
#    src: "files/bluetooth"
#    dest: "/lib/systemd/system-sleep/bluetooth-suspend"
#    owner: "root"
#    force: yes  
#    mode: 'a+x'

- ansible.posix.firewalld:
    zone: public
    service: "{{ item }}" 
    permanent: yes
    state: disabled
  with_items:  "{{ firewall_to_disable }}"       
