---
-
  name: "Creating installation directories for NomadWeb"
  ansible.builtin.file:
    mode: '0755'
    path: "{{ item }}"
    state: directory
  with_items:
    - "{{ installer_dir }}/nomadweb/archives"
    - "{{ installer_dir }}/nomadweb/NomadWeb"

-
  name: "Checking if NomadWeb is installed: {{ nomadweb_version }}"
  register: nomadweb_installed_check
  ansible.builtin.stat:
    path: "{{ completed_dir }}/nomadweb_install"
    get_md5: false

-
  name: "Checking if the NomadWeb installer is at nomadweb/archives/{{ nomadweb_archive }}"
  register: nomad_archive_check
  ansible.builtin.stat:
    path: "{{ installer_dir }}/nomadweb/archives/{{ nomadweb_archive }}"
    get_md5: false

-
  name: "Downloading NomadWeb from {{ domino_installer_base_url }}"
  register: nomadwebresult
  until: "nomadwebresult is not failed"
  retries: 3
  ansible.builtin.get_url:
    mode: '0755'
    url: "{{ domino_installer_base_url }}/ND{{ domino_major_version }}/NomadWeb/{{ nomadweb_archive }}"
    dest: "{{ installer_dir }}/nomadweb/archives/{{ nomadweb_archive }}"
    username: "{{ domino_installer_url_user }}"
    password: "{{ secrets.domino_installer_url_pass }}"
  when: not nomad_archive_check.stat.exists and not nomadweb_installed_check.stat.exists

-
  name: "Extracting NomadWeb from {{ nomadweb_archive }}"
  when: not nomadweb_installed_check.stat.exists
  ansible.builtin.unarchive:
    mode: "a+x"
    owner: "{{ domino_user }}"
    group: "{{ domino_group }}"
    src: "{{ installer_dir }}/nomadweb/archives/{{ nomadweb_archive }}"
    dest: "{{ installer_dir }}/nomadweb/NomadWeb"
    creates: "{{ installer_dir }}/nomadweb/NomadWeb/nwsp-linux"
    remote_src: true

-
  name: "Copying Nomadweb installer files to {{ domino_install_dir }}"
  when: not nomadweb_installed_check.stat.exists
  ansible.builtin.copy:
    mode: "a+x"
    src: "{{ installer_dir }}/nomadweb/NomadWeb/"
    dest: "{{ domino_install_dir }}"

-
  name: "Configuring NomadWeb"
  when: "not nomadweb_installed_check.stat.exists"
  ansible.builtin.shell: "{{ item }}"
  become: true
  args:
    executable: "/bin/bash"
  register: nomadweb_config_status
  with_items:
    - 'domino cmd "set config NOMAD_WEB_HOST={{ domino_nomadweb_proxy_url }}" 20'

-
  name: "Configuring NomadWeb"
  when: not nomadweb_installed_check.stat.exists
  ansible.builtin.template:
    dest: "{{ domino_home_dir }}/nomad-config.yml"
    mode: "a+x"
    owner: "{{ domino_user }}"
    group: "{{ domino_group }}"
    src: "nomad-config.yml.j2"

-
  name: "Outputting NomadWeb Configuration Status Logs"
  when: ( nomadweb_debug or debug_all ) and not nomadweb_installed_check.stat.exists
  ansible.builtin.debug:
    var: nomadweb_config_status

-
  name: "Stopping Domino for Changes to take effect"
  when: not nomadweb_installed_check.stat.exists
  become: true
  ansible.builtin.service:
    name: domino
    state: stopped
    enabled: true
  register: domino_service_stop_details
  until: domino_service_stop_details.state == "stopped"
  retries: 3
  delay: 5

-
  name: "Starting Domino for Changes to take effect"
  become: true
  when: not nomadweb_installed_check.stat.exists
  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: "Checking NomadWeb is listening on port {{ domino_nomadweb_port_forwards[0].guest }}"
  ansible.builtin.wait_for:
    port: "{{ domino_nomadweb_port_forwards[0].guest }}"
    delay: 5
    timeout: 60
    msg: "Timeout waiting for {{ domino_nomadweb_port_forwards[0].guest }} to respond"
  register: port_check

-
  name: "Configuring NomadWeb and Starting Service"
  when: not nomadweb_installed_check.stat.exists
  ansible.builtin.file:
    mode: '0644'
    path: "{{ item }}"
    state: touch
  with_items:
    - "{{ completed_dir }}/nomadweb_install"
