---
- name: Decide which Debian release to use
  ansible.builtin.set_fact:
    java_apt_release: '{% if ansible_distribution_release == "jessie" %}jessie-backports{% else %}{{ ansible_distribution_release }}{% endif %}'
  tags:
    - java

- name: Install jessie-backports
  ansible.builtin.include_role:
    name: evolix/apt
    tasks_from: backports.yml
  when: ansible_distribution_release == "jessie"
  tags:
    - java

- name: Install default openjdk package
  ansible.builtin.apt:
    name: "default-jre-headless"
    default_release: "{{ java_apt_release }}"
    state: present
  tags:
    - java
    - packages
  when: java_version is none

- name: Install specific openjdk package
  ansible.builtin.apt:
    name: "openjdk-{{ java_version }}-jre-headless"
    default_release: "{{ java_apt_release }}"
    state: present
  tags:
    - java
    - packages
  when: java_version is not none

- name: This openjdk version is the default alternative
  community.general.alternatives:
    name: java
    path: "{{ java_bin_path[java_version] }}"
  tags:
    - java
  when:
    - java_default_alternative | bool
    - java_version is not none
