---
- name: Ensure custom facts directory exists
  file:
    path: /etc/ansible/facts.d
    mode: 0755
    state: directory

- name: "Ensure {{ hexyl_src_directory }} exists"
  file:
    path: "{{ hexyl_src_directory }}"
    mode: 0755
    state: directory

- name: "Detect the latest {{ app_name }} version"
  uri:
    url: https://api.github.com/repos/sharkdp/hexyl/releases/latest
  register: hexyl_latest_release_tag

- name: "Determine whether or not the latest version of {{ app_name }} is already installed"
  set_fact:
    install_hexyl: "{{ (ansible_local.hexyl is not defined) or \
      ((ansible_local.hexyl is defined) and \
      (ansible_local['hexyl']['settings']['version'] != hexyl_latest_release_tag.json.tag_name | replace('v',''))) }}"

- name: "Ensure {{ app_name }} is installed"
  unarchive:
    src: "https://github.com/sharkdp/hexyl/releases/download/{{ hexyl_latest_release_tag.json.tag_name }}/\
      hexyl-{{ hexyl_latest_release_tag.json.tag_name }}-x86_64-unknown-linux-musl.tar.gz"
    dest: /usr/local/bin
    exclude:
      - LICENSE-APACHE
      - LICENSE-MIT
      - README.md
    extra_opts:
      - --strip-components=1
    remote_src: true
  when: install_hexyl

- name: "Save meta information about the version of {{ app_name }} that was installed"
  community.general.ini_file:
    path: /etc/ansible/facts.d/hexyl.fact
    mode: 0644
    section: settings
    option: version
    value: "{{ hexyl_latest_release_tag.json.tag_name | replace('v','') }}"
    backup: true
    no_extra_spaces: true
  when: install_hexyl
