---
- name: Set up fonts
  vars:
    uid: "{{ lookup('env','USER') }}"
    fonts:
      - key: UbuntuMono
        value: https://github.com/ryanoasis/nerd-fonts/releases/download/v3.0.2/UbuntuMono.zip
      - key: EBGaramond
        value: https://www.fontsc.com/font/download/eb-garamond
    font_size: 14
  hosts: localhost
  connection: local
  become: false
  tasks:
    - name: Have font installed allready
      ansible.builtin.stat:
        path: /usr/local/share/fonts/{{ item.key }}
      register: r
      loop: '{{ fonts }}'
    - name: get font
      ansible.builtin.command:
        chdir: /tmp/
        cmd: wget -O {{ item.item.key }}.zip {{ item.item.value }}
      when: not item.stat.exists 
      loop: '{{ r.results }}'
    - name: create font dir
      ansible.builtin.file:
        dest: /tmp/{{ item.item.key }}
        state: directory
      when: not item.stat.exists
      loop: '{{ r.results }}'
    - name: unzip font
      become: true
      ansible.builtin.command:
        chdir: /tmp/{{ item.item.key }}
        cmd: unzip ../{{ item.item.key }}.zip
      when: not item.stat.exists
      loop: '{{ r.results }}'
    - name: install font
      become: true
      ansible.builtin.command:
        chdir: /tmp/
        cmd: mv {{ item.item.key }} /usr/local/share/fonts/
        creates: /usr/local/share/fonts/{{ item.item.key }}
      when: not item.stat.exists
      loop: '{{ r.results }}'
    - name: delete font dir
      ansible.builtin.file:
        dest: /tmp/{{ item.item.key }}
        state: absent
      when: not item.stat.exists
      loop: '{{ r.results }}'
    - name: delete font zip
      ansible.builtin.file:
        dest: /tmp/{{ item.item.key }}.zip
        state: absent
      when: not item.stat.exists
      loop: '{{ r.results }}'
    - name: make foot dir
      ansible.builtin.file:
        dest: /home/{{ uid }}/.config/foot
        state: directory
    - name: install foot config
      ansible.builtin.template:
        src: ../templates/foot.ini.j2
        dest: /home/{{ uid }}/.config/foot/foot.ini
        owner: '{{ uid }}'
        group: '{{ uid }}'
        mode: '0640'
