---
- name: Fetch bazel {{ bazel_version }} source
  ansible.builtin.get_url:
    url: "https://github.com/bazelbuild/bazel/releases/download/{{ bazel_version }}/bazel-{{ bazel_version }}-dist.zip"
    dest: "{{ build_dir }}"
    mode: 0600

- name: Create directory to build bazel {{ bazel_version }}
  ansible.builtin.file:
    path: "{{ build_dir }}/bazel-{{ bazel_version }}/"
    state: directory
    mode: 0700

- name: Unpack bazel {{ bazel_version }} source
  ansible.builtin.unarchive:
    src: "{{ build_dir }}/bazel-{{ bazel_version }}-dist.zip"
    dest: "{{ build_dir }}/bazel-{{ bazel_version }}"
    remote_src: true
    creates: "{{ build_dir }}/bazel-{{ bazel_version }}/compile.sh"

- name: Build bazel {{ bazel_version }}
  ansible.builtin.shell:
    cmd: |
      set -xe
      export BAZEL_LINKLIBS=-lstdc++
      export BAZEL_LINKOPTS=
      EXTRA_BAZEL_ARGS='--host_javabase=@local_jdk//:jdk' ./compile.sh
    chdir: "{{ build_dir }}/bazel-{{ bazel_version }}/"
    creates: "{{ build_dir }}/bazel-{{ bazel_version }}/output/bazel"
    executable: /bin/bash

- name: Install bazel {{ bazel_version }}
  become: true
  ansible.builtin.copy:
    src: "{{ build_dir }}/bazel-{{ bazel_version }}/output/bazel"
    dest: /usr/local/bin/bazel
    mode: 0755
    force: true
    remote_src: true
