# Initialize

- name: ...IS BEGINNING ============================================
  stat:
    path: "{{ iiab_env_file }}"
  register: NewInstall

- name: Set first_run flag
  set_fact:
    first_run: True
  when: not NewInstall.stat.exists

# We need to inialize the ini file and only write the location and version
# sections once and only once to preserve the install date and git hash.
- name: Create IIAB directory structure and {{ iiab_ini_file }}, if first_run
  include_tasks: first_run.yml
  when: first_run | bool

#- name: Loading computed_vars
#  include_tasks: roles/0-init/tasks/computed_vars.yml
- name: Re-read local_facts.facts from /etc/ansible/facts.d
  setup:
    filter: ansible_local

- name: Set top-level variables from local_facts for convenience
  set_fact:
    xo_model: "{{ ansible_local.local_facts.xo_model }}"
    phplib_dir: "{{ ansible_local.local_facts.phplib_dir }}"
    iiab_stage: "{{ ansible_local.local_facts.stage }}"

- name: Discover if this is running Raspbian -- if so assume it is an RPi
  set_fact:
    rpi_model: "rpi"
    is_rpi: True
#      no_net_restart: True
#      nobridge: True
  when: ansible_local.local_facts.os == "raspbian"

- name: Set exFAT_enabled if xo_model != "none"
  set_fact:
    exFAT_enabled: True
  when: xo_model != "none"

# Discover: do we have a gateway?
# If Ansible detects gateway, becomes WAN candidate.
- name: "Do we have a gateway? If so set discovered_wan_iface: {{ ansible_default_ipv4.alias }}, iiab_wan_iface: {{ discovered_wan_iface }}"
  set_fact:
    discovered_wan_iface: "{{ ansible_default_ipv4.alias }}"
    iiab_wan_iface: "{{ discovered_wan_iface }}"
  when: ansible_default_ipv4.gateway is defined

- name: "Verify gateway active: ping -c4 {{ ansible_default_ipv4.gateway }}"
  shell: ping -c4 "{{ ansible_default_ipv4.gateway }}" | grep icmp_seq=4 | wc -l
  when: discovered_wan_iface != "none"
  register: gw_active_test

- name: If so, set gw_active
  set_fact:
    gw_active: True
  when: discovered_wan_iface != "none" and gw_active_test.stdout == "1"

