---
-
  name: Configure SDKMAN
  ansible.builtin.template:
    src: templates/sdkman_config.j2
    dest: '{{ sdkman_dir }}/etc/config'
    owner: '{{ service_user }}'
    group: '{{ service_group }}'
    mode: '0755'

-
  name: Flush SDK caches (before)
  ansible.builtin.shell: >-
    . {{ sdkman_dir }}/bin/sdkman-init.sh && sdk flush {{ item }}
  args:
    executable: /bin/bash
  loop: '{{ sdkman_flush_caches_before }}'
  changed_when: false

-
  name: Update SDKMAN
  ansible.builtin.shell: . {{ sdkman_dir }}/bin/sdkman-init.sh && sdk selfupdate
  args:
    executable: /bin/bash
  register: sdk_selfupdate
  changed_when: sdk_selfupdate.stdout != 'No update available at this time.'
  when: sdkman_update | bool

-
  name: Install SDK candidates/versions
  ansible.builtin.shell: >-
    . {{ sdkman_dir }}/bin/sdkman-init.sh &&
    sdk install {{ item.candidate }} {{ item.version | default('') }} {{ item.localpath | default('') }}
  args:
    executable: /bin/bash
  loop: '{{ sdkman_install_packages }}'
  register: sdk_install
  changed_when: >-
    'is already installed.' not in sdk_install.stdout
  failed_when: >-
    sdk_install.rc != 0 and
    'is already installed.' not in sdk_install.stdout

-
  name: Uninstall SDK candidates/versions
  ansible.builtin.shell: >-
    . {{ sdkman_dir }}/bin/sdkman-init.sh &&
    sdk uninstall {{ item.candidate }} {{ item.version }}
  args:
    executable: /bin/bash
  loop: '{{ sdkman_uninstall_packages }}'
  register: sdk_uninstall
  changed_when: >-
    not item.candidate + ' ' + item.version + ' is not installed.'
    in sdk_uninstall.stdout

-
  name: Get SDK defaults
  ansible.builtin.shell: . {{ sdkman_dir }}/bin/sdkman-init.sh && sdk current {{ item }}
  args:
    executable: /bin/bash
  register: get_sdk_defaults
  changed_when: false
  loop: >-
    {{ sdkman_install_packages | map(attribute="candidate") | unique | list }}

-
  name: Set SDK defaults
  ansible.builtin.shell: >-
    . {{ sdkman_dir }}/bin/sdkman-init.sh &&
    sdk default {{ item.key }} {{ item.value }}
  args:
    executable: /bin/bash
  loop: '{{ sdkman_defaults | dict2items }}'
  changed_when: >-
    not item.value in
    (get_sdk_defaults.results |
     selectattr('item', 'equalto', item.key) |
     first).stdout

-
  name: Flush SDK caches (after)
  ansible.builtin.shell: . {{ sdkman_dir }}/bin/sdkman-init.sh && sdk flush {{ item }}
  args:
    executable: /bin/bash
  loop: '{{ sdkman_flush_caches_after }}'
  changed_when: false
