- name: Download {{ phpmyadmin_dl_url }} to {{ downloads_dir }}
  get_url:
    url: "{{ phpmyadmin_dl_url }}"    # e.g. https://files.phpmyadmin.net/phpMyAdmin/5.0.4/phpMyAdmin-5.0.4-all-languages.zip
    dest: "{{ downloads_dir }}"    # /opt/iiab/downloads
    timeout: "{{ download_timeout }}"

- name: Does {{ downloads_dir }}/{{ phpmyadmin_name_zip }} exist?
  stat:
    path: "{{ downloads_dir }}/{{ phpmyadmin_name_zip }}"    # e.g. /opt/iiab/downloads/phpMyAdmin-5.0.4-all-languages.zip
  register: phpmyadmin_dl


- name: Unarchive {{ phpmyadmin_dl_url }} (7+ MB) to /opt (60+ MB)
  unarchive:
    remote_src: yes    # Overwrite even if "already exists on the target"
    src: "{{ phpmyadmin_dl_url }}"    # e.g. https://files.phpmyadmin.net/phpMyAdmin/5.1.1/phpMyAdmin-5.1.1-all-languages.tar.xv
    dest: /opt
    owner: "{{ apache_user }}"    # www-data on debuntu
    group: "{{ apache_user }}"
    #timeout: "{{ download_timeout }}"    # Use get_url if timeout (100s) required

- name: Symlink /opt/phpmyadmin -> {{ phpmyadmin_name }}
  file:
    src: "{{ phpmyadmin_name }}"
    path: /opt/phpmyadmin
    #owner: "{{ apache_user }}"    # Generally not used by Linux
    #group: "{{ apache_user }}"    # Generally not used by Linux
    state: link

- name: Install /opt/phpmyadmin/config.inc.php owned by {{ apache_user }}:{{ apache_user }}, from template
  template:
    src: config.inc.php
    dest: /opt/phpmyadmin/config.inc.php
    owner: "{{ apache_user }}"
    group: "{{ apache_user }}"

# Above 3 stanzas set link/tree/contents ownership to {{ apache_user }}:root
# OOPS: CHOWN BELOW CHANGED LINK ALONE (TREE/CONTENTS REMAINED root:root)

# - name: Change the owner of the PHP tree to Apache
#   shell: "chown -R {{ apache_user }} /opt/phpmyadmin"
#   #file:
#   #  path: "/opt/{{ phpmyadmin_name_zip }}"
#   #  owner: "{{ apache_user }}"
#   #  recurse: yes
#   #  state: directory


# RECORD phpMyAdmin AS INSTALLED

- name: "Set 'phpmyadmin_installed: True'"
  set_fact:
    phpmyadmin_installed: True

- name: "Add 'phpmyadmin_installed: True' to {{ iiab_state_file }}"
  lineinfile:
    path: "{{ iiab_state_file }}"    # /etc/iiab/iiab_state.yml
    regexp: '^phpmyadmin_installed'
    line: 'phpmyadmin_installed: True'
