- name: Install Docker if it isn't present
  import_playbook: setup-docker.yml

- name: Set up Stack Orchestrator
  hosts: "{{ target_host }}"

  vars:
    target_host: "localhost"

  environment:
    PATH: "{{ ansible_env.PATH }}:/home/{{ansible_user}}/bin"

  tasks:
    - name: Check if Stack Orchestrator is installed
      shell: which laconic-so
      register: is_so_present
      ignore_errors: yes

    - block:
        - name: Exit if Stack Orchestrator is present
          debug:
            msg: "Stack Orchestrator already on host, ending play"
        - meta: end_play
      when: is_so_present.rc == 0

    - name: Install jq
      apt:
        name: jq
        state: present
        update_cache: yes
      become: yes

    - name: Ensure that directory ~/bin exists and is writable
      file:
        path: "{{ ansible_env.HOME }}/bin"
        state: directory
        mode: '0755'

    - name: Download the laconic-so binary
      get_url:
          url: https://git.vdb.to/cerc-io/stack-orchestrator/releases/download/latest/laconic-so
          dest: "{{ ansible_env.HOME }}/bin/laconic-so"
          mode: '0755'
          force: yes

    - name: Ensure ~/.laconic-so directory exists
      file:
        path: "{{ ansible_env.HOME }}/.laconic-so"
        state: directory
        mode: '0755'

    - name: Save the distribution url to ~/.laconic-so directory
      copy:
        dest: "{{ ansible_env.HOME }}/.laconic-so/config.yml"
        content: |
          distribution-url: https://git.vdb.to/cerc-io/stack-orchestrator/releases/download/latest/laconic-so
        mode: '0644'