---

- ansible.builtin.set_fact:
    _mysql_scripts_dir: "{{ mysql_scripts_dir | default(general_scripts_dir, True) | mandatory }}"

- ansible.builtin.include_role:
    name: evolix/remount-usr
  when: _mysql_scripts_dir is search("/usr")

- name: Scripts directory exists
  ansible.builtin.file:
    dest: "{{ _mysql_scripts_dir }}"
    mode: "0700"
    state: directory
  tags:
    - mysql

# mytop

- name: "mytop is installed (Debian 9)"
  ansible.builtin.apt:
    name: mytop
    state: present
  tags:
    - packages
    - mytop
    - mysql
  when: ansible_distribution_release == "jessie"

# - name: "mysql-utilities are installed (Debian 9 or later)"
#   apt:
#     name: mysql-utilities
#   when: ansible_distribution_major_version is version('9', '>=')

- name: "mytop dependencies are installed (Buster)"
  ansible.builtin.apt:
    name:
      - libconfig-inifiles-perl
      - libdbd-mysql-perl
      - libdbi-perl
      - libterm-readkey-perl
      - libtime-hires-perl
  tags:
    - packages
    - mytop
    - mysql
  when: ansible_distribution_release == "stretch"

- name: "Install dependencies for mytop (Debian 10)"
  ansible.builtin.apt:
    name:
      - mariadb-client-10.3
      - libconfig-inifiles-perl
      - libterm-readkey-perl
  when: ansible_distribution_release == "buster"

- name: "Install dependencies for mytop (Debian 11 or later)"
  ansible.builtin.apt:
    name:
      - mariadb-client-10.5
      - libconfig-inifiles-perl
      - libterm-readkey-perl
  when: ansible_distribution_major_version is version('11', '>=')

- ansible.builtin.include_role:
    name: evolix/remount-usr
  tags:
    - mytop
    - mysql

- name: "mytop is installed (Debian 9 or later)"
  ansible.builtin.copy:
    src: mytop
    dest: /usr/local/bin/mytop
    mode: "0755"
    owner: root
    group: staff
    force: true
  tags:
    - mytop
    - mysql
  when: ansible_distribution_major_version is version('9', '>=')

- name: Read debian-sys-maint password
  ansible.builtin.shell:
    cmd: 'cat /etc/mysql/debian.cnf | grep -m1 "password = .*" | cut -d" " -f3'
  register: mysql_debian_password
  changed_when: False
  check_mode: no
  tags:
    - mytop
    - mysql

- name: mytop configuration is copied
  ansible.builtin.template:
    src: mytop-config.j2
    dest: /root/.mytop
    mode: "0600"
    force: true
  tags:
    - mytop
    - mysql

# mysqltuner

- ansible.builtin.include_role:
    name: evolix/remount-usr
  tags:
    - mysql
  when: _mysql_scripts_dir is search("/usr")

- name: mysqltuner is installed
  # copy:
  #   src: mysqltuner.pl
  #   dest: "{{ _mysql_scripts_dir }}/mysqltuner.pl"
  #   mode: "0700"
  ansible.builtin.apt:
    name: mysqltuner
    state: present
  tags:
    - mysql
    - mysqltuner

- name: aha is installed
  ansible.builtin.apt:
    name: aha
  tags:
    - mysql

# automatic optimizations

- ansible.builtin.include_role:
    name: evolix/remount-usr
  tags:
    - mysql
  when: _mysql_scripts_dir is search("/usr")

- name: mysql-optimize.sh is installed
  ansible.builtin.copy:
    src: mysql-optimize.sh
    dest: "{{ _mysql_scripts_dir }}/mysql-optimize.sh"
    mode: "0700"
  tags:
    - mysql

- name: "Cron dir for optimize is present"
  ansible.builtin.file:
    path: "/etc/cron.{{ mysql_cron_optimize_frequency | mandatory }}"
    state: directory
    mode: "0755"
    owner: root
    group: root
  tags:
    - mysql

- name: "Enable cron to optimize MySQL"
  ansible.builtin.file:
    src: "{{ _mysql_scripts_dir }}/mysql-optimize.sh"
    dest: /etc/cron.{{ mysql_cron_optimize_frequency | mandatory }}/mysql-optimize.sh
    state: link
  when: mysql_cron_optimize | bool
  tags:
    - mysql

- name: "Disable cron to optimize MySQL"
  ansible.builtin.file:
    dest: /etc/cron.{{ mysql_cron_optimize_frequency | mandatory }}/mysql-optimize.sh
    state: absent
  when: not (mysql_cron_optimize | bool)
  tags:
    - mysql

- name: "Cron dir for mysqltuner is present"
  ansible.builtin.file:
    path: "/etc/cron.{{ mysql_cron_mysqltuner_frequency | mandatory }}"
    state: directory
    mode: "0755"
    owner: root
    group: root
  tags:
    - mysql
    - mysqltuner

- name: "Enable mysqltuner in cron"
  ansible.builtin.copy:
    src: mysqltuner.cron.sh
    dest: /etc/cron.{{ mysql_cron_mysqltuner_frequency | mandatory }}/mysqltuner.sh
    mode: "0755"
  when: mysql_cron_mysqltuner | bool
  tags:
    - mysql
    - mysqltuner

- name: "Disable mysqltuner in cron"
  ansible.builtin.file:
    dest: /etc/cron.{{ mysql_cron_mysqltuner_frequency | mandatory }}/mysqltuner.sh
    state: absent
  when: not (mysql_cron_mysqltuner | bool)
  tags:
    - mysql
    - mysqltuner

# my-add.sh

- ansible.builtin.include_role:
    name: evolix/remount-usr
  when: _mysql_scripts_dir is search("/usr")

- name: Install my-add.sh
  ansible.builtin.copy:
    src: my-add.sh
    dest: "{{ _mysql_scripts_dir }}/my-add.sh"
    mode: "0700"
  tags:
    - mysql
