---
-
  name: "Block to Allow Loading of Variables without running task"
  when: run_tasks
  block:
    -
      name: "Creating installation directories for Leap"
      ansible.builtin.file:
        mode: '0644'
        path: "{{ item }}"
        state: directory
      with_items:
        - "{{ installer_dir }}/leap/archives"
        - "{{ installer_dir }}/leap/Leap"
    
    -
      name: "Checking if Leap installer is at leap/archives/{{ leap_archive }}"
      register: domino_server_installer_check
      ansible.builtin.stat:
        path: "{{ installer_dir }}/leap/archives/{{ leap_archive }}"
        get_md5: false
    
    -
      name: "Checking if Leap is installed: {{ leap_version }} "
      register: leap_installed_check
      ansible.builtin.stat:
        path: "{{ completed_dir }}/leap_install"
        get_md5: false
    
    -
      name: "Downloading Leap from {{ domino_installer_base_url }}"
      register: leapresult
      until: "leapresult is not failed"
      retries: 3
      ansible.builtin.get_url:
        mode: '0755' 
        validate_certs: false
        url: "{{ domino_installer_base_url }}/{{ installer_url_app_path }}/{{ leap_archive }}"
        dest: "{{ installer_dir }}/leap/archives/{{ leap_archive }}"
        username: "{{ domino_installer_url_user }}"
        password: "{{ secrets.domino_installer_url_pass }}"
      when: not domino_server_installer_check.stat.exists and not leap_installed_check.stat.exists
    
    -
      name: "Extracting Leap from {{ leap_archive }}"
      when: not leap_installed_check.stat.exists
      ansible.builtin.unarchive:
        mode: "a+x"
        owner: "{{ domino_user }}"
        group: "{{ domino_group }}"
        src: "{{ installer_dir }}/leap/archives/{{ leap_archive }}"
        dest: "{{ installer_dir }}/leap/Leap"
        creates: "{{ installer_dir }}/leap/Leap/Leap-{{ leap_version }}-for-domino-1201-linux"
        remote_src: true
    
    -
      name: "Stopping Domino for Leap Installation"
      when: not leap_installed_check.stat.exists
      become: true
      ansible.builtin.service:
        name: domino
        state: stopped
        enabled: true
    
    -
      name: "Configuring Leap and Starting Service"
      when: not leap_installed_check.stat.exists
      ansible.builtin.shell: "{{ item }}"
      become: true
      args:
        executable: "/bin/bash"
        chdir: "{{ installer_dir }}/leap/Leap/Leap-{{ leap_version }}/linux"
        creates: "{{ domino_install_dir }}/osgi/volt"
      with_items:
        - ./install silent
    
    -
      name: "Registering leap installation Output"
      when: not leap_installed_check.stat.exists and leap_version == "1.0.5"
      ansible.builtin.shell: cat /var/log/volt_install_*.log
      changed_when: false
      register: leap_install

    -
      name: "Registering leap installation Output"
      when: not leap_installed_check.stat.exists and not leap_version == "1.0.5"
      ansible.builtin.shell: cat /var/log/leap_install_*.log
      changed_when: false
      register: leap_install

    -
      name: "Outputting Leap installation logs"
      when: not leap_installed_check.stat.exists and ( leap_debug or debug_all )
      ansible.builtin.debug:
        var: leap_install.stdout_lines
    
    -
      name: "Starting Domino"
      ansible.builtin.service:
        name: domino
        state: started
        enabled: true
    
    -
      name: "Handing Leap/Volt ACL templated JSON to Genesis"
      when: not leap_installed_check.stat.exists
      ansible.builtin.template:
        dest: "{{ domino_home_dir }}/JavaAddin/Genesis/json/voltacl-org.json"
        mode: "a+x"
        owner: "{{ domino_user }}"
        group: "{{ domino_group }}"
        src: "voltacl-org.json.j2"
    
    -
      name: Waiting until Genesis returns OK
      when: not leap_installed_check.stat.exists
      ansible.builtin.wait_for:
        path: "{{ domino_home_dir }}/JavaAddin/Genesis/jsonresponse/voltacl-org.json"
    
    -
      name: "Checking if ACL applied succesfully"
      when: not leap_installed_check.stat.exists
      ansible.builtin.lineinfile:
        path: "{{ domino_home_dir }}/JavaAddin/Genesis/jsonresponse/voltacl-org.json"
        line: "OK"
        state: present
      check_mode: true
      register: presence
      failed_when: presence is changed
    
    -
      name: "Marking leap as installed"
      when: not leap_installed_check.stat.exists
      ansible.builtin.file:
        mode: '0644'
        path: "{{ item }}"
        state: touch
      with_items:
        - "{{ completed_dir }}/leap_install"
