---

- block:

  - set_fact:
      pepwave_ip_address: "{{ lookup('vehicle_config', 'get_pepwave_ip_address') }}"

  - name: Check pepwave address
    fail:
      msg: "Pepwave address '{{ pepwave_ip_address }}' is not valid; must be '192.168.12.50', '192.168.129.1', '192.168.130.1', or null."
    when: pepwave_ip_address not in ('', '192.168.12.50', '192.168.129.1', '192.168.130.1')

  - set_fact:
      lte_gateway_ip_1: "192.168.12.51"
      lte_gateway_ip_2: "192.168.12.52"
      gateway: "192.168.12.50"
    when: pepwave_ip_address | length == 0

  - set_fact:
      lte_gateway_ip_1: "{{ pepwave_ip_address }}"
      lte_gateway_ip_2: "{{ pepwave_ip_address }}"
      gateway: "{{ '192.168.12.51' if pepwave_ip_address == '192.168.129.1'
              else '192.168.12.52' if pepwave_ip_address == '192.168.130.1'
              else pepwave_ip_address }}"
    when: pepwave_ip_address | length > 0

  - name: Check /etc/network/interfaces
    replace: &interfaces_task
      path: /etc/network/interfaces
      # Match the entire bond1 interface stanza
      regexp: 'auto bond1(.|\n)*post-up ip route add default via .* table table_tcu2'
      replace: |
        # This file may be modified during deploy by scripts/ansible/roles/cellular_routing
        source interfaces.d/bond1
    check_mode: yes
    register: interfaces_result

  - name: Check bond1 interface definition file
    template: &bond1_task
      src: bond1.j2
      dest: /etc/network/interfaces.d/bond1
      owner: root
      group: root
      mode: '644'
    check_mode: yes
    register: bond1_result

  # So that the proper pre-down/down/post-down scripts are run
  - name: Bring bond1 down
    command: "{{ item }}"
    with_items:
      - ifdown bond1
      - ifdown eth3.50
      - ifdown eth1.50
    when: interfaces_result.changed or bond1_result.changed

  - name: Edit /etc/network/interfaces
    replace: *interfaces_task

  - name: Edit bond1 interface definition file
    template: *bond1_task

  - name: Bring bond1 back up
    command: "{{ item }}"
    with_items:
      - ifup eth1.50
      - ifup eth3.50
      - ifup bond1
    when: interfaces_result.changed or bond1_result.changed

  when: zoox_compute_version is version('3.5', '>=')
  become: yes
