---
-
  name: "Creating Installation Directories"
  ansible.builtin.file:
    mode: '0644'
    path: "{{ item }}"
    state: directory
    owner: "{{ service_user }}"
  with_items:
    - "{{ installer_dir }}/restapi/rest"
    - "{{ installer_dir }}/restapi/archives"

-
  name: "Checking if the Rest Interface deployed"
  register: rest_interface_deployed
  ansible.builtin.stat:
    path: "{{ rest_app_home_dir }}/environment"
    get_md5: false

-
  name: "Downloading Domino VagrantCRUD API {{ rest_vagrant_crud_version }}"
  when: not rest_interface_deployed.stat.exists
  ansible.builtin.get_url:
    mode: '0644'
    url: "https://github.com/Moonshine-IDE/Vagrant-REST-Interface/releases/download/{{ rest_vagrant_crud_version }}/{{ rest_vagrant_crud_archive }}"
    dest: "{{ installer_dir }}/restapi/archives/{{ rest_vagrant_crud_archive }}"

-
  name: "Extracting Domino VagrantCRUD API from {{ rest_vagrant_crud_archive }}"
  when: not rest_interface_deployed.stat.exists
  ansible.builtin.unarchive:
    src: "{{ installer_dir }}/restapi/archives/{{ rest_vagrant_crud_archive }}"
    dest: "{{ installer_dir }}/restapi/rest"
    remote_src: true
    creates: "{{ installer_dir }}/restapi/rest/*.jar"

-
  name: "Making Rest Libraries and Binaries executable"
  when: not rest_interface_deployed.stat.exists
  ansible.builtin.file:
    path: "{{ item }}"
    owner: "{{ service_user }}"
    mode: "a+x"
  with_items:
    - "{{ installer_dir }}/restapi/rest/provision.sh"
    - "{{ installer_dir }}/restapi/rest/always.sh"

-
  name: "Cleaning up first before installing Vagrant Rest interface"
  when: not rest_interface_deployed.stat.exists
  ansible.builtin.file:
    path: "{{ rest_app_home_dir }}"
    state: absent
    owner: "{{ service_user }}"
    mode: "0744"

-
  name: "Installing Vagrant Rest interface"
  when: not rest_interface_deployed.stat.exists
  ansible.builtin.file:
    path: "{{ item.path }}"
    state: directory
    owner: "{{ service_user }}"
    mode: "0744"
  with_items:
    - { path: "/opt/domino/scripts" }
    - { path: "{{ rest_app_home_dir }}" }
    - { path: "{{ rest_app_home_dir }}/bin" }
    - { path: "{{ rest_app_home_dir }}/log" }
    - { path: "{{ rest_app_home_dir }}/config" }

-
  name: "Copying Rest interface Jar to working path"
  become: true
  when: not rest_interface_deployed.stat.exists
  ansible.builtin.copy:
    src: "{{ installer_dir }}/restapi/rest/{{ rest_interface_jar }}"
    dest: "{{ rest_app_home_dir }}/bin/{{ rest_interface_jar }}"
    mode: "a+x"
    owner: "{{ service_user }}"
    remote_src: true

-
  name: "Placing Domino Specific Configuration and scripts"
  become: true
  when: domino_home_dir is defined and domino_install_dir is defined and not rest_interface_deployed.stat.exists
  ansible.builtin.template:
    owner: "{{ item.owner }}"
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: "a+x"
  loop:
    - { src: 'deploy_database.sh.j2', dest: '/opt/domino/scripts/deploy_database.sh', owner: '{{ service_user }}' }
    - { src: 'deploy_html.sh.j2', dest: '/opt/domino/scripts/deploy_html.sh', owner: '{{ service_user }}' }
    - { src: 'run_dxl_importer.sh.j2', dest: '/opt/domino/scripts/run_dxl_importer.sh', owner: '{{ service_user }}'}
    - { src: 'import_json_documents.sh.j2', dest: '/opt/domino/scripts/import_json_documents.sh', owner: '{{ service_user }}'}

-
  name: "Placing Configuration and scripts"
  become: true
  when: not rest_interface_deployed.stat.exists
  ansible.builtin.template:
    owner: "{{ item.owner }}"
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: "a+x"
  loop:
    - { src: 'rest_config.yml.j2', dest: '{{ rest_app_home_dir }}{{ rest_config_file }}', owner: '{{ service_user }}' }
    - { src: 'restapi.service.j2', dest: '/etc/systemd/system/restapi.service', owner: '{{ service_user }}' }
    - { src: 'environment.j2', dest: '{{ rest_app_home_dir }}/environment', owner: '{{ service_user }}' }

-
  name: "Starting Vagrant CRUD Rest API"
  when: not rest_interface_deployed.stat.exists
  ansible.builtin.service:
    name: restapi
    state: started
    enabled: true

-
  name: "Checking Vagrant CRUD Rest API is listening on port {{ domino_vagrant_rest_api_port_forwards[0].guest }}"
  when: not rest_interface_deployed.stat.exists
  ansible.builtin.wait_for:
    port: "{{ domino_vagrant_rest_api_port_forwards[0].guest }}"
    delay: 5
    timeout: 60
    msg: "Timeout waiting for {{ domino_vagrant_rest_api_port_forwards[0].guest }} to respond"
  register: port_check
