---
- block:
  # NOTE on PPA keys: our Ubuntu 20.04 (focal) image does not have a valid
  # /etc/resolv.conf when this role runs *if* the machine performing the build
  # is Ubuntu 14.04 because /etc/resolv.conf links to systemd's stub-resolv.conf
  # in /run. Instead of downloading from a keyserver, find the public key with
  # `apt-key export` and put it into a file.

  - name: Copy the new sources list
    template:
      src: "{{ os_release_name }}-sources.list"
      dest: /etc/apt/sources.list
      mode: '644'

  # Check and enable i386 compatibility
  - name: Check if i386 is enabled
    shell: dpkg --print-foreign-architectures | grep i386
    register: result_i386_check
    changed_when: result_i386_check.rc == 1
    failed_when: result_i386_check.rc > 1

  - name: Enable i386 architecture
    command: dpkg --add-architecture i386
    when: result_i386_check.rc == 1

  - name: Install debug symbols apt repository key
    apt_key:
      data: "{{ lookup('file', 'dbgsym-release-key.asc') }}"
      state: present

  - name: Add debug symbols apt repository
    apt_repository:
      repo: "deb [arch=amd64] {{ apt_mirror_base_url }}/ddebs.ubuntu.com/ {{ os_release_name }} main restricted universe multiverse"
      state: present
      filename: "ddebs"

  - name: Add debug symbols updates apt repository
    apt_repository:
      repo: "deb [arch=amd64] {{ apt_mirror_base_url }}/ddebs.ubuntu.com/ {{ os_release_name }}-updates main restricted universe multiverse"
      state: present
      filename: "ddebs_updates"

  - name: Install testing toolchain apt repository key
    apt_key:
      data: "{{ lookup('file', 'ubuntu-toolchain-r-test.asc') }}"
      state: present

  - name: Add testing toolchain apt repository
    apt_repository:
      repo: "deb [arch=amd64] {{ apt_mirror_base_url }}/ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu {{ os_release_name }} main"
      state: present
      filename: "ubuntu-toolchain-r-test"

  - name: Add testing toolchain apt repository
    apt_repository:
      repo: "deb [arch=i386] {{ apt_mirror_base_url }}/ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu {{ os_release_name }} main"
      state: present
      filename: "ubuntu-toolchain-r-test"

  - name: Add deadsnakes PPA signature
    apt_key:
      state: present
      data: "{{ lookup('file', 'deadsnakes.gpg.key') }}"

  - name: Add deadsnakes PPA
    apt_repository:
      repo: "deb [arch=amd64] {{ apt_mirror_base_url }}/ppa.launchpad.net/deadsnakes/ppa/ubuntu {{ os_release_name }} main"
      state: present
      filename: "deadsnakes"

  - include_tasks: trusty_sources.yml
    when: "os_release_name == 'trusty'"

  - name: Zoox Artifactory key
    apt_key:
      data: "{{ lookup('file', 'zoox-artifactory.gpg.key') }}"
      state: present

  # Uses Zoox Artifactory key
  - name: Zoox PKI PPA
    apt_repository:
      repo: "deb https://artifactory.zooxlabs.com/artifactory/sec-zoox generic private"
      filename: zoox_pki

  # Uses Zoox Artifactory key
  - name: Add the Zoox Product Security apt repository
    apt_repository:
      repo: "deb [arch=amd64] {{ apt_mirror_base_url }}/prod-sec trusty main"
      state: present
      filename: zoox-product-security

  # Docker setup block - Only runs on specified compute versions
  - block:

    # Docker repo key
    - name: Add Docker apt key
      apt_key:
        url: "https://download.docker.com/linux/ubuntu/gpg"

    # we need our local mirror for trusty as the public one is no longer hosted.
    - name: Add Docker apt repo for trusty
      apt_repository:
        repo: "deb [arch=amd64] http://apt-mirror.zooxlabs.com/mirror/download.docker.com/linux/ubuntu trusty stable"
      when:  ansible_distribution_version is version('15.00', '<')

    - name: Add Docker apt repo for xenial, bionic, focal
      apt_repository:
        repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
      when: ansible_distribution_version is version('15.00', '>=')

    when: zoox_compute_version|default('') == "IK.1"

  # run apt-get update at the very end
  - name: Update apt cache
    apt:
      update_cache: yes
      cache_valid_time: "{{ apt_cache_valid_time }}"

  become: yes
