---
# file: roles/minio_client/tasks/main.yml

# https://github.com/minio/minio/issues/4632#issuecomment-313232005
- name: "get last version via github API"
  ansible.builtin.uri:
    url: https://api.github.com/repos/minio/mc/releases/latest
    method: GET
  register: github_client_lastest_version
  check_mode: false
  tags: minio_client

- name: "set minio_client_latest_tag_version"
  ansible.builtin.set_fact:
    minio_client_latest_version_tag: "{{ github_client_lastest_version.json.tag_name.split('.').1  | regex_replace('T', '-') | regex_replace('Z', '') }}"
  tags: minio_client

- name: "parse release tag"
  ansible.builtin.set_fact:
    minio_client_latest_version_year: "{{ minio_client_latest_version_tag.split('-')[0] }}"
    minio_client_latest_version_month: "{{ minio_client_latest_version_tag.split('-')[1] }}"
    minio_client_latest_version_day: "{{ minio_client_latest_version_tag.split('-')[2] }}"
    minio_client_latest_version_hour: "{{ minio_client_latest_version_tag.split('-')[3] }}"
    minio_client_latest_version_minute: "{{ minio_client_latest_version_tag.split('-')[4] }}"
    minio_client_latest_version_second: "{{ minio_client_latest_version_tag.split('-')[5] }}"
  tags: minio_client

- name: "set minio_client_latest_version_deb"
  ansible.builtin.set_fact:
    minio_client_latest_version_deb: "{{ minio_client_latest_version_year }}{{ minio_client_latest_version_month }}{{ minio_client_latest_version_day }}{{ minio_client_latest_version_hour }}{{ minio_client_latest_version_minute }}{{ minio_client_latest_version_second }}.0.0"
  tags: minio_client

- name: "gather the package facts"
  ansible.builtin.package_facts:
  tags: minio_client

- name: "set minio_client_installed_version"
  ansible.builtin.set_fact:
    minio_client_installed_version: "{{ ansible_facts.packages['mcli'][0].version | default(None) }}"
  when:
    - "'mcli' in ansible_facts.packages"
  tags: minio_client

- name: "do we need to update minio?"
  debug:
    msg: "Updating minio client, from {{ minio_client_installed_version }} to {{ minio_client_latest_version_deb }}"
  when:
    - minio_client_installed_version is defined
    - minio_client_installed_version != minio_client_latest_version_deb
  tags: minio_client

- name: "get the minio client checksum"
  ansible.builtin.uri:
    url: "https://dl.min.io/client/mc/release/linux-amd64/mcli_{{ minio_client_latest_version_deb }}_amd64.deb.sha256sum"
    method: GET
    return_content: true
  register: minio_checksum_file
  check_mode: false
  when: >
    minio_client_installed_version is not defined
    or minio_client_installed_version != minio_client_latest_version_deb
  tags: minio_client

- name: "set minio_client_version_deb_checksum"
  ansible.builtin.set_fact:
    minio_client_version_deb_checksum: "{{ minio_checksum_file.content.split(' ')[0] }}"
  when: >
    minio_client_installed_version is not defined
    or minio_client_installed_version != minio_client_latest_version_deb
  tags: minio_client

- name: "get_url https://dl.min.io/client/mc/release/linux-amd64/mcli_{{ minio_client_latest_version_deb }}_amd64.deb"
  ansible.builtin.get_url:
    url: "https://dl.min.io/client/mc/release/linux-amd64/mcli_{{ minio_client_latest_version_deb }}_amd64.deb"
    dest: "/dev/shm/minio_client.deb"
    checksum: "sha256:{{ minio_client_version_deb_checksum }}"
    mode: "0640"
  when: >
    minio_client_installed_version is not defined
    or minio_client_installed_version != minio_client_latest_version_deb
  tags: minio_client

- name: "install minio client"
  ansible.builtin.apt:
    deb: "/dev/shm/minio_client.deb"
  when:
    - not ansible_check_mode
    - minio_client_installed_version is not defined or minio_client_installed_version != minio_client_latest_version_deb
  tags: minio_client

- name: "make sure /dev/shm/minio_client.deb is removed"
  ansible.builtin.file:
    path: "/dev/shm/minio_client.deb"
    state: absent
  tags: minio_client
