---
- name: "Remount /usr if needed"
  ansible.builtin.include_role:
    name: remount-usr

- name: Previous autosysadmin restart directory is renamed
  command:
    cmd: mv "/usr/share/scripts/autosysadmin/auto" "{{ autosysadmin_agent_auto_dir }}"
    removes: "/usr/share/scripts/autosysadmin/auto"
    creates: "{{ autosysadmin_agent_auto_dir }}" 

- name: Create autosysadmin directories
  ansible.builtin.file:
    path: "{{ item }}"
    state: directory
    owner: "root"
    group: "root"
    mode: "0750"
  loop:
    - "{{ autosysadmin_agent_bin_dir }}"
    - "{{ autosysadmin_agent_lib_dir }}"
    - "{{ autosysadmin_agent_auto_dir }}"

- name: Copy libraries
  ansible.builtin.copy:
    src: "upstream/lib/"
    dest: "{{ autosysadmin_agent_lib_dir }}/"
    owner: root
    group: root
    mode: "0750"

- name: Copy repair scripts
  ansible.builtin.copy:
    src: "upstream/repair/"
    dest: "{{ autosysadmin_agent_bin_dir }}/"
    owner: root
    group: root
    mode: "0750"

- name: Copy other utilities
  ansible.builtin.copy:
    src: "upstream/bin/"
    dest: "{{ autosysadmin_agent_bin_dir }}/"
    owner: root
    group: root
    mode: "0750"

### WARNING: thos files are explicitly marked as non-executable
### to prevent them from being run automatically by run-parts

- name: Copy restart scripts
  ansible.builtin.copy:
    src: "upstream/restart/"
    dest: "{{ autosysadmin_agent_auto_dir }}/"
    owner: root
    group: root
    mode: "0640"

- name: Ensure /etc/evolinux folder exists
  ansible.builtin.file:
    path: "/etc/evolinux"
    state: directory
    owner: "root"
    group: "root"
    mode: "0700"

- name: Copy the configuration file if missing
  ansible.builtin.template:
    src: "autosysadmin.cf.j2"
    dest: "/etc/evolinux/autosysadmin"
    owner: root
    group: root
    mode: "0640"
    force: no

# Repair scripts are supposed to be 'on' by default
# A line "repair_XXX=off" is added to the file only if the script is to be disabled.
# That's why all the ternary logic for the state is reversed.
- name: Update value per variable
  ansible.builtin.lineinfile:
    dest: "/etc/evolinux/autosysadmin"
    line: "{{ item }}={{ autosysadmin_config[item] | default(true) | bool | ternary('on', 'off') }}"
    regexp: '^(#\s*)?{{ item }}=.*'
    state: "{{ autosysadmin_config[item] | default(true) | bool | ternary('absent', 'present') }}"
  register: _line
  loop: "{{ autosysadmin_repair_scripts | union(['repair_all']) }}"

- name: Ensure restart folder exists
  ansible.builtin.file:
    path: "auto"
    state: directory
    owner: "root"
    group: "root"
    mode: "0700"

- name: Legacy scripts are removed
  ansible.builtin.file:
    path: "{{ general_scripts_dir }}/autosysadmin/{{ item }}"
    state: absent
  loop:
    - repair_amavis.sh
    - repair_disk.sh
    - repair_elasticsearch.sh
    - repair_http.sh
    - repair_mysql.sh
    - repair_opendkim.sh
    - repair_php_fpm56.sh
    - repair_php_fpm70.sh
    - repair_php_fpm73.sh
    - repair_php_fpm74.sh
    - repair_php_fpm80.sh
    - repair_php_fpm81.sh
    - repair_redis.sh
    - repair_tomcat_instance.sh
