---
## Install Guide here: https://help.hcltechsw.com/traveler/11.0.0/Silent_install_Linux.html
-
  name: "Block to Allow Loading of Variables without running task"
  when: run_tasks
  block:
    -
      name: "Checking if Traveler is installed: {{ traveler_version }} "
      register: traveler_installed_check
      ansible.builtin.stat:
        path: "{{ completed_dir }}/traveler_installed"
        get_md5: false

    -
      name: "Block to Allow Loading of Variables without running task"
      when: not traveler_installed_check.stat.exists
      block:
        -
          name: "Creating installation directories for Traveler"
          ansible.builtin.file:
            mode: '0644'
            path: "{{ item }}"
            state: directory
          with_items:
            - "{{ installer_dir }}/traveler/archives"
            - "{{ installer_dir }}/traveler/Traveler"

        -
          name: "Checking if Traveler installer is at traveler/archives/{{ traveler_archive }}"
          register: traveler_installer_check
          ansible.builtin.stat:
            path: "{{ installer_dir }}/traveler/archives/{{ traveler_archive }}"
            get_md5: false

        -
          name: "Downloading Traveler from {{ installer_base_url }}"
          register: travelerresult
          until: "travelerresult is not failed"
          retries: 3
          ansible.builtin.get_url:
            mode: '0755'
            validate_certs: false
            url: "{{ installer_base_url }}/{{ installer_url_app_path }}/{{ traveler_archive }}"
            dest: "{{ installer_dir }}/traveler/archives/{{ traveler_archive }}"
            username: "{{ installer_url_user }}"
            password: "{{ secrets.installer_url_pass }}"
          when: not traveler_installer_check.stat.exists

        -
          name: "Extracting Traveler from {{ traveler_archive }}"
          ansible.builtin.unarchive:
            mode: "a+x"
            owner: "{{ domino_user }}"
            group: "{{ domino_group }}"
            src: "{{ installer_dir }}/traveler/archives/{{ traveler_archive }}"
            dest: "{{ installer_dir }}/traveler/Traveler"
            creates: "{{ installer_dir }}/traveler/Traveler/Traveler"
            remote_src: true

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

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

        -
          name: "Adding Traveler silent install response file"
          ansible.builtin.template:
            dest: "{{ installer_dir }}/traveler/Traveler/installer.properties"
            mode: a+x
            src: "installer.properties.j2"

        -
          name: "Installing Traveler"
          ansible.builtin.shell: "{{ item }}"
          become: true
          args:
            executable: "/bin/bash"
            chdir: "{{ installer_dir }}/traveler/Traveler"
            creates: "{{ completed_dir }}/traveler_installed"
          with_items:
            - "./TravelerSetup -f ./installer.properties -i SILENT -l en && touch {{ completed_dir }}/traveler_installed"

        -
          name: "Checking if Traveler FP is installed: {{ traveler_fixpack_version }} "
          register: traveler_fp_installed_check
          ansible.builtin.stat:
            path: "{{ completed_dir }}/traveler_fp_installed"
            get_md5: false

        -
          name: "Block to Allow Installation of Traveler Fixpack"
          when: traveler_fixpack_install and not traveler_fp_installed_check.stat.exists
          block:
            -
              name: "Creating installation directories for Traveler FP"
              ansible.builtin.file:
                mode: '0644'
                path: "{{ item }}"
                state: directory
              with_items:
                - "{{ installer_dir }}/traveler/Traveler_{{ traveler_fixpack_version }}"

            -
              name: "Checking if Traveler FP installer is at traveler/archives/{{ traveler_fixpack_archive }}"
              register: traveler_fp_installer_check
              ansible.builtin.stat:
                path: "{{ installer_dir }}/traveler/archives/{{ traveler_fixpack_archive }}"
                get_md5: false

            -
              name: "Downloading Traveler FP from {{ installer_base_url }}"
              register: travelerfpresult
              until: "travelerfpresult is not failed"
              retries: 3
              ansible.builtin.get_url:
                mode: '0755'
                validate_certs: false
                url: "{{ installer_base_url }}/{{ installer_url_app_path }}/{{ traveler_fixpack_archive }}"
                dest: "{{ installer_dir }}/traveler/archives/{{ traveler_fixpack_archive }}"
                username: "{{ installer_url_user }}"
                password: "{{ secrets.installer_url_pass }}"
              when: not traveler_fp_installer_check.stat.exists

            -
              name: "Extracting Traveler FP from {{ traveler_fixpack_archive }}"
              ansible.builtin.unarchive:
                mode: "a+x"
                owner: "{{ domino_user }}"
                group: "{{ domino_group }}"
                src: "{{ installer_dir }}/traveler/archives/{{ traveler_archive }}"
                dest: "{{ installer_dir }}/traveler/Traveler_{{ traveler_fixpack_version }}"
                creates: "{{ installer_dir }}/traveler/Traveler_{{ traveler_fixpack_version }}/Traveler"
                remote_src: true

            -
              name: "Adding Traveler FP silent install response file"
              ansible.builtin.template:
                dest: "{{ installer_dir }}/traveler/Traveler_{{ traveler_fixpack_version }}/installer.properties"
                mode: a+x
                src: "installer.properties.j2"

            -
              name: "Installing Traveler FP"
              ansible.builtin.shell: "{{ item }}"
              become: true
              args:
                executable: "/bin/bash"
                chdir: "{{ installer_dir }}/traveler/Traveler_{{ traveler_fixpack_version }}"
                creates: "{{ completed_dir }}/traveler_fp_installed"
              with_items:
                - "./TravelerSetup -f ./installer.properties -i SILENT -l en && touch {{ completed_dir }}/traveler_fp_installed"

        -
          name: "Creating/Updating Domino Traveler internet Site Document"
          block:
              -
                name: "Handing templated gather_domino_traveler_internet_site_doc.json JSON to Genesis"
                ansible.builtin.template:
                  dest: "{{ domino_home_dir }}/JavaAddin/Genesis/json/gather_domino_traveler_internet_site_doc.json"
                  mode: "a+x"
                  owner: "{{ domino_user }}"
                  group: "{{ domino_group }}"
                  src: "gather_domino_traveler_internet_site_doc.json.j2"

              -
                name: "Waiting until Genesis returns OK after processing gather_domino_traveler_internet_site_doc.json"
                ansible.builtin.wait_for:
                  path: "{{ domino_home_dir }}/JavaAddin/Genesis/jsonresponse/gather_domino_traveler_internet_site_doc.json"

              -
                name: "Checking if gather_domino_traveler_internet_site_doc.json JSON applied succesfully"
                ansible.builtin.lineinfile:
                  path: "{{ domino_home_dir }}/JavaAddin/Genesis/jsonresponse/gather_domino_traveler_internet_site_doc.json"
                  line: "OK"
                  state: present
                check_mode: true
                register: presence_oidc_gather_for_additional
                failed_when: presence_oidc_gather_for_additional is changed

              -
                name: "Checking for traveler_internet_site_doc_check"
                register: traveler_internet_site_doc_check
                ansible.builtin.stat:
                  path: "/JavaAddin/Genesis/jsonresponse/oidc_domino_traveler_internet_site_doc.txt"
                  get_md5: false

              -
                name: "Reading OIDC hostnames from file"
                when: traveler_internet_site_doc_check.stat.exists
                ansible.builtin.slurp:
                  src: "{{ domino_home_dir }}/JavaAddin/Genesis/jsonresponse/oidc_domino_traveler_internet_site_doc.txt"
                register: oidc_provider_file_internet_site_doc_for_traveler

              -
                name: "Processing Domino OIDC Provider Inetsite doc entry"
                when: traveler_internet_site_doc_check.stat.exists
                ansible.builtin.set_fact:
                  oidc_provider_file_internet_site_doc_for_traveler_additional_raw: "{{ oidc_provider_file_internet_site_doc_for_traveler['content'] | b64decode | regex_replace('\n', '') | regex_replace('\r', '') }}"

              -
                name: "Removing OIDCHostname= prefix, checking for Auth URL for server, and ensuring unique entries"
                when: traveler_internet_site_doc_check.stat.exists
                ansible.builtin.set_fact:
                  oidc_provider_file_internet_site_doc_for_traveler_additional: "{{ oidc_provider_file_internet_site_doc_for_traveler_additional_raw | regex_replace('ISiteName=', ' ') | split(' ') | unique | join(' ') }}"

              -
                name: "Setting Update/Create on document creation to prevent duplicates by checking oidc_provider_file_internet_site_doc_for_traveler_additional for matching hostname"
                when: traveler_internet_site_doc_check.stat.exists
                ansible.builtin.set_fact:
                  domino_traveler_inet_site_create: "{{ false if settings.hostname + ' Traveler' in oidc_provider_file_internet_site_doc_for_traveler_additional else domino_traveler_inet_site_create | default(true) }}"

              -
                name: "Handing Traveler templated JSON to Genesis"
                ansible.builtin.template:
                  dest: "{{ domino_home_dir }}/JavaAddin/Genesis/json/domino_oidc_internet_site_traveler.json"
                  mode: "a+x"
                  owner: "{{ domino_user }}"
                  group: "{{ domino_group }}"
                  src: "domino_oidc_internet_site_traveler.json"

              -
                name: "Waiting until Genesis returns OK"
                ansible.builtin.wait_for:
                  path: "{{ domino_home_dir }}/JavaAddin/Genesis/jsonresponse/domino_oidc_internet_site_traveler.json"

              -
                name: "Checking if JSON applied succesfully"
                ansible.builtin.lineinfile:
                  path: "{{ domino_home_dir }}/JavaAddin/Genesis/jsonresponse/domino_oidc_internet_site_traveler.json"
                  line: "OK"
                  state: present
                check_mode: true
                register: presence_traveler
                failed_when: presence_traveler is changed

        -
          name: "Starting Domino for Changes to take effect"
          become: true
          ansible.builtin.service:
            name: domino
            state: started
            enabled: true
          register: domino_service_start_details_traveler
          until: domino_service_start_details_traveler.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 }}"