# - name: "Set 'apache_install: True' and 'apache_enabled: True'"
#   set_fact:
#     apache_install: True
#     apache_enabled: True
#
# - name: APACHE - run 'httpd' role
#   include_role:
#     name: httpd


# 2020-06-15: roles/httpd/tasks/install.yml now takes care of this.
# # 2020-05-21: Required now that mysql/tasks/install.yml installs
# # "php{{ php_version }}-common" rather than the full "php{{ php_version }}"
# - name: "Install package: libapache2-mod-php{{ php_version }}"
#   package:
#     name: "libapache2-mod-php{{ php_version }}"

# Assume (enforce?) MySQL is running
#
# - name: "Set 'mysql_install: True' and 'mysql_enabled: True'"
#   set_fact:
#     mysql_install: True
#     mysql_enabled: True
#
# - name: MYSQL - run 'mysql' role (attempt to install & enable MySQL)
#   include_role:
#     name: mysql


- name: Download {{ iiab_download_url }}/elgg-{{ elgg_version }}.zip to {{ downloads_dir }}
  #shell: wget {{ iiab_download_url }}/elgg-{{ elgg_version }}.zip -c -P {{ downloads_dir }}
  #args:
  #  creates: "{{ downloads_dir }}/elgg-{{ elgg_version }}.zip"
  get_url:
    url: "{{ iiab_download_url }}/elgg-{{ elgg_version }}.zip"
    dest: "{{ downloads_dir }}"    # /opt/iiab/downloads
    timeout: "{{ download_timeout }}"
  when: internet_available

- name: Check for existence of /opt/elgg-{{ elgg_version }}/index.php
  stat:
    path: "/opt/elgg-{{ elgg_version }}/index.php"
  register: elgg

- name: Unpack (unarchive) .zip to /opt, if above index.php doesn't exist ({{ apache_user }}:{{ apache_user }})
  #shell: "/usr/bin/unzip -o {{ downloads_dir }}/elgg-{{ elgg_version }}.zip -d /opt"
  unarchive:
    #remote_src: yes
    #src: "{{ iiab_download_url }}/elgg-{{ elgg_version }}.zip"
    src: "{{ downloads_dir }}/elgg-{{ elgg_version }}.zip"
    dest: /opt
    owner: "{{ apache_user }}"
    group: "{{ apache_user }}"
  when: elgg.stat.exists is defined and not elgg.stat.exists

- name: Symlink /opt/elgg -> /opt/elgg-{{ elgg_version }}
  file:
    src: "./elgg-{{ elgg_version }}"
    path: /opt/elgg
    owner: "{{ apache_user }}"
    group: "{{ apache_user }}"
    state: link
    force: yes

- name: 'Install /opt/elgg/elgg-config/settings.php from template (WARNING: overwrites manual settings!)'
  template:
    src: "settings.php.j2"
    dest: "/opt/{{ elgg_xx }}/elgg-config/settings.php"
    owner: "{{ apache_user }}"
    group: "{{ apache_user }}"

# The name of this file changed from 1.9 to 1.10.
- name: Copy default .htaccess into /opt/{{ elgg_xx }}, root of Elgg tree (0644)
  copy:
    src: "/opt/{{ elgg_xx }}/vendor/elgg/elgg/install/config/htaccess.dist"
    dest: "/opt/{{ elgg_xx }}/.htaccess"
    owner: "{{ apache_user }}"
    group: "{{ apache_user }}"
    mode: 0644

#regexp='^#RewriteBase'
- name: Change .htaccess to include RewriteBase for http://box{{ elgg_url }}    # http://box/elgg
  lineinfile:
    backup: no
    path: "/opt/{{ elgg_xx }}/.htaccess"
    state: present
    insertafter: '^#RewriteBase'
    line: "RewriteBase {{ elgg_url }}/"

- name: Set /opt/elgg/engine directory permissions to 0755 (owner {{ apache_user }}) so Apache can write there
  file:
    state: directory
    path: /opt/elgg/engine/
    owner: "{{ apache_user }}"
    mode: 0755

- name: Change /opt/elgg-{{ elgg_version }} ownership to {{ apache_user }}:{{ apache_user }} (likely not nec, as unarchive & all do this above)
  file:
    state: directory    # Overkill given recurse below?
    path: "/opt/elgg-{{ elgg_version }}"
    owner: "{{ apache_user }}"
    group: "{{ apache_user }}"
    recurse: yes

- name: Create upload directory {{ elgg_upload_path }} that Apache (and Elgg) can write to
  file:
    path: "{{ elgg_upload_path }}"    # /library/elgg
    state: directory
    owner: "{{ apache_user }}"

# - name: Install /etc/{{ apache_conf_dir }}/elgg.conf from template, for http://box{{ elgg_url }}    # http://box/elgg
#   template:
#     src: elgg.conf
#     dest: "/etc/{{ apache_conf_dir }}/elgg.conf"


- name: Set up Elgg's MySQL database
  include_tasks: setup.yml


# RECORD Elgg AS INSTALLED

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

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