- name: Set Zen browser version
  ansible.builtin.set_fact:
    zen_browser_version: "1.8.2b"

- name: Create directory for browser
  ansible.builtin.file:
    path: "/opt/{{ browser_name }}"
    state: directory
    mode: '0755'
  become: true

- name: Download Zen browser tarball
  ansible.builtin.get_url:
    url: "https://github.com/zen-browser/desktop/releases/download/{{ zen_browser_version }}/zen.linux-x86_64.tar.xz"
    dest: "/tmp/{{ browser_name }}.tar.xz"
    mode: '0644'
  become: true

- name: Extract browser tarball
  ansible.builtin.unarchive:
    src: "/tmp/{{ browser_name }}.tar.xz"
    dest: "/opt/{{ browser_name }}"
    remote_src: true
    extra_opts: [--strip-components=1]
  become: true

- name: Create symlink to browser executable
  ansible.builtin.file:
    src: "/opt/{{ browser_name }}/{{ browser_executable }}"
    dest: "/usr/local/bin/{{ browser_name }}"
    state: link
  become: true

- name: Create desktop file
  ansible.builtin.copy:
    content: |
      [Desktop Entry]
      Version=1.0
      Name=Zen Browser
      Exec=/usr/local/bin/{{ browser_name }}
      Icon=/opt/zen/browser/chrome/icons/default/default128.png
      Type=Application
      Categories=Network;WebBrowser;
    dest: "/usr/share/applications/zen.desktop"
    mode: '0644'
  become: true

- name: Update desktop database
  ansible.builtin.command:
    cmd: update-desktop-database
    creates: /usr/share/applications/mimeinfo.cache
  become: true

- name: Make desktop file executable
  ansible.builtin.file:
    dest: "/usr/share/applications/zen.desktop"
    mode: '0755'
  become: true

- name: Clean up downloaded tarball
  ansible.builtin.file:
    path: "/tmp/{{ browser_name }}.tar.xz"
    state: absent
  become: true
