---
- name: "Running context"
  debug:
    msg: >-
      Running Ansible on {{ inventory_hostname }} with OS {{ ansible_distribution }} {{ ansible_distribution_release }}
      {{ ansible_distribution_version }} {{ ansible_architecture }} ({{ app_env }})

- name: "Shut down all services"
  service:
    name: "{{ uninstall_svc_to_stop }}"
    state: stopped
  loop:
    - "influxdb"
    - "mysql"
    - "php7.4-fpm"
    - "nginx"
    - "redis-server"
    - "supervisor"
  loop_control:
    loop_var: uninstall_svc_to_stop
  ignore_errors: true

- name: Kill all processes owned by AzuraCast user
  command: pkill -9 -u azuracast
  ignore_errors: true

- name: Update apt
  apt:
    update_cache: true

- name: Remove AzuraCast User
  user:
    name: azuracast
    state: absent

- name: "Remove AzuraCast folders"
  file:
    path: "{{ uninstall_file_to_delete }}"
    state: absent
  loop:
    - "{{ tmp_base }}"
    - "{{ app_base }}/servers"
  loop_control:
    loop_var: uninstall_file_to_delete

- name: Remove PPAs
  apt_repository:
    repo: "{{ uninstall_repo_to_delete }}"
    state: absent
  loop:
    - "ppa:avsm/ppa"
  loop_control:
    loop_var: uninstall_repo_to_delete

- name: UFW - Turn off Firewall
  ufw:
    state: disabled
  ignore_errors: true

- name: Remove software
  apt:
    name: "{{ packages }}"
    state: absent
    force: true
    purge: true
  vars:
    packages:
      # Radio software
      - icecast2
      - liquidsoap
      - liquidsoap-plugin-*
      - libxml2
      - libxslt1-dev
      - libvorbis-dev
      - libssl-dev
      - libcurl4-openssl-dev
      - opam
      - libpcre3-dev
      - libfdk-aac-dev
      - libmad0-dev
      - libmp3lame-dev
      - libtag1-dev
      - libfaad-dev
      - libflac-dev
      - libogg-dev
      - libopus-dev
      - m4
      - aspcud
      - camlp4

      # Supervisord
      - supervisor

      # InfluxDB
      - influxdb

      # Nginx
      - nginx
      - nginx-*

      # PHP 7.2
      - php7.2-*
      - php7.4-*

      # MariaDB
      - mariadb-*

      # Redis
      - redis-server

      # UFW Firewall
      - ufw

      # Ansible itself
      - python2.7
      - python-pip
      - python-mysqldb
      - ansible

      # System packages
      - pwgen
      - lnav
