---
- block:
  - name: Remove apt pip and pip3
    apt:
      name:
        - python-pip
        - python3-pip
      state: absent
      cache_valid_time: "{{ apt_cache_valid_time }}"

  - name: Read pip2 version
    command: "pip2.7 --version"
    register: pip2_7_version
    changed_when: false
    ignore_errors: true

  - name: Read pip3.4 version
    command: "pip3.4 --version"
    register: pip3_4_version
    changed_when: false
    ignore_errors: true

  - name: Read pip3.6 version
    command: "pip3.6 --version"
    register: pip3_6_version
    changed_when: false
    ignore_errors: true

  # Set the pip_install fact to determine if any of the pip versions need to be
  # installed. If so, we install all of them in the declared order so `pip`,
  # `pip2`, and `pip3` are all the correct version.
  - name: Default pip_install to false
    set_fact:
      pip_install: false

  - name: Set pip_install
    vars:
      expected_3_6_pip_version: "pip {{ pip_version }} from /usr/local/lib/python3.6/dist-packages (python 3.6)"
      expected_3_4_pip_version: "pip {{ pip_version }} from /usr/local/lib/python3.4/dist-packages (python 3.4)"
      expected_2_7_pip_version: "pip {{ pip_version }} from /usr/local/lib/python2.7/dist-packages (python 2.7)"
    set_fact:
      pip_install: true
    when: (pip3_6_version is failed) or
          (pip3_6_version.stdout != expected_3_6_pip_version) or
          (pip3_4_version is failed) or
          (pip3_4_version.stdout != expected_3_4_pip_version) or
          (pip2_7_version is failed) or
          (pip2_7_version.stdout != expected_2_7_pip_version)

  - name: "Install pip for each Python version"
    script: "get-pip.py pip=={{ pip_version }} {{ item.extra_args }} --index-url https://pypi.zooxlabs.com/simple/ --force-reinstall"
    args:
      executable: "{{ item.executable }}"
    when: pip_install
    with_items:
      # The order here is important; those installed later will clobber some previously-installed symlinks.
      # `pip` should resolve to 2.7, and `pip3` to 3.4
      # Note: if setuptools is left unspecified, a default version that may not be compatible will be installed
      - { executable: "python3.6", extra_args: "" }
      - { executable: "python3.4", extra_args: "setuptools==38.2.4" }
      - { executable: "python2.7", extra_args: "setuptools==33.1.1" }

  become: yes
  when: os_release_name == 'trusty'

- block:
  - name: Install apt pip
    apt:
      name:
        - python3-pip

  - name: Read pip2.7 version
    command: "pip2.7 --version"
    register: pip2_7_version
    changed_when: false
    ignore_errors: true

  - name: Read pip3.8 version
    command: "pip3.8 --version"
    register: pip3_8_version
    changed_when: false
    ignore_errors: true

  - name: Read pip3.6 version
    command: "pip3.6 --version"
    register: pip3_6_version
    changed_when: false
    ignore_errors: true

  - name: Default pip_install to false
    set_fact:
      pip_install: false

  - name: Set pip_install
    vars:
      expected_2_7_pip_version: "pip {{ pip_version }} from /usr/local/lib/python2.7/dist-packages (python 2.7)"
      expected_3_6_pip_version: "pip {{ pip_version }} from /usr/local/lib/python3.6/dist-packages (python 3.6)"
      expected_3_8_pip_version: "pip {{ pip_version }} from /usr/local/lib/python3.8/dist-packages (python 3.8)"
    set_fact:
      pip_install: true
    when: (pip2_7_version is failed) or
          (pip2_7_version.stdout != expected_2_7_pip_version) or
          (pip3_6_version is failed) or
          (pip3_6_version.stdout != expected_3_6_pip_version) or
          (pip3_8_version is failed) or
          (pip3_8_version.stdout != expected_3_8_pip_version)

  # python2.7 doesn't come with pip, so we must use get_pip.py
  - name: Install pip for python2.7
    script: "get-pip.py pip=={{ pip_version }} {{ item.extra_args }} --force-reinstall"
    args:
      executable: "{{ item.executable }}"
    when: pip_install
    with_items:
      - { executable: "python2.7", extra_args: "setuptools==33.1.1" }

  # This uses the apt-provided pip and updates it instead of the get-pip.py script.
  # A newer version of get-pip.py is required for python3.8, but that version doesn't
  # support python3.4.
  - name: Install python pip for python3.6 and python3.8
    command: "{{ item.executable }} -m pip install pip=={{ pip_version }} --force-reinstall"
    when: pip_install
    with_items:
      - { executable: "python3.8" }
      - { executable: "python3.6" }

  become: yes
  when: os_release_name == 'focal'
