# It isn't best practice to use this task
# Better load the repositories into /opt/docker/[servicename]/services, build them there and then use a docker-compose file for customizing
# @todo Refactor\Remove 
# @deprecated
- name: "Merge detached_files with applications | get_app_conf('web-app-oauth2-proxy','configuration_file')"
  set_fact:
    merged_detached_files: "{{ detached_files + [applications | get_app_conf('web-app-oauth2-proxy','configuration_file')] }}"
  when: "{{ applications | get_app_conf(application_id,'features.oauth2')"

- name: "backup detached files"
  command: >
    mv "{{ docker_compose.directories.instance }}{{ item }}" "/tmp/{{ application_id }}-{{ item }}.backup"
  args:
    removes: "{{ docker_compose.directories.instance }}{{ item }}"
  become: true
  loop: "{{ merged_detached_files | default(detached_files) }}"

- name: checkout repository 
  ansible.builtin.shell: git checkout .
  become: true
  args:
    chdir: "{{ docker_compose.directories.instance }}"
  ignore_errors: true

- name: "restore detached files"
  command: >
    mv "/tmp/{{ application_id }}-{{ item }}.backup" "{{ docker_compose.directories.instance }}{{ item }}"
  args:
    removes: "/tmp/{{ application_id }}-{{ item }}.backup"
  become: true
  loop: "{{ merged_detached_files | default(detached_files) }}"

- name: "copy {{ detached_files }} templates to server"
  template:
    src: "{{ item }}.j2"
    dest: "{{ docker_compose.directories.instance }}{{ item }}"
  loop: "{{ detached_files }}"
  notify: docker compose up
