---

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

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

- name: Ensure scripts directory exists
  file:
    dest: "{{ _mysql_scripts_dir }}"
    mode: "0700"
    state: directory
  tags:
    - mysql

# mytop

- name: "Install mytop (jessie)"
  apt:
    name: mytop
    state: present
  tags:
    - packages
    - mytop
    - mysql
  when: ansible_distribution_release == "jessie"

- name: "Install dependencies for mytop (stretch)"
  apt:
    name:
      - mariadb-client-10.1
      - libconfig-inifiles-perl
      - libterm-readkey-perl
  when: ansible_distribution_release == "stretch"

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

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

- name: Configure mytop
  template:
    src: mytop.j2
    dest: /root/.mytop
    mode: "0600"
    force: yes
  tags:
    - mytop
    - mysql

# mysqltuner

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

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

- name: Install aha
  apt:
    name: aha
  tags:
    - mysql

# Percona Toolkit

- name: "Install percona-toolkit (Debian 9 or later)"
  apt:
    name: percona-toolkit
    state: present
  tags:
    - packages
    - percona-toolkit
    - mysql
  when: ansible_distribution_major_version is version('9', '>=')

# automatic optimizations

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

- name: Optimize script for MySQL
  copy:
    src: mysql-optimize.sh
    dest: "{{ _mysql_scripts_dir }}/mysql-optimize.sh"
    mode: "0700"
  tags:
    - mysql

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

- name: "Enable cron to optimize MySQL"
  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"
  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"
  file:
    path: "/etc/cron.{{ mysql_cron_mysqltuner_frequency | mandatory }}"
    state: directory
    mode: "0755"
    owner: root
    group: root

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

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

# my-add.sh

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

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

- name: Install apg
  apt:
    name: apg
  tags:
    - mysql
    - packages

- name: "Install save_mysql_processlist.sh"
  copy:
    src: save_mysql_processlist.sh
    dest: "{{ _mysql_scripts_dir }}/save_mysql_processlist.sh"
    mode: "0755"
    force: no
  tags:
    - mysql
