---
-
  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 }}-self-signed.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 }}-self-signed.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/default-signed.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/default-signed.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 Installation Directories"
  ansible.builtin.file:
    mode: '0755'
    path: "{{ item }}"
    state: directory
    owner: "{{ domino_user }}"
    group: "{{ domino_group }}"
  with_items:
    - "/safe-id-to-cross-certify"
    - "{{ 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: "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
  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: Pause for 60 seconds to let Domino fully configure
  when: not domino_server_touched.stat.exists
  ansible.builtin.pause:
    seconds: 60

-
  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: "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

-
  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 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: Pause 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'

-
  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: 'Copying userID per "convention" for cross-certification'
  become: true
  when: not domino_server_touched.stat.exists
  ansible.builtin.file:
    path: "{{ domino_home_dir }}/ids/{{ domino_admin_user_id }}"
    owner: "{{ domino_user }}"
    group: "{{ domino_group }}"
    mode: '0755'

-
  name: 'Copying userID per "convention" for cross-certification'
  become: true
  when: not domino_server_touched.stat.exists
  ansible.builtin.copy:
    mode: '0755'
    src: "{{ domino_home_dir }}/ids/{{ domino_admin_user_id }}"
    dest: "{{ service_home_dir }}/user.id"
    owner: "{{ service_user }}"
    group: "{{ service_group }}"

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