---

- ansible.builtin.set_fact:
    mysql_apt_config_package: mysql-apt-config_0.8.9-1_all.deb

- name: Set default MySQL version to 5.7
  ansible.builtin.debconf:
    name: mysql-apt-config
    question: mysql-apt-config/enable-repo
    value: mysql-5.7
    vtype: select

- name: MySQL APT config package is available
  ansible.builtin.copy:
    src: "{{ mysql_apt_config_package }}"
    dest: "/root/{{ mysql_apt_config_package }}"

- ansible.builtin.include_role:
    name: evolix/remount-usr

- name: MySQL APT config package is installed
  ansible.builtin.apt:
    deb: "/root/{{ mysql_apt_config_package }}"
    state: present
  register: mysql_apt_config_deb

- name: Open firewall for MySQL.com repository
  ansible.builtin.replace:
    name: /etc/default/minifirewall
    regexp: "^(HTTPSITES='((?!(repo\\.mysql\\.com|0\\.0\\.0\\.0)).)*)'$"
    replace: "\\1 repo.mysql.com'"
  notify: Restart minifirewall

- ansible.builtin.meta: flush_handlers

- ansible.builtin.include_role:
    name: evolix/remount-usr

- name: /usr/share/mysql exists
  ansible.builtin.file:
    dest: /usr/share/mysql/
    mode: "0755"
    owner: root
    group: root
    state: directory

- name: mysql-systemd-start scripts is installed
  ansible.builtin.copy:
    src: debian/mysql-systemd-start
    dest: /usr/share/mysql/mysql-systemd-start
    mode: "0755"
    owner: root
    group: root
    force: true

- name: systemd unit is installed
  ansible.builtin.copy:
    src: debian/mysql-server-5.7.mysql.service
    dest: /etc/systemd/system/mysql.service
    mode: "0644"
    owner: root
    group: root
    force: true
  register: mysql_systemd_unit

- name: APT cache is up-to-date
  ansible.builtin.apt:
    update_cache: yes
  when: mysql_apt_config_deb is changed

- name: Install MySQL packages
  ansible.builtin.apt:
    name:
      - mysql-server
      - mysql-client
    update_cache: yes
    state: present
  tags:
    - mysql
    - packages

- name: Install MySQL dev packages
  ansible.builtin.apt:
    name: libmysqlclient20
    update_cache: yes
    state: present
  tags:
    - mysql
    - packages
  when: mysql_install_libclient | bool

- name: MySQL is started
  ansible.builtin.systemd:
    name: mysql
    daemon_reload: yes
    state: started
  tags:
    - mysql
    - services

- name: apg package is installed
  ansible.builtin.apt:
    name: apg
    state: present
  tags:
    - mysql
    - packages
