---
-
  name: "Block to Allow Loading of Variables without running task"
  when: run_tasks
  block:
    -
      name: "Gathering facts on current container"
      community.docker.current_container_facts:
    
    #-
    #  name: "Grab the Domino Organization if this is an additional server"
    #  ansible.builtin.shell: "{{ item }}"
    #  become: true
    #  become_user: "{{ service_user }}"
    #  args:
    #    chdir: ~
    #    executable: /bin/bash
    #  environment:
    #    PASSWORD: "{{ domino_admin_notes_id_password }}"
    #  when: not domino_server_touched.stat.exists and is_additional_server
    #  with_items:
    #    - "java -jar CheckServerID.jar"
    #  register: domino_organization
    #
    #-
    #  name: "If additional server, set the domino organization"
    #  ansible.builtin.set_fact:
    #    domino_organization: "{{ domino_organization.stdout }}"
    #  when: not domino_server_touched.stat.exists and is_additional_server
    -
      name: "Setting Directories Permissions for SSLs"
      ansible.builtin.file:
        path: "{{ item }}"
        state: directory
        owner: "{{ domino_user }}"
        group: "{{ domino_group }}"
        mode: '0755'
        recurse: yes
      with_items:
        - "{{ cert_dir }}"
        - "{{ cert_dir }}/key"
        - "{{ cert_dir }}/crt"
        - "{{ cert_dir }}/csr"
        - "{{ cert_dir }}/pfx"
        - "{{ cert_dir }}/kyr"
        - "{{ cert_dir }}/jks"
        - "{{ cert_dir }}/combined"
        - "{{ cert_dir }}/ca"
    
    -
      name: "Creating KYR for Domino"
      ansible.builtin.shell: "{{ item }}"
      become_user: "{{ domino_user }}"
      when: domino_https_enabled
      args:
        executable: "/bin/bash"
        chdir: "{{ domino_home_dir }}"
        creates: "{{ cert_dir }}/kyr/{{ kyr_cert }}"
      with_items:
        - "{{ domino_install_basedir }}/bin/tools/startup {{ domino_install_dir }}/kyrtool create -k {{ cert_dir }}/kyr/{{ kyr_cert }} -p {{ cert_pass }}"
    
    -
      name: "Adding SSL Keys to KYR"
      ansible.builtin.shell: >
        {{ domino_install_basedir }}/bin/tools/startup
        {{ domino_install_dir }}/kyrtool import keys
        -k {{ cert_dir }}/kyr/{{ kyr_cert }}
        -i {{ cert_dir }}/key/{{ settings.hostname }}.{{ settings.domain }}.key &&
        touch {{ completed_dir }}/kyr-key-imported
      become_user: "{{ domino_user }}"
      when: domino_https_enabled and selfsigned_enabled
      args:
        executable: "/bin/bash"
        chdir: "{{ domino_home_dir }}"
        creates: "{{ completed_dir }}/kyr-key-imported"
    
    -
      name: "Adding SSL Certificate to KYR"
      ansible.builtin.shell: >
        {{ domino_install_basedir }}/bin/tools/startup
        {{ domino_install_dir }}/kyrtool import certs
        -k {{ cert_dir }}/kyr/{{ kyr_cert }}
        -i {{ cert_dir }}/crt/{{ settings.hostname }}.{{ settings.domain }}.crt &&
        touch {{ completed_dir }}/kyr-cert-imported
      become_user: "{{ domino_user }}"
      when: domino_https_enabled and selfsigned_enabled
      args:
        executable: "/bin/bash"
        chdir: "{{ domino_home_dir }}"
        creates: "{{ completed_dir }}/kyr-cert-imported"
    
    -
      name: "Adding SSL Keys to KYR"
      ansible.builtin.shell: >
        {{ domino_install_basedir }}/bin/tools/startup
        {{ domino_install_dir }}/kyrtool import keys
        -k {{ cert_dir }}/kyr/{{ kyr_cert }}
        -i {{ cert_dir }}/key/{{ settings.hostname }}.{{ settings.domain }}.key &&
        touch {{ completed_dir }}/kyr-key-imported
      become_user: "{{ domino_user }}"
      when: domino_https_enabled and not selfsigned_enabled
      args:
        executable: "/bin/bash"
        chdir: "{{ domino_home_dir }}"
        creates: "{{ completed_dir }}/kyr-key-imported"
    
    -
      name: "Adding SSL Certificate to KYR"
      ansible.builtin.shell: >
        {{ domino_install_basedir }}/bin/tools/startup
        {{ domino_install_dir }}/kyrtool import certs
        -k {{ cert_dir }}/kyr/{{ kyr_cert }}
        -i {{ cert_dir }}/crt/{{ settings.hostname }}.{{ settings.domain }}.crt &&
        touch {{ completed_dir }}/kyr-cert-imported
      become_user: "{{ domino_user }}"
      when: domino_https_enabled and not selfsigned_enabled
      args:
        executable: "/bin/bash"
        chdir: "{{ domino_home_dir }}"
        creates: "{{ completed_dir }}/kyr-cert-imported"
    
    -
      name: "Creating Domino DAOS, Translogs, NIFNSF and FullText Thread Path directories"
      ansible.builtin.file:
        mode: '0755'
        path: "{{ item }}"
        state: directory
        owner: "{{ domino_user }}"
        group: "{{ domino_group }}"
      with_items:
        - "{{ domino_daos_base_path }}"
        - "{{ domino_update_fulltext_thread_path }}"
        - "{{ domino_nifnsf_base_path }}"
        - "{{ domino_translogs_path }}"
    
    -
      name: "Creating Installation Directories"
      ansible.builtin.file:
        mode: '0755'
        path: "{{ item }}"
        state: directory
        owner: "{{ domino_user }}"
        group: "{{ domino_group }}"
      with_items:
        - "/id-files/user-safe-ids"
        - "/id-files/user-ids"
        - "/id-files/server-ids"
        - "{{ domino_home_dir }}/idvault"
        - "{{ domino_home_dir }}/ids"
    
    -
      name: "Checking if Domino has been touched"
      register: domino_server_touched
      ansible.builtin.stat:
        path: "{{ completed_dir }}/domsetup"
        get_md5: false
    
    -
      name: 'Copying Server ID for One Touch Additional Server setup'
      become: true
      when: not domino_server_touched.stat.exists and is_additional_server
      ansible.builtin.copy:
        mode: '0755'
        src: "/id-files/server-ids/{{ domino_server_id }}"
        dest: "{{ domino_home_dir }}/ids/{{ domino_server_id }}"
        owner: "{{ domino_user }}"
        group: "{{ domino_group }}"
    
    -
      name: "Copying Safe ID for One Touch Additional Server setup"
      become: true
      when: not domino_server_touched.stat.exists and not is_additional_server
      ansible.builtin.copy:
        mode: '0755'
        src: "/id-files/user-safe-ids/{{ user_safe_id }}"
        dest: "{{ domino_home_dir }}/ids/{{ user_safe_id }}"
        owner: "{{ domino_user }}"
        group: "{{ domino_group }}"
    
    -
      name: "Running CreateNamesDatabase application, Cleanup names.nsf, and Generate a new names.nsf"
      ansible.builtin.shell: "{{ item }}"
      become: true
      become_user: "{{ service_user }}"
      args:
        chdir: ~
        executable: /bin/bash
        creates: "{{ domino_home_dir }}/names.nsf"
      environment:
        PASSWORD: "{{ domino_admin_notes_id_password }}"
      when: java_helpers_install
      with_items:
        - "java -jar CreateNamesDatabase.jar"
        - "yes {{ domino_admin_notes_id_password }} | java -jar CreateNamesDatabase.jar"
        - "expect CreateNamesDatabase.exp {{ domino_admin_notes_id_password }}"
        - "rm -f {{ domino_home_dir }}/names.nsf"
        - "java -jar CreateNamesDatabase.jar"
    
    -
      name: "Adding the Domino One-Touch Setup.json"
      when: not domino_server_touched.stat.exists
      ansible.builtin.template:
        dest: "{{ domino_home_dir }}/setup.json"
        mode: "a+x"
        owner: "{{ domino_user }}"
        group: "{{ domino_group }}"
        src: setup.json.j2
    
    -
      name: "Configuring Domino server via setup.json"
      when: not domino_server_touched.stat.exists
      ansible.builtin.shell: "{{ item }}"
      become: true
      async: 25920
      poll: 0
      args:
        chdir: "{{ domino_home_dir }}"
        executable: /bin/sh
        creates: "{{ completed_dir }}/domsetup"
      with_items:
        - "(su - {{ domino_user }} -c '{{ domino_install_basedir }}/bin/server -autoconf setup.json && touch {{ completed_dir }}/domsetup  > /dev/null 2>&1 &')"

    -
      name: "Waiting until the file autoconfigure.log is present before continuing"
      when: not domino_server_touched.stat.exists
      ansible.builtin.wait_for:
        path: "{{ domino_home_dir }}/IBM_TECHNICAL_SUPPORT/autoconfigure.log"

    -
      name: "Waiting for 'Application configuration completed successfully' in Autoconfigure"
      when: not domino_server_touched.stat.exists
      ansible.builtin.wait_for:
        path: "{{ domino_home_dir }}/IBM_TECHNICAL_SUPPORT/autoconfigure.log"
        search_regex: "Application configuration completed successfully"
        state: present
        timeout: 300

    -
      name: "Capturing autoconfigure log output"
      ansible.builtin.command: "cat {{ domino_home_dir }}/IBM_TECHNICAL_SUPPORT/autoconfigure.log"
      register: autoconf_result
      when: ( debug_autoconfigure or debug_all ) and not domino_server_touched.stat.exists
      changed_when: false
    
    -
      name: "Outputting Autoconfigure Log"
      ansible.builtin.debug:
        var: autoconf_result.stdout_lines
      when: ( debug_autoconfigure or debug_all ) and not domino_server_touched.stat.exists
    
    -
      name: "Waiting for 60 seconds to let Domino fully configure"
      when: not domino_server_touched.stat.exists
      ansible.builtin.pause:
        seconds: 60
    
    -
      name: "Cleanly stopping Domino because this was detected as a Secondary Server Setup"
      when: not domino_server_touched.stat.exists and is_additional_server 
      ansible.builtin.shell: "{{ item }}"
      become: true
      args:
        executable: "/bin/bash"
      register: domino_stop_status
      with_items:
        - 'domino cmd "quit" 20'
    
    -
      name: "Outputting Domino Stop Status"
      when: ( debug_autoconfigure or debug_all ) and not domino_server_touched.stat.exists and is_additional_server
      ansible.builtin.debug:
        var: domino_stop_status
    
    -
      name: "Waiting for 60 seconds to let Domino fully configure"
      when: not domino_server_touched.stat.exists and is_additional_server
      ansible.builtin.pause:
        seconds: 60
    
    -
      name: "Starting Domino for Changes to take effect"
      become: true
      when: not domino_server_touched.stat.exists and is_additional_server and not ansible_module_running_in_container
      ansible.builtin.service:
        name: domino
        state: started
        enabled: true
      register: domino_service_start_details
      retries: 3
      delay: 5
      until: domino_service_start_details.state == "started"
    
    -
      name: "Waiting for 60 seconds to let Domino fully configure"
      when: not domino_server_touched.stat.exists and is_additional_server
      ansible.builtin.pause:
        seconds: 60
    
    -
      name: "Waiting until the autoconfigure.log has the Completed Succesfully String"
      when: not domino_server_touched.stat.exists and is_additional_server
      ansible.builtin.wait_for:
        path: "{{ domino_home_dir }}/IBM_TECHNICAL_SUPPORT/autoconfigure.log"
        search_regex: Application configuration completed successfully
    
    -
      name: "Telling Domino to load certmgr to replicate the certificates"
      when: not domino_server_touched.stat.exists and is_additional_server
      ansible.builtin.shell: "{{ item }}"
      become: true
      args:
        executable: "/bin/bash"
      register: domino_stop_status
      with_items:
        - 'domino cmd "load certmgr" 20'
    
    -
      name: "Telling Domino to load certmgr to replicate the certificates"
      when: not domino_server_touched.stat.exists and is_additional_server
      ansible.builtin.shell: "{{ item }}"
      become: true
      args:
        executable: "/bin/bash"
      register: domino_stop_status
      with_items:
        - 'domino cmd "load certmgr -importkyr {{ cert_dir }}/kyr/{{ kyr_cert }}"'
    
    -
      name: "Waiting until the file certstore.nsf is present before continuing"
      when: not domino_server_touched.stat.exists
      ansible.builtin.wait_for:
        path: "{{ domino_home_dir }}/certstore.nsf"
    
    -
      name: "Waiting for 60 seconds to let Domino fully configure"
      when: not domino_server_touched.stat.exists
      ansible.builtin.pause:
        seconds: 60
    
    -
      name: "Cleanly stopping Domino"
      when: not domino_server_touched.stat.exists
      ansible.builtin.shell: "{{ item }}"
      become: true
      args:
        executable: "/bin/bash"
      register: domino_stop_status
      with_items:
        - 'domino cmd "quit" 20'
      changed_when: false
    
    -
      name: "Outputting Domino Stop Status"
      when: ( debug_autoconfigure or debug_all ) and not domino_server_touched.stat.exists
      ansible.builtin.debug:
        var: domino_stop_status
    
    -
      name: "Waiting until the completed installation file is present before continuing"
      when: not domino_server_touched.stat.exists
      ansible.builtin.wait_for:
        path: "{{ completed_dir }}/domsetup"
    
    -
      name: "Setting up Domino OIDC"
      when: 
        - domino_oidc_provider_enabled
        - domino_major_version is version('14', '>=')
