---

- name: Set default install mode
  set_fact: bb_install_mode='none'
  tags: bb

- name: Check if bb is installed
  stat: path=/usr/local/bin/bb
  register: bb_bin
  changed_when: False
  tags: bb

- name: Set install mode to install
  set_fact: bb_install_mode='install'
  when: not bb_bin.stat.exists
  tags: bb

- name: Detect installed version
  block:
    - shell: bb --version | perl -ne 'm/babashka\s+v(\d+(\.\d+)*)/ && print "$1"'
      changed_when: False
      register: bb_current_version
    - set_fact: bb_current_version={{ bb_current_version.stdout }}
  tags: bb

- name: Set install mode to upgrade
  set_fact: bb_install_mode='upgrade'
  when: bb_current_version != bb_version
  tags: bb

