# Role "www_base" runs earlier, likely in 3-BASE-SERVER.
# Role "www_options" runs here, probably in 4-SERVER-OPTIONS.


# HOMEPAGE

- name: Create dir {{ doc_root }}{{ iiab_home_url }} just in case variable iiab_home_url was customized.  (Standard path {{doc_root}}/home was created earlier.)
  file:
    state: directory
    path: "{{ doc_root }}{{ iiab_home_url }}"    # /library/www/html/home
    owner: "{{ apache_user }}"
    group: "{{ apache_user }}"
    mode: '0755'

# Used to be run by httpd/tasks/install.yml
#- name: "IN CASE NGINX IS DISABLED: Enable IIAB pages via Apache (e.g. on port 80) if apache_install"
#  include_tasks: roles/httpd/tasks/homepage.yml
#  when: apache_installed is defined

# Used to be run by nginx/tasks/install.yml
- name: Enable IIAB pages via NGINX (e.g. on port 80) if nginx_install
  include_tasks: roles/nginx/tasks/homepage.yml
  when: nginx_installed is defined


# 2022-07-22: SIMILAR TO roles/iiab-admin/tasks/pwd-warnings.yml FOR passwords
#                    AND roles/network/tasks/netwarn.yml      FOR iiab-network

- name: Does /etc/xdg/lxsession/LXDE-pi/autostart exist? (if so, auto-launch browser on boot, displaying http://box/home IIAB home page)
  stat:
    path: /etc/xdg/lxsession/LXDE-pi/autostart
  register: lxde_pi_autostart_present

- name: Does /usr/bin/chromium exist? (check for browser filename change)
  stat:
    path: /usr/bin/chromium
  register: chromium_present

- name: Add chromium-browser to /etc/xdg/lxsession/LXDE-pi/autostart
  lineinfile:
    path: /etc/xdg/lxsession/LXDE-pi/autostart
    regexp: '^/usr/bin/chromium-browser'
    line: '/usr/bin/chromium-browser --disable-restore-session-state http://box/home'
  when: lxde_pi_autostart_present.stat.exists and not chromium_present.stat.exists

- name: Add chromium to /etc/xdg/lxsession/LXDE-pi/autostart
  lineinfile:
    path: /etc/xdg/lxsession/LXDE-pi/autostart
    regexp: '^/usr/bin/chromium'
    line: '/usr/bin/chromium --disable-restore-session-state http://box/home'
  when: lxde_pi_autostart_present.stat.exists and chromium_present.stat.exists


# 2022-12-29: php-settings.yml is ALSO attempted (on demand) by every
# <ROLE>/tasks/install.yml that needs it (Matomo, Moodle, Nextcloud, PBX,
# WordPress) so './runrole <ROLE>' and similar are fully self-sufficient!
- name: "Run php-settings.yml -- allows post-install toggling of nginx_high_php_limits in /etc/iiab/local_vars.yml -- if you run './runrole www_options'"
  include_tasks: php-settings.yml
  when: matomo_install or moodle_install or nextcloud_install or pbx_install or wordpress_install


# 'Is a "Rapid Power Off" button possible for low-electricity environments?'
# gives more details here: http://FAQ.IIAB.IO

# COMPARE nginx_high_php_limits further above.

# 2020-03-08: DOES THE FLAG BELOW (apache_allow_sudo) PRESUMABLY WORK
# WITH NGINX TOO ?  (The single-click poweroff button on IIAB's home
# page certainly does still work with NGINX.)

- name: Give {{ apache_user }} (per variable apache_user) permission to poweroff, installing /etc/sudoers.d/020_apache_poweroff from template
  template:
    src: 020_apache_poweroff.j2
    dest: /etc/sudoers.d/020_apache_poweroff
    mode: '0440'
  when: apache_allow_sudo

- name: Remove {{ apache_user }} (per variable apache_user) permission to poweroff, removing /etc/sudoers.d/020_apache_poweroff
  file:
    path: /etc/sudoers.d/020_apache_poweroff
    state: absent
  when: not apache_allow_sudo


# 2022-06-30: internet_available var removed
- name: 'Test for Internet access, using: {{ iiab_download_url }}/heart-beat.txt'
  get_url:
    url: "{{ iiab_download_url }}/heart-beat.txt"
    dest: /tmp/heart-beat.txt
    #timeout: "{{ download_timeout }}"
    # @jvonau recommends: 100sec is too much (keep 10sec default)
  ignore_errors: True
  #async: 10
  #poll: 2
  register: internet_access_test

- name: Remove downloaded Internet test file /tmp/heart-beat.txt
  file:
    path: /tmp/heart-beat.txt
    state: absent

- name: Run /usr/bin/iiab-refresh-wiki-docs (scraper script) to create http://box/info offline documentation.  (This script was installed in Stage 3 = roles/3-base-server/tasks/main.yml, which ran roles/www_base/tasks/main.yml)
  command: /usr/bin/iiab-refresh-wiki-docs
  when: not internet_access_test.failed and not nodocs


- name: (Re)Start '{{ apache_service }}' systemd service, if installed & enabled
  systemd:
    name: "{{ apache_service }}"    # apache2 on debuntu
    state: restarted
  when: apache_installed is defined and apache_enabled

- name: (Re)Start 'nginx' systemd service, if nginx_enabled
  systemd:
    name: nginx
    state: restarted
  when: nginx_enabled


# RECORD www_options AS INSTALLED

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

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