---
  ## Do not quote the shell command below without reworking the command
-
  name: "Extracting Java SDK version from SDKMAN: {{ java_version }}"
  ansible.builtin.shell: |
    set -o pipefail
    source {{ service_home_dir }}/.sdkman/bin/sdkman-init.sh
    sdk list java | grep " 8.0.*-zulu" | grep -v "fx-" | sed 's/^.*\(8.0.[0-9]\+-zulu\)[ ]*$/\1/' | head -n 1
  become: true
  become_user: "{{ service_user }}"
  args:
    executable: "/bin/bash"
  register: computed_java_version
  when: "java_version  == 'LATEST'"

-
  name: "Setting Java SDK version from SDKMAN: {{ java_version }}"
  ansible.builtin.shell: "echo {{ java_version }}"
  become: true
  become_user: "{{ service_user }}"
  args:
    executable: "/bin/bash"
  register: selected_java_version
  when: "java_version != 'LATEST'"

-
  name: "Installing Java via SDKMAN: {{ java_version }}"
  become: true
  become_user: "{{ service_user }}"
  ansible.builtin.shell: >-
    . {{ sdkman_dir }}/bin/sdkman-init.sh &&
    sdk install java {{ computed_java_version.stdout }} {{ item.localpath | default('') }}
  args:
    executable: /bin/bash
  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: "Adding Java to /etc/profile.d/java.sh for {{ service_user }}"
  ansible.builtin.lineinfile:
    mode: '0755'
    path: "{{ item.dir }}"
    regexp: "^PATH=\"$PATH:{{ service_home_dir }}/.sdkman/candidates/java/current/bin\""
    insertbefore: EOF
    create: true
    line: 'PATH=$PATH:{{ service_home_dir }}/.sdkman/candidates/java/current/bin'
  with_items:
    - { user: "{{ service_user }}", dir: "/etc/profile.d/java.sh" }

-
  name: "Updating Java Alternatives"
  community.general.alternatives:
    name: "{{ item.name }}"
    path: "{{ sdkman_dir }}/candidates/{{ item.candidate }}/current/bin/{{ item.name }}"
    link: "{{ item.link }}"
  loop: "{{ sdkman_update_alternatives }}"
  become: true
  when: ansible_os_family != 'Darwin'
  tags:
    - sdkman_privilege
