---
-
  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: "Creating Installation Directories"
      ansible.builtin.file:
        mode: '0644'
        path: "{{ item }}"
        state: directory
        owner: "{{ domino_user }}"
        group: "{{ domino_group }}"
      with_items:
        - "{{ installer_dir }}/domino/service-file"
    
    -
      name: "Checking if Nash's Domino Service Scripts has been installed"
      register: domino_server_nash_installed
      ansible.builtin.stat:
        path: "{{ completed_dir }}/service_configured"
        checksum_algorithm: sha256

    -
      name: "Checking if JEDI has been installed"
      register: nash_domino_jedi_installed
      ansible.builtin.stat:
        path: "{{ completed_dir }}/domino_jedi_installed"
        checksum_algorithm: sha256

    -
      name: "Check if Domino service is running"
      register: domino_service_status
      when: nash_domino_jedi_installed.stat.exists and domino_server_nash_installed.stat.exists
      ansible.builtin.service_facts:

    -
      name: "Check if JEDI service is running"
      register: jedi_service_status
      when: nash_domino_jedi_installed.stat.exists and domino_server_nash_installed.stat.exists
      ansible.builtin.service_facts:

    -
      name: "Disabling and Stopping Domino"
      when: >
        nash_domino_jedi_installed.stat.exists and 
        domino_server_nash_installed.stat.exists and
        ansible_facts.services['domino.service'] is defined and 
        ansible_facts.services['domino.service'].state == 'running'
      ansible.builtin.systemd:
        name: "domino"
        state: stopped

    -
      name: "Disabling and Stopping JeDI service"
      when: >
        nash_domino_jedi_installed.stat.exists and 
        domino_server_nash_installed.stat.exists and
        ansible_facts.services[jedi_service + '.service'] is defined and 
        ansible_facts.services[jedi_service + '.service'].state == 'running'
      ansible.builtin.systemd:
        name: "{{ jedi_service }}"
        enabled: false
        state: stopped

    -
      name: "Check for Domino server process"
      community.general.pids:
        pattern: "/opt/hcl/domino/notes/latest/linux/server"
      register: process_check
      when: nash_domino_jedi_installed.stat.exists and domino_server_nash_installed.stat.exists

    -
      name: "Starting Initial Domino Stop Attempt"
      become: true
      when: >
        nash_domino_jedi_installed.stat.exists and
        domino_server_nash_installed.stat.exists and
        process_check.pids | length > 0
      ansible.builtin.shell: 'echo "exit" >> {{ domino_home_dir }}/noteslog/notes.input'

    -
      name: "Waiting for Domino configuration"
      when: >
        nash_domino_jedi_installed.stat.exists and
        domino_server_nash_installed.stat.exists and
        process_check.pids | length == 0
      ansible.builtin.pause:
        seconds: 90

    -
      name: "Setting initial shutdown status"
      when: >
        nash_domino_jedi_installed.stat.exists and
        domino_server_nash_installed.stat.exists
      ansible.builtin.set_fact:
        shutdown_complete: false
    
    -
      name: "Looping checking server status"
      when: >
        nash_domino_jedi_installed.stat.exists and
        domino_server_nash_installed.stat.exists and
        not shutdown_complete
      ansible.builtin.include_tasks: check_shutdown.yml
      loop: "{{ range(1, 11)|list }}"

    -
      name:  "Enabling the nashed service to ensure provisioning works"
      when: nash_domino_jedi_installed.stat.exists and domino_server_nash_installed.stat.exists
      ansible.builtin.service:
        name: domino
        enabled: true

    -
      name: "Downloading Nash's Service Script for Linux version {{ nash_domino_service_script_version }}"
      when: not domino_server_nash_installed.stat.exists
      register: nashresult
      until: "nashresult is not failed"
      retries: 3
      ansible.builtin.get_url:
        mode: '0644'
        url: "{{ nash_domino_service_script_git_repo }}/v{{ nash_domino_service_script_version }}/{{ nash_domino_service_script_archive }}"
        dest: "{{ installer_dir }}/domino/archives/domino-startscript_v{{ nash_domino_service_script_version }}.tar"
    
    -
      name: "Extracting Domino Service file Installer"
      when: not domino_server_nash_installed.stat.exists
      ansible.builtin.unarchive:
        src: "{{ installer_dir }}/domino/archives/domino-startscript_v{{ nash_domino_service_script_version }}.tar"
        dest: "{{ installer_dir }}/domino/service-file"
        remote_src: true
        creates: "{{ completed_dir }}/service_configured and not domino_server_nash_installed.stat.exists"
    
    -
      name: "Changing Nash's service installer script's default Datadir"
      when: not domino_server_nash_installed.stat.exists
      ansible.builtin.replace:
        path: "{{ installer_dir }}/domino/service-file/{{ domino_start_script_source_folder }}/sysconfig/rc_domino_config"
        regexp: "(^DOMINO_DATA_PATH=)(.*)$"
        replace: "DOMINO_DATA_PATH={{ domino_home_dir }}"
    
    -
      name: "Changing Nash's service installer script's default User"
      when: not domino_server_nash_installed.stat.exists
      ansible.builtin.replace:
        path: "{{ installer_dir }}/domino/service-file/{{ domino_start_script_source_folder }}/install_script"
        regexp: "(^  DOMINO_USER=notes)(.*)$"
        replace: "  DOMINO_USER={{ domino_user }}"
    
    -
      name: "Changing Nash's service installer script's default Domino Group"
      when: not domino_server_nash_installed.stat.exists
      ansible.builtin.replace:
        path: "{{ installer_dir }}/domino/service-file/{{ domino_start_script_source_folder }}/install_script"
        regexp: "(^  DOMINO_GROUP=notes)(.*)$"
        replace: "  DOMINO_GROUP={{ domino_group }}"
    
    -
      name: "Configuring Domino systemd service file"
      when: not domino_server_nash_installed.stat.exists
      ansible.builtin.shell: "{{ item }}"
      become: true
      args:
        chdir: "{{ installer_dir }}/domino/service-file/{{ domino_start_script_source_folder }}"
        executable: "/bin/bash"
        creates: "{{ completed_dir }}/service_configured"
      with_items:
        - "./install_script"
    
    -
      name: "Marking Nash Systemd Service as installed"
      when: not domino_server_nash_installed.stat.exists
      ansible.builtin.file:
        mode: '0644'
        path: "{{ item }}"
        state: touch
      with_items:
        - "{{ completed_dir }}/service_configured"
    
    -
      name: "Ensuring Domino is stopped but enabled at boot"
      when: not ansible_module_running_in_container
      become: true
      ansible.builtin.service:
        name: domino
        state: stopped
        enabled: true
