---
- name: install required dependencies
  yum:
    name:
      - tar
      - gzip
    state: present

- name: create installation directory
  file:
    path: "{{ phoebus_installation_dir }}"
    state: directory
    owner: root
    group: root
    mode: 0755

- name: install Phoebus
  unarchive:
    src: "{{ phoebus_archive }}"
    dest: "{{ phoebus_installation_dir }}"
    remote_src: true
    owner: root
    group: root
    creates: "{{ phoebus_home }}"

- name: add Phoebus settings
  template:
    src: phoebus.ini.j2
    dest: "{{ phoebus_settings_path }}"
    owner: root
    group: root
    mode: 0644

- name: add MIME types
  copy:
    src: phoebus.xml
    dest: /usr/share/mime/packages
    owner: root
    group: root
    mode: 0644
  notify: Update MIME database

- name: create phoebus startup script
  template:
    src: phoebus.j2
    dest: /usr/local/bin/phoebus
    owner: root
    group: root
    mode: 0755

- name: add Phoebus to desktop menu
  copy:
    src: phoebus.desktop
    dest: /usr/share/applications/phoebus.desktop
    owner: root
    group: root
    mode: 0644

- name: Get all installations
  find:
    paths: "{{ phoebus_installation_dir }}"
    pattern: "ess-phoebus-*"
    file_type: directory
  register: phoebus_installations

- name: Remove obsolete installations
  file:
    path: "{{ item.path }}"
    state: absent
  loop: "{{ (phoebus_installations.files | sort(attribute='ctime', reverse=True))[phoebus_installations_keep:] }}"

- name: install Python environment
  conda:
    name: "{{ phoebus_python_packages }}"
    state: present
    channels:
      - ics-conda
      - conda-forge
      - anaconda-main
    environment: csstudio
  when: phoebus_python_packages != []
  tags: phoebus-python-env
