---
-
  name: "Managing progress for {{ ansible_role_name }}"
  when: count_progress | default(false)
  run_once: true
  block:
    -
      name: "Incrementing global progress step counter for {{ ansible_role_name }}"
      ansible.builtin.set_fact:
        global_current_progress_step: "{{ global_current_progress_step | default(0) | int + 1 }}"

    -
      name: "Including progress reporting task for {{ ansible_role_name }}"
      ansible.builtin.include_role:
        name: startcloud.startcloud_roles.progress
      vars:
        _progress_role_is_setup_run: false
        current_progress_step: "{{ global_current_progress_step | default(0) }}"
        progress_description: "{{ progress_role_description | default('Processing ' + ansible_role_name) }}"

-
  name: "Block to Allow Loading of Variables without running task"
  when: run_tasks
  block:
  ## Do not quote the shell command below without reworking the command

    -
      name: "Set SDKMAN user/group vars"
      ansible.builtin.set_fact:
        sdk_service_user: '{{ service_user }}'
        sdk_service_group: '{{ service_group }}'
    
    -
      name: "Setting SDKMAN_DIR environment variable"
      ansible.builtin.set_fact:
        sdkman_dir: '{{ service_home_dir }}/.sdkman'
    
    -
      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: "{{ sdk_service_user }}"
      args:
        executable: "/bin/bash"
      register: computed_java_version
      changed_when: false
      when: "java_version  == 'LATEST'"
    
    -
      name: "Setting Java SDK version from SDKMAN: {{ java_version }}"
      ansible.builtin.shell: "echo {{ java_version }}"
      become_user: "{{ sdk_service_user }}"
      changed_when: false
      args:
        executable: "/bin/bash"
      register: selected_java_version
      when: "java_version != 'LATEST'"
    
    -
      name: "Setting unified Java version variable"
      ansible.builtin.set_fact:
        java_version_to_install: "{{ computed_java_version.stdout if (java_version == 'LATEST') else selected_java_version.stdout }}"
    
    -
      name: "Installing Java via SDKMAN: {{ java_version }}"
      become_user: "{{ sdk_service_user }}"
      ansible.builtin.shell: . {{ sdkman_dir }}/bin/sdkman-init.sh && sdk install java {{ java_version_to_install }} {{ 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
