---
- name: Install supervisord from repo
  apt:
    name: supervisor
  register: supervisord_install

- name: Change supervisord configuration
  template:
    src: supervisord.conf.j2
    dest: /etc/supervisor/supervisord.conf
    force: true
    mode: 0644
  register: supervisord_conf

- name: Remove old core supervisord configuration
  file:
    path: /etc/supervisor/conf.d/core.conf
    state: absent
  register: supervisord_core

- name: Kill legacy radio processes (if upgrading from older version)
  command: >-
    killall -q -9 icecast2 sc_serv liquidsoap
  ignore_errors: true
  when: supervisord_install.changed

- name: Trigger manual restart of supervisord (if not already running)
  service:
    name: supervisor
    enabled: true
    state: restarted
  when: >-
    supervisord_install is changed
    or supervisord_conf is changed
    or supervisord_core is changed
  ignore_errors: true
