---
# This role assumes the existence of the node manager, which is setup using another role.
- name: get the private IP for the instance
  set_fact:
    node_rpc_ip: "{{ instance_facts.instances[0].network_interfaces[0].private_ip_address }}"
  when: provider == "aws" and public_rpc

- name: use the public IP as the RPC address on DO
  set_fact:
    node_rpc_ip: "{{ ansible_host }}"
  when: provider == "digital-ocean" and public_rpc

#
# Calculate the number of nodes to add
#
- name: check current number of node services
  become: True
  ansible.builtin.command: safenode-manager status --json
  register: node_status

- name: calculate current non-genesis node count
  set_fact:
    current_node_count: "{{ (node_status.stdout | from_json).nodes | selectattr('genesis', 'equalto', false) | list | length }}"
  when: node_status.stdout != ""

- name: calculate number of nodes to add
  set_fact:
    nodes_to_add: "{{ node_instance_count | int - (current_node_count | default(0)) | int }}"

# 
# Calculate the port range
#
- name: check if we need a port range
  set_fact:
    use_port_range: "{{ nodes_to_add | default(0) | int > 1 }}"
    rpc_port: "{{ initial_rpc_start_port | int + (current_node_count | default(0)) | int }}"
    metrics_port: "{{ initial_metrics_start_port | int + (current_node_count | default(0)) | int }}"
  when: nodes_to_add | default(0) | int > 0

- name: set default value for use_port_range
  set_fact:
    use_port_range: false
  when: use_port_range is not defined

- name: calculate start port
  set_fact:
    rpc_start_port: "{{ initial_rpc_start_port | int + (current_node_count | default(0)) | int }}"
    metrics_start_port: "{{ initial_metrics_start_port | int + (current_node_count | default(0)) | int }}"
  when: use_port_range | bool

- name: calculate end port
  set_fact:
    rpc_end_port: "{{ rpc_start_port | int + nodes_to_add | int - 1 | int }}"
    metrics_end_port: "{{ metrics_start_port | int + nodes_to_add | int - 1 | int }}"
  when: use_port_range

#
# Obtain private IP if make_vm_private is set
#
- name: get private ip of eth1
  shell: ip -4 addr show dev eth1 | grep inet | awk '{print $2}' | cut -d/ -f1
  register: private_ip_eth1
  when: make_vm_private

#
# Add the nodes
#
- name: add node services
  become: True
  ansible.builtin.command:
    # The `omit` filter is used to remove arguments that don't have values
    argv: "{{ command_args | reject('equalto', omit) | list }}"
  vars:
    command_args:
      - "{{ binary_dir }}/safenode-manager"
      - -v
      - add
      - "--count={{ nodes_to_add }}"
      - "--owner=maidsafe"
      - "--peer={{ genesis_multiaddr }}"
      - "--rpc-address={{ node_rpc_ip }}"
      - "--rewards-address={{ rewards_address }}"
      - "--max-archived-log-files={{ max_archived_log_files }}"
      - "--max-log-files={{ max_log_files }}"
      - "{{ ('--node-ip=' + private_ip_eth1.stdout) if make_vm_private else omit }}"
      - "{{ '--home-network' if make_vm_private else omit }}"
      - "{{ ('--rpc-port=' + rpc_port) if not use_port_range else omit }}"
      - "{{ ('--rpc-port=' + rpc_start_port + '-' + rpc_end_port) if use_port_range else omit }}"
      - "{{ ('--metrics-port=' + metrics_port) if not use_port_range else omit }}"
      - "{{ ('--metrics-port=' + metrics_start_port + '-' + metrics_end_port) if use_port_range else omit }}"
      - "{{ ('--log-format=' + log_format) if log_format is defined else omit }}"
      - "{{ ('--env=' + env_variables) if env_variables is defined else omit }}"
      - "{{ ('--version=' + version) if version is defined else ('--url=' + node_archive_url) }}"
      - "{{ evm_network_type }}"
      - "{{ ('--rpc-url=' + evm_rpc_url) if evm_network_type == 'evm-custom' else omit }}"
      - "{{ ('--payment-token-address=' + evm_payment_token_address) if evm_network_type == 'evm-custom' else omit }}"
      - "{{ ('--data-payments-address=' + evm_data_payments_address) if evm_network_type == 'evm-custom' else omit }}"
  when: nodes_to_add | default(0) | int > 0

- name: start the node services
  become: True
  command: safenode-manager -v start --interval 200