---
-
  name: "Block to Allow Loading of Variables without running task"
  when: run_tasks
  block:
    -
      name: "Disabling Exim to prevent it from interfering with Domino"
      when: not ansible_module_running_in_container
      ansible.builtin.service:
        name: "exim4"
        state: stopped
        enabled: false

    -
      name: "Creating Domino installation directories"
      ansible.builtin.file:
        mode: '0644'
        path: "{{ item }}"
        state: directory
      with_items:
        - "{{ installer_dir }}/domino"
        - "{{ installer_dir }}/domino/core"
        - "{{ installer_dir }}/domino/fixpack"
        - "{{ installer_dir }}/domino/hotfix"
        - "{{ installer_dir }}/domino/archives"

    -
      name: "Checking if the Domino Installer archive is at /domino/archives/{{ domino_server_installer_tar }}"
      register: domino_server_installer_check
      ansible.builtin.stat:
        path: "{{ installer_dir }}/domino/archives/{{ domino_server_installer_tar }}"
        get_md5: false

    -
      name: "Checking if Domino has been installed"
      register: domino_server_installed
      ansible.builtin.stat:
        path: "{{ domino_home_dir }}/notes.ini"
        get_md5: false

    -
      name: "Downloading Domino from {{ installer_base_url }}"
      register: domlsresult
      until: "domlsresult is not failed"
      retries: 3
      ansible.builtin.get_url:
        mode: '0755'
        validate_certs: false
        url: "{{ installer_base_url }}/{{ installer_url_app_path }}/{{ domino_server_installer_tar }}"
        dest: "{{ installer_dir }}/domino/archives/{{ domino_server_installer_tar }}"
        username: "{{ installer_url_user }}"
        password: "{{ installer_url_pass }}"
      when: not domino_server_installer_check.stat.exists and not domino_server_installed.stat.exists

    -
      name: "Extracting Domino from {{ domino_server_installer_tar }}"
      when: not domino_server_installed.stat.exists
      ansible.builtin.unarchive:
        src: "{{ installer_dir }}/domino/archives/{{ domino_server_installer_tar }}"
        dest: "{{ installer_dir }}/domino/core"
        creates: "{{ installer_dir }}/domino/core/linux64"
        remote_src: true

    -
      name: "Checking if the Domino fixpack installer archive is at /domino/archives/{{ domino_server_fixpack_tar }}"
      register: fixpack_archive
      ansible.builtin.stat:
        path: "{{ installer_dir }}/domino/archives/{{ domino_server_fixpack_tar }}"
        get_md5: false

    -
      name: "Checking if the Domino fixpack extracted installer is at /domino/fixpack/linux64"
      register: fixpack_extracted
      ansible.builtin.stat:
        path: "{{ installer_dir }}/domino/fixpack/linux64"
        get_md5: false

    -
      name: "Checking if the Domino fixpack has been installed"
      register: fixpack_installed
      ansible.builtin.stat:
        path: "{{ completed_dir }}/fpinstalled"
        get_md5: false

    -
      name: "Downloading Domino Fixpack from {{ installer_base_url }}"
      register: fplsresult
      until: "fplsresult is not failed"
      retries: 3
      when: not fixpack_archive.stat.exists and not fixpack_extracted.stat.exists and domino_installer_fixpack_install and not fixpack_installed.stat.exists
      ansible.builtin.get_url:
        validate_certs: false
        mode: '0755'
        url: "{{ installer_base_url }}/{{ installer_url_app_path }}/{{ domino_server_fixpack_tar }}"
        dest: "{{ installer_dir }}/domino/archives/{{ domino_server_fixpack_tar }}"
        username: "{{ installer_url_user }}"
        password: "{{ installer_url_pass }}"

    -
      name: "Extracting Domino fixpack from {{ domino_server_fixpack_tar }}"
      when: domino_installer_fixpack_install and not fixpack_extracted.stat.exists and not fixpack_installed.stat.exists
      ansible.builtin.unarchive:
        src: "{{ installer_dir }}/domino/archives/{{ domino_server_fixpack_tar }}"
        dest: "{{ installer_dir }}/domino/fixpack"
        creates: "{{ installer_dir }}/domino/fixpack/linux64"
        remote_src: true

    -
      name: "Checking if the Domino hotfix installer archive is at /domino/archives/{{ domino_server_hotfix_tar }}"
      register: hotfix_installer_check
      ansible.builtin.stat:
        path: "{{ installer_dir }}/domino/archives/{{ domino_server_hotfix_tar }}"
        get_md5: false

    -
      name: "Checking if the Domino hotfix extracted installer is at /domino/hotfix/domino"
      register: hotfix_extracted
      ansible.builtin.stat:
        path: "{{ installer_dir }}/domino/hotfix/domino"
        get_md5: false

    -
      name: "Checking if the Domino hotfix has been installed"
      register: hotfix_installed
      ansible.builtin.stat:
        path: "{{ completed_dir }}/hfinstalled"
        get_md5: false

    -
      name: "Downloading Domino hotfix from {{ installer_base_url }}"
      register: hflsresult
      until: "hflsresult is not failed"
      retries: 3
      ansible.builtin.get_url:
        validate_certs: false
        mode: '0755'
        url: "{{ installer_base_url }}/{{ installer_url_app_path }}/{{ domino_server_hotfix_tar }}"
        dest: "{{ installer_dir }}/domino/archives/{{ domino_server_hotfix_tar }}"
        username: "{{ installer_url_user }}"
        password: "{{ installer_url_pass }}"
      when: not hotfix_installer_check.stat.exists and not hotfix_extracted.stat.exists and domino_installer_hotfix_install and not hotfix_installed.stat.exists

    -
      name: "Extracting Domino Hotfix from {{ domino_server_hotfix_tar }}"
      when: domino_installer_hotfix_install and not hotfix_extracted.stat.exists and not hotfix_installed.stat.exists
      ansible.builtin.unarchive:
        src: "{{ installer_dir }}/domino/archives/{{ domino_server_hotfix_tar }}"
        dest: "{{ installer_dir }}/domino/hotfix"
        creates: "{{ installer_dir }}/domino/hotfix/domino"
        remote_src: true

    -
      name: "Creating the group {{ domino_group }}"
      ansible.builtin.group:
        name: "{{ domino_group }}"
        state: present

    -
      name: "Adding user group: {{ domino_group }}"
      ansible.builtin.user:
        name: "{{ domino_user }}"
        shell: /bin/sh
        groups: "{{ domino_group }}"
        home: "{{ domino_home_dir }}"

    -
      name: "Adding soft nofile limits in limits.conf for: {{ domino_user }}"
      community.general.pam_limits:
        domain: "{{ domino_user }}"
        limit_type: soft
        limit_item: nofile
        value: "{{ domino_user_soft_limit }}"

    -
      name: "Adding hard nofile limits to limits.conf for: {{ domino_user }}"
      community.general.pam_limits:
        domain: "{{ domino_user }}"
        limit_type: hard
        limit_item: nofile
        value: "{{ domino_user_hard_limit }}"

    -
      name: "Adding Domino silent install response file"
      when: not domino_server_installed.stat.exists
      ansible.builtin.template:
        dest: "{{ installer_dir }}/domino/installer.properties"
        mode: a+x
        src: "installer.properties.j2"

    -
      name: "Installing Domino"
      ansible.builtin.command: "{{ item }}"
      become: true
      become_user: root
      when: not domino_server_installed.stat.exists
      args:
        chdir: "{{ installer_dir }}/domino/core/linux64"
        creates: "{{ domino_home_dir }}/notes.ini"
      with_items:
        - "bash ./install -f {{ installer_dir }}/domino/installer.properties -i silent"
      environment:
        INSTALL_NO_CHECK: "true"

    -
      name: "Configuring fixpack installer {{ domino_fixpack_version }}"
      when: domino_installer_fixpack_install and not fixpack_installed.stat.exists
      ansible.builtin.lineinfile:
        path: "{{ installer_dir }}/domino/fixpack/linux64/domino/script.dat"
        line: "installation_type = 2"

    -
      name: "Installing Domino fixpack {{ domino_fixpack_version }}"
      ansible.builtin.command: "{{ item }}"
      become: true
      become_user: root
      environment:
        NUI_NOTESDIR: "{{ domino_install_basedir }}/"
      args:
        chdir: "{{ installer_dir }}/domino/fixpack/linux64/domino"
        creates: "{{ completed_dir }}/fpinstalled"
      when: domino_installer_fixpack_install and not fixpack_installed.stat.exists
      with_items:
        - "bash ./install -script ./script.dat && touch {{ completed_dir }}/fpinstalled"

    -
      name: "Configuring hotfix installer {{ domino_hotfix_version }}"
      when: domino_installer_hotfix_install and not hotfix_installed.stat.exists
      ansible.builtin.lineinfile:
        path: "{{ installer_dir }}/domino/hotfix/linux64/script.dat"
        line: "installation_type = 2"

    -
      name: "Installing Domino Hotfix {{ domino_hotfix_version }}"
      ansible.builtin.command: "{{ item }}"
      become: true
      become_user: root
      environment:
        NUI_NOTESDIR: "{{ domino_install_basedir }}/"
      args:
        chdir: "{{ installer_dir }}/domino/hotfix/linux64"
        creates: "{{ completed_dir }}/hfinstalled"
      when: domino_installer_hotfix_install and not hotfix_installed.stat.exists
      with_items:
        - "bash ./install -script ./script.dat && touch {{ completed_dir }}/hfinstalled"

    -
      name: "Append ADMIN_IGNORE_NEW_SERVERNAMING_CONVENTION to notes.ini"
      ansible.builtin.lineinfile:
        path: "{{ domino_home_dir }}/notes.ini"
        line: "ADMIN_IGNORE_NEW_SERVERNAMING_CONVENTION=1"
        insertafter: EOF
        state: present
      when: domino_major_version == 12 and domino_patch_version == 1

    -
      name: "Append EnableJavaAgentCache=2 to notes.ini: https://github.com/STARTcloud/hcl_roles/issues/8"
      ansible.builtin.lineinfile:
        path: "{{ domino_home_dir }}/notes.ini"
        line: "EnableJavaAgentCache=2"
        insertafter: EOF
        state: present

    -
      name: "Append TEST_JWTIDVAULT to notes.ini"
      ansible.builtin.blockinfile:
        path: "{{ domino_home_dir }}/notes.ini"
        insertafter: EOF
        content: |
          $$TEST_JWTIDVAULT=1

      when: domino_major_version == 14