#        - (domino_fixpack_version | replace('FP', '') | int) is version('2', '>=')
      block:
        -
          name: "Setting up OIDC Configurations"
          when: not domino_server_touched.stat.exists
          ansible.builtin.template:
            dest: "{{ domino_home_dir }}/oidcinfo.json"
            mode: "0644"
            owner: "{{ domino_user }}"
            group: "{{ domino_group }}"
            src: oidcinfo.json.j2

    -
      name: 'Setting file permissions Admin User ID, filename: {{ domino_admin_user_id }}'
      become: true
      when: not domino_server_touched.stat.exists and not is_additional_server
      ansible.builtin.file:
        path: "{{ domino_home_dir }}/ids/{{ domino_admin_user_id }}"
        owner: "{{ domino_user }}"
        group: "{{ domino_group }}"
        mode: '0644'
    
    -
      name: 'Copying Admin User ID per "convention" for cross-certification: {{ domino_admin_user_id }}'
      become: true
      when: not domino_server_touched.stat.exists and not is_additional_server
      ansible.builtin.copy:
        mode: '0644'
        src: "{{ domino_home_dir }}/ids/{{ domino_admin_user_id }}"
        dest: "{{ service_home_dir }}/user.id"
        owner: "{{ service_user }}"
        group: "{{ service_group }}"
    
    -
      name: 'Copying Admin User ID to /id-files/user-ids/{{ domino_admin_user_id }}'
      become: true
      when: not domino_server_touched.stat.exists and not is_additional_server
      ansible.builtin.copy:
        mode: '0644'
        src: "{{ domino_home_dir }}/ids/{{ domino_admin_user_id }}"
        dest: "/id-files/user-ids/{{ domino_admin_user_id }}"
        owner: "{{ service_user }}"
        group: "{{ service_group }}"
        force: true
    
    -
      name: 'Copying Dev User ID to /id-files/user-ids/{{ domino_dev_user_id }}'
      become: true
      when: not domino_server_touched.stat.exists and not is_additional_server
      ansible.builtin.copy:
        mode: '0644'
        src: "{{ domino_home_dir }}/ids/{{ domino_dev_user_id }}"
        dest: "/id-files/user-ids/{{ domino_dev_user_id }}"
        owner: "{{ service_user }}"
        group: "{{ service_group }}"
        force: true

    -
      name: "Using OTS to generate additional server IDs"
      when: domino_server_id_creation_tool == "ots"
      block:
        -
          name: 'Copying Server IDs to /id-files/server-ids/'
          become: true
          when: not domino_server_touched.stat.exists and not is_additional_server and domino_major_version < 14
          ansible.builtin.copy:
            mode: '0644'
            src: "{{ domino_home_dir }}/ids/{{ ots_generate_server_id_pattern }}{{ item }}.id"
            dest: "/id-files/server-ids/{{ ots_generate_server_id_pattern }}{{ item }}.id"
            owner: "{{ service_user }}"
            group: "{{ service_group }}"
            force: true
          loop: "{{ range(0, ots_generate_server_id_count | int) | list }}"

        -
          name: 'Copying Server IDs to /id-files/server-ids/'
          become: true
          when: not domino_server_touched.stat.exists and not is_additional_server and domino_major_version >= 14
          ansible.builtin.copy:
            mode: '0644'
            src: "{{ domino_home_dir }}/ids/{{ item }}.id"
            dest: "/id-files/server-ids/{{ item }}.id"
            owner: "{{ service_user }}"
            group: "{{ service_group }}"
            force: true
          loop: "{{ ots_generate_server_names | default([]) }}"

    -
      name: "Using the Additional Server Creation Tool to generate Server IDs"
      when: domino_server_id_creation_tool == "asja"
      block:
        -
          name: 'Setting asja_generate_server_ids for loop'
          set_fact:
            actual_asja_generate_server_ids: "{{ asja_generate_server_ids }}"
          when: 
            - not domino_server_touched.stat.exists
            - not is_additional_server
            - asja_generate_server_ids is defined
            - asja_generate_server_ids | length > 0

        -
          name: "Creating the Properties file to Generate Additional Server IDs"
          when: not domino_server_touched.stat.exists
          ansible.builtin.template:
            dest: "/tmp/{{ item.name }}-additional.properties"
            mode: "0644"
            owner: "{{ domino_user }}"
            group: "{{ domino_group }}"
            src: additional.properties.j2
          loop: "{{ actual_asja_generate_server_ids }}"

        -
          name: "Creating Additional Server IDs using Custom Generate Additional Server ID Tool"
          ansible.builtin.shell: "java -jar /local/notesjava/CreateAdditionalServerJNA.jar /tmp/{{ item.name }}-additional.properties"
          become: true
          become_user: "{{ domino_user }}"
          args:
            chdir: "{{ domino_home_dir }}"
            executable: /bin/bash
            creates: "{{ domino_home_dir }}/ids/{{ item.name }}.id"
          environment:
            PASSWORD: "{{ domino_admin_notes_id_password }}"
            LD_LIBRARY_PATH: "{{ domino_install_dir }}"
          when: 
            - not domino_server_touched.stat.exists
            - not is_additional_server
            - asja_generate_server_ids is defined
            - asja_generate_server_ids | length > 0
          loop: "{{ actual_asja_generate_server_ids }}"

        -
          name: "Copying Server IDs to /id-files/server-ids/"
          become: true
          ansible.builtin.copy:
            mode: '0644'
            src: "{{ domino_home_dir }}/ids/{{ item.name }}.id"
            dest: "/id-files/server-ids/{{ item.name }}.id"
            owner: "{{ domino_user }}"
            group: "{{ domino_group }}"
            force: true
          loop: "{{ actual_asja_generate_server_ids }}"
          when: 
            - not domino_server_touched.stat.exists
            - not is_additional_server
            - actual_asja_generate_server_ids is defined
            - actual_asja_generate_server_ids | length > 0

    -
      name: "Stopping Domino and Enabling at boot"
      when: not ansible_module_running_in_container
      become: true
      ansible.builtin.service:
        name: domino
        state: stopped
        enabled: true
