---
-
  name: "Block to Allow Loading of Variables without running task"
  when: run_tasks
  block:
    -
      name: "Block to Allow Loading of Variables without running task"
      when: not web_terminal_build_from_source
      block:
        -
          name: "Ensuring apt cache is updated"
          ansible.builtin.apt:
            cache_valid_time: 3600
            update_cache: true

        -
          name: "Upgrading all apt packages"
          ansible.builtin.apt:
            upgrade: dist
            update_cache: true

        -
          name: "Adding web-terminal"
          ansible.builtin.apt:
            name: web-terminal
            state: present

        -
          name: "Placing Configuration file"
          become: true
          ansible.builtin.template:
            src: "{{ item.src }}"
            dest: "{{ item.dest }}"
            mode: '0755'
          loop:
            - { src: 'config.yaml.j2', dest: '/etc/web-terminal/config.yaml' }
            - { src: 'config.yaml.j2', dest: '/etc/web-terminal/dev-config.yaml' }
            - { src: 'config.yaml.j2', dest: '/opt/web-terminal/dev-config.yaml' }
            - { src: 'config.yaml.j2', dest: '/opt/web-terminal/config.yaml' }

        -
          name: "Starting Web Terminal Service"
          when: not ansible_module_running_in_container
          ansible.builtin.service:
            name: web-terminal
            state: started
            enabled: true

        -
          name: "Copying Web Terminal supervisord configuration file"
          when: ansible_module_running_in_container
          ansible.builtin.template:
            src: supervisord.webterminal.conf.j2
            dest: /etc/supervisor/conf.d/supervisord.webterminal.conf
            owner: root
            group: root
            mode: '0644'

    -
      name: "Building Web Terminal from source"
      when: web_terminal_build_from_source
      block:

        -
          name: "Gathering facts on current container"
          community.docker.current_container_facts:

        -
          name: "Creating Installation Directories"
          ansible.builtin.file:
            path: "{{ item }}"
            state: directory
            mode: '0755'
            owner: "{{ service_user }}"
            group: "{{ service_group }}"
          with_items:
            - "{{ web_terminal_path }}"

        -
          name: "Installing Dependencies"
          ansible.builtin.apt:
            update_cache: yes

        - name: "Installing Node.js and npm"
          ansible.builtin.apt:
            name:
              - nodejs
              - npm
            state: present

        -
          name: "Placing Web Terminal Service file"
          become: true
          ansible.builtin.template:
            owner: "{{ item.owner }}"
            src: "{{ item.src }}"
            dest: "{{ item.dest }}"
            mode: '0644'
          loop:
            - { src: 'web-terminal.service.j2', dest: '/etc/systemd/system/web-terminal.service', owner: '{{ service_user }}' }

        -
          name: "Placing Configuration file"
          become: true
          ansible.builtin.template:
            owner: "{{ item.owner }}"
            src: "{{ item.src }}"
            dest: "{{ item.dest }}"
            mode: '0644'
          loop:
            - { src: 'config.yaml.j2', dest: '{{ web_terminal_path }}/config.yaml', owner: '{{ service_user }}' }

        -
          name: "Copying source files to /opt/web-terminal"
          ansible.builtin.copy:
            src: "web-terminal/{{ web_terminal_source_files }}"
            dest: "{{ web_terminal_path }}"
          loop:
            - client.js
            - index.html
            - logger.js
            - package.json
            - robots.txt
            - server.js
            - style.css
          loop_control:
            loop_var: web_terminal_source_files

        -
          name: "Installing packages based on package.json"
          community.general.npm:
            path: "{{ web_terminal_path }}"

        -
          name: "Starting Web Terminal Service"
          when: not ansible_module_running_in_container
          ansible.builtin.service:
            name: web-terminal
            state: started
            enabled: true

        -
          name: "Checking if Web Terminal Service is listening on port {{ startcloud_web_terminal_port_forwards[0].guest }}"
          when: not ansible_module_running_in_container
          ansible.builtin.wait_for:
            port: "{{ startcloud_web_terminal_port_forwards[0].guest }}"
            delay: 5
            timeout: 60
            msg: "Timeout waiting for {{ startcloud_web_terminal_port_forwards[0].guest }} to respond"
          register: port_check_web_terminal
          ignore_errors: true

        -
          name: "Copying Web Terminal supervisord configuration file"
          when: ansible_module_running_in_container
          ansible.builtin.template:
            src: supervisord.webterminal.conf.j2
            dest: /etc/supervisor/conf.d/supervisord.webterminal.conf
            owner: root
            group: root
            mode: '0644'
