---
-
  name: "Block to Allow Loading of Variables without running task"
  when: run_tasks
  block:
    -
      name: "Determine REST API archive from version"
      ansible.builtin.set_fact:
        domino_rest_api_archive: "{% if domino_rest_api_archive is defined and domino_rest_api_archive != '' and domino_rest_api_archive is not none %}{{ domino_rest_api_archive }}{% elif domino_rest_api_version is none or domino_rest_api_version == '' %}Domino_REST_API_V1.1.2_Installer.tar.gz{% elif domino_rest_api_version == '1' %}Domino_REST_API_V1_Installer.tar.gz{% elif domino_rest_api_version is version('1.1.5', '>=') and domino_minor_version|int >= 5 %}Domino_REST_API_V{{ domino_rest_api_version }}_R{{ domino_major_version }}.{{ domino_minor_version }}_Installer.tar.gz{% elif domino_rest_api_version is version('1.0.9', '>=') %}Domino_REST_API_V{{ domino_rest_api_version }}_R{{ domino_major_version }}_Installer.tar.gz{% else %}Domino_REST_API_V{{ domino_rest_api_version }}_Installer.tar.gz{% endif %}"

    -
      name: "Checking if domino-rest-api is installed: {{ domino_rest_api_version }}"
      register: domino_rest_api_installed_check
      ansible.builtin.stat:
        path: "{{ completed_dir }}/domino_rest_api_installed"
        checksum_algorithm: sha256

    -
      name: "Installing Domino Rest API"
      when: not domino_rest_api_installed_check.stat.exists
      block:
        -
          name: "Creating installation directories for domino-rest-api"
          ansible.builtin.file:
            mode: '0644'
            path: "{{ item }}"
            state: directory
          with_items:
            - "{{ installer_dir }}/domino-rest-api/archives"

        -
          name: "Checking if domino-rest-api installer is at domino-rest-api/archives/{{ domino_rest_api_archive }}"
          register: domino_restapi_installer_check
          ansible.builtin.stat:
            path: "{{ installer_dir }}/domino-rest-api/archives/{{ domino_rest_api_archive }}"
            checksum_algorithm: sha256

        -
          name: "Downloading domino-rest-api from {{ installer_base_url }} using Bearer token"
          register: domino_rest_apiresult_bearer
          ansible.builtin.get_url:
            mode: '0755'
            validate_certs: false
            url: "{{ installer_base_url }}/{{ installer_url_app_path }}/{{ domino_rest_api_archive }}"
            dest: "{{ installer_dir }}/domino-rest-api/archives/{{ domino_rest_api_archive }}"
            headers:
              Authorization: "Bearer {{ secrets.installer_url_bearer }}"
          when: 
            - not domino_restapi_installer_check.stat.exists
            - secrets.installer_url_bearer is defined
            - secrets.installer_url_bearer != ''
          failed_when: false

        -
          name: "Downloading domino-rest-api from {{ installer_base_url }} using basic auth"
          register: domino_rest_apiresult
          until: "domino_rest_apiresult is not failed"
          retries: 3
          ansible.builtin.get_url:
            mode: '0755'
            validate_certs: false
            url: "{{ installer_base_url }}/{{ installer_url_app_path }}/{{ domino_rest_api_archive }}"
            dest: "{{ installer_dir }}/domino-rest-api/archives/{{ domino_rest_api_archive }}"
            username: "{{ installer_url_user }}"
            password: "{{ secrets.installer_url_pass }}"
          when: 
            - not domino_restapi_installer_check.stat.exists
            - (secrets.installer_url_bearer is not defined or secrets.installer_url_bearer == '' or domino_rest_apiresult_bearer is failed)

        -
          name: "Extracting domino-rest-api from {{ domino_rest_api_archive }}"
          ansible.builtin.unarchive:
            mode: "a+x"
            owner: "{{ domino_user }}"
            group: "{{ domino_group }}"
            src: "{{ installer_dir }}/domino-rest-api/archives/{{ domino_rest_api_archive }}"
            dest: "{{ installer_dir }}/domino-rest-api"
            creates: "{{ installer_dir }}/domino-rest-api/restapiInstall.jar"
            remote_src: true

        -
          name: "Finding REST API installer jar file in extracted archive"
          register: domino_rest_api_jar_files
          ansible.builtin.find:
            paths: "{{ installer_dir }}/domino-rest-api"
            patterns: "restapiInstall*.jar"
            file_type: file

        -
          name: "Setting REST API installer jar filename from discovered file"
          ansible.builtin.set_fact:
            domino_rest_api_installer_jar: "{{ domino_rest_api_jar_files.files[0].path | basename }}"
          when: domino_rest_api_jar_files.matched > 0

        -
          name: "Failing if no REST API installer jar found in archive"
          ansible.builtin.fail:
            msg: "Could not find restapiInstall*.jar in {{ installer_dir }}/domino-rest-api"
          when: domino_rest_api_jar_files.matched == 0

        -
          name: "Stopping Domino for Changes to take effect"
          become: true
          ansible.builtin.service:
            name: domino
            state: stopped
            enabled: true
          register: domino_service_stopped_domino_restapi
          until: domino_service_stopped_domino_restapi.state == "stopped"
          retries: 3
          delay: 5

        -
          name: "Waiting for 30 seconds to let Domino fully shutdown"
          ansible.builtin.pause:
            seconds: "{{ domino_shutdown_delay }}"

        -
          name: "Installing Domino Rest API"
          ansible.builtin.shell: "{{ item }}"
          become: true
          become_user: "{{ service_user }}"
          args:
            chdir: "{{ installer_dir }}/domino-rest-api"
            executable: "/bin/bash"
            creates: "/opt/hcl/restapi/notices.txt"
          with_items:
            - "sudo {{ domino_install_basedir }}/notes/latest/linux/java -jar {{ domino_rest_api_installer_jar }} -d={{ domino_home_dir }} -i={{ domino_home_dir }}/notes.ini -r=/opt/hcl/restapi -p={{ domino_install_dir }} -a"

        -
          name: "Marking Domino Rest API as installed"
          ansible.builtin.file:
            mode: '0644'
            path: "{{ item }}"
            state: touch
          with_items:
            - "{{ completed_dir }}/domino_rest_api_installed"

        -
          name: "Starting Domino for Changes to take effect"
          become: true
          ansible.builtin.service:
            name: domino
            state: started
            enabled: true
          register: domino_service_start_details_domino_restapi
          until: domino_service_start_details_domino_restapi.state == "started"
          retries: 10
          delay: 10

        -
          name: "Waiting for HTTP Server to start"
          ansible.builtin.wait_for:
            path: /local/notesdata/IBM_TECHNICAL_SUPPORT/console.log
            search_regex: "HTTP Server: Started"
            timeout: 300
            msg: "HTTP Server did not start within the timeout period."

        -
          name: "Waiting for 60 seconds to let Domino HTTP fully configure"
          ansible.builtin.pause:
            seconds: "{{ domino_startup_delay }}"