- name: Test for Internet access ({{ 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: Set internet_available if download succeeded and not disregard_network
  set_fact:
    internet_available: True
  when: not internet_access_test.failed and not disregard_network

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

# Put all computed vars here so derive properly from any prior var file.
- name: If the TZ is not set in env, set it to UTC
  include_tasks: tz.yml

- name: Set port 80 for Admin Console if not adm_cons_force_ssl
  set_fact:
    gui_port: 80
  when: not adm_cons_force_ssl

- name: Set port 443 for Admin Console if adm_cons_force_ssl
  set_fact:
    gui_port: 443
  when: adm_cons_force_ssl | bool

- name: Turn on both vars for MySQL (mandatory in Stage 3!)
  set_fact:
    mysql_install: True
    mysql_enabled: True

# We decided to enable mysql unconditionally.
#  when: elgg_enabled or rachel_enabled or owncloud_enabled or phpmyadmin_enabled or wordpress_enabled or iiab_menu_install

# MongoDB is auto-included by Sugarizer as of Feb 2019, thanks to: roles/sugarizer/meta/main.yml
#
## Late 2017: Had commented out MongoDB on a trial basis, for a more basic/lightweight Sugarizer, per https://github.com/iiab/iiab/pull/427
#- name: Turn on both vars for MongoDB if sugarizer_enabled
#  set_fact:
#    mongodb_install: True
#    mongodb_enabled: True
#  when: sugarizer_enabled | bool

# There might be other db's
- name: Turn on both vars for PostgreSQL if moodle_enabled or pathagar_enabled
  set_fact:
    postgresql_install: True
    postgresql_enabled: True
  when: moodle_enabled or (pathagar_enabled is defined and pathagar_enabled)

#- name: Turn on vars for Docker if SchoolTool is to be installed
#  set_fact:
#    docker_install: True
#    docker_enabled: True
#  when: schooltool_enabled or schooltool_install

- name: "Set python_path: /lib/python2.7/site-packages/ (redhat)"
  set_fact:
    python_path: /lib/python2.7/site-packages/
  when: is_redhat | bool

- name: "Set python_path: /usr/local/lib/python2.7/dist-packages/ (debuntu)"
  set_fact:
    python_path: /usr/local/lib/python2.7/dist-packages/
  when: is_debuntu | bool

# For various reasons the mysql service cannot be enabled on Fedora 20, but
# 'mariadb', which is its real name can.  On Fedora 18 we need to use 'mysqld'.

# BETTER TO USE /opt/iiab/iiab/vars/<OS>.yml
#- name: "Set mysql_service: mariadb by default"
#  set_fact:
#    mysql_service: mariadb

- name: "Set mysql_service: mysqld etc (Fedora 18)"
  set_fact:
    # BETTER TO USE /opt/iiab/iiab/vars/<OS>.yml
    #mysql_service: mysqld
    no_NM_reload: True
    is_F18: True
  when: (ansible_distribution_release == "based on Fedora 18" or ansible_distribution_version == "18") and ansible_distribution == "Fedora"

# BETTER TO USE /opt/iiab/iiab/vars/<OS>.yml
#- name: "Set mysql_service: mysql (debuntu)"
#  set_fact:
#    mysql_service: mysql
#  when: is_debuntu | bool

- name: "Set iiab_fqdn: {{ iiab_hostname }}.{{ iiab_domain }}"
  set_fact:
    iiab_fqdn: "{{ iiab_hostname }}.{{ iiab_domain }}"
    FQDN_changed: False

- name: Set FQDN_changed when iiab_fqdn != ansible_fqdn ({{ ansible_fqdn }})
  set_fact:
    FQDN_changed: True
  when: iiab_fqdn != ansible_fqdn

- name: Set hostname if FQDN_changed
  include_tasks: hostname.yml
  when: FQDN_changed | bool

- name: Add 'runtime' variable values to {{ iiab_ini_file }}
  ini_file:
    dest: "{{ iiab_ini_file }}"
    section: runtime
    option: "{{ item.option }}"
    value: "{{ item.value }}"
  with_items:
    - option: iiab_stage
      value: "{{ iiab_stage }}"
    - option: iiab_base_ver
      value: "{{ iiab_base_ver }}"
    - option: iiab_revision
      value: "{{ iiab_revision }}"
    - option: runtime_php
      value: "{{ phplib_dir }}"
    - option: runtime_branch
      value: "{{ ansible_local.local_facts.iiab_branch }}"
    - option: runtime_commit
      value: "{{ ansible_local.local_facts.iiab_commit }}"
    - option: runtime_date
      value: "{{ ansible_date_time.iso8601 }}"
    - option: ansible_version
      value: "{{ ansible_local.local_facts.ansible_version }}"
    - option: kernel
      value: "{{ ansible_kernel }}"
    - option: memory_mb
      value: "{{ ansible_memtotal_mb }}"
    - option: swap_mb
      value: "{{ ansible_swaptotal_mb }}"
    - option: product_id
      value: "{{ ansible_product_uuid }}"
    - option: gw_active
      value: "{{ gw_active }}"
    - option: internet_available
      value: "{{ internet_available }}"
    - option: is_rpi
      value: "{{ is_rpi }}"
    - option: first_run
      value: "{{ first_run }}"
    - option: local_tz
      value: "{{ local_tz }}"
    - option: no_NM_reload
      value: "{{ no_NM_reload }}"
    - option: is_F18
      value: "{{ is_F18 }}"
    - option: FQDN_changed
      value: "{{ FQDN_changed }}"

- name: Add 'runtime' variable 'is_VM' value if defined, to {{ iiab_ini_file }}
  ini_file:
    dest: "{{ iiab_ini_file }}"
    section: runtime
    option: "{{ item.option }}"
    value: "{{ item.value }}"
  with_items:
    - option: is_VM
      value: "yes"
  when: is_VM is defined

- name: STAGE 0 HAS COMPLETED ======================================
  meta: noop
