---
- name: Set up firefox
  hosts: localhost
  vars:
    uid: "{{ lookup('env','USER') }}"
    host_app_url: https://github.com/passff/passff-host/releases/latest/download/install_host_app.sh
    key_path: /usr/share/keyrings/firefox.gpg
    key_url: http://keyserver.ubuntu.com:11371/pks/lookup?op=get&search=0xc77205f7194a3e1abe2df9a4b7b9c16f2667ca5c
  connection: local
  become: false
  tasks:
    - name: Have key allready installed
      stat:
        path: '{{key_path}}'
      register: key
    - name: Get key
      ansible.builtin.command:
        chdir: /tmp
        cmd: wget -O ./keyring.gpg '{{ key_url }}'
        creates: /tmp/keyring.gpg
      when: not key.stat.exists
    - name: Dearmor key
      ansible.builtin.command:
        chdir: /tmp
        cmd: gpg --dearmor -o {{ key_path }} ./keyring.gpg 
        creates: '{{key_path}}'
      become: true
      when: not key.stat.exists
    - name: install repo
      become: true
      ansible.builtin.template:
        src: ../templates/firefox.sources.j2
        dest: /etc/apt/sources.list.d/firefox.sources
      when: not key.stat.exists
    - name: Install apt package
      become: true
      ansible.builtin.apt:
        autoclean: true
        autoremove: true
        install_recommends: true
        pkg: [firefox-mozilla-build]
        state: latest
        update_cache: true
    - name: Get host app
      ansible.builtin.command:
        chdir: /tmp
        cmd: wget {{ host_app_url }}
        creates: /tmp/install_host_app.sh
      when: not key.stat.exists
    - name: Install host app
      ansible.builtin.command:
        cmd: bash /tmp/install_host_app.sh firefox
      when: not key.stat.exists
    - name: Clean up
      ansible.builtin.command:
        chdir: /tmp
        cmd: rm ./install_host_app.sh ./keyring.gpg
        removes: [/tmp/install_host_app.sh, /tmp/keyring.gpg]
      when: not key.stat.exists
