# CHECK FOR PHP VERSION AUTOMATICALLY, TO DETERMINE WHICH NEXTCLOUD TO INSTALL.
# INSPIRED BY: github.com/iiab/iiab/blob/master/roles/nodejs/tasks/main.yml

# - name: Try to run 'php -v' to get PHP version
#   # e.g. converts multi-line "PHP 7.0.33-0ubuntu0.16.04.5 (cli) ( NTS ) ..." to "7.0.33"
#   shell: php -v | head -1 | sed 's/^[^0-9.]*//; s/[^0-9.].*//'
#   register: php_version_installed
#   #ignore_errors: yes    # NOT NEC: if php is not installed, php_version_installed.stdout will get set to ""

# - debug:
#     var: php_version_installed

# # NOTE: this could easily be made to work even if PHP was not installed, e.g.
# # by pre-initializing variable 'php_new' to False here.  But trapping the
# # absence of PHP (below) is a useful software safety precondition!
# #
# #- name: Initialize var 'php_new' to False
# #  set_fact:
# #    php_new: False

# - name: INTENTIONALLY FAIL, IF PHP (Nextcloud prerequisite) ISN'T INSTALLED
#   fail:
#     msg: >
#       Nextcloud install cannot proceed, as it requires PHP be installed first.
#       Note that as of 2019-07-04, IIAB takes care of this by forcing vars
#       mysql_install and mysql_enabled to True in
#       /opt/iiab/iiab/roles/0-init/tasks/main.yml, which in turn forces the
#       installation of PHP in /opt/iiab/iiab/roles/mysql/tasks/main.yml, as
#       invoked by /opt/iiab/iiab/roles/3-base-server/tasks/main.yml
#   when: php_version_installed.stdout == ""

# - name: Set var 'php_new' indicating if installed version of PHP ({{ php_version_installed.stdout }}) >= 7.1, as required by Nextcloud 16
#   set_fact:
#     php_new: "{{ php_version_installed.stdout is version('7.1', '>=') }}"
#     # Ansible's Version Comparison routine:
#     # https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#version-comparison
#   when: php_version_installed.stdout != ""    # i.e. IF ABOVE 'php -v' WORKED

# - debug:
#     var: php_new


# February 2020: See @m-anish's PR #2119 and follow-up PR #2258.
# 2023-03-21: Check the latest required AND recommended prereqs below.
# e.g. Nextcloud 26 now allows installation on PHP 8.2:
# https://docs.nextcloud.com/server/latest/admin_manual/installation/system_requirements.html
# https://docs.nextcloud.com/server/latest/admin_manual/installation/source_installation.html#prerequisites-for-manual-installation
# https://docs.nextcloud.com/server/25/admin_manual/installation/source_installation.html#prerequisites-for-manual-installation
# https://docs.nextcloud.com/server/24/admin_manual/installation/source_installation.html#prerequisites-for-manual-installation
- name: Install ffmpeg + libxml2 + 11 PHP packages (run 'php -m' or 'php -i' to verify)
  package:
    name:
      #- dnsutils    # NOT REQUESTED by Nextcloud
      - ffmpeg       # Optional (for preview generation)
      - libxml2      # php-libxml requires libxml2 >= 2.7.0
      #- libapache2-mod-php    # 2020-02-15: NO LONGER NEEDED?
      - php{{ php_version }}-bcmath        # Highly recommended by Nextcloud 21 for "improved performance and better compatibility" -- Likewise installed in pbx/tasks/freepbx_dependencies.yml, wordpress/tasks/install.yml
      - php{{ php_version }}-bz2           # OPTIONAL (for extraction of apps)
      #- php{{ php_version }}-common       # Auto-installed as an apt dependency.  REGARDLESS: php{{ php_version }}-common superset php{{ php_version }}-cli is auto-installed by php{{ php_version }}-fpm in nginx/tasks/install.yml
      - php{{ php_version }}-curl          # Likewise installed in moodle/tasks/install.yml, pbx/tasks/freepbx_dependencies.yml, wordpress/tasks/install.yml
      #- php{{ php_version }}-exif         # OPTIONAL (for image rotation in pictures app) but somehow already installed with PHP's core, as confirmed by 'php -m'
      #- php{{ php_version }}-fileinfo     # OPTIONAL (enhances file analysis performance) but somehow already installed with PHP's core, as confirmed by 'php -m'
      - php{{ php_version }}-gd            # Likewise installed in moodle/tasks/install.yml, pbx/tasks/freepbx_dependencies.yml
      - php{{ php_version }}-gmp           # OPTIONAL (for SFTP storage)
      - php-imagick                        # OPTIONAL (for preview generation).  BUT drags in Apache's libapache2-mod-phpX.Y etc, as confirmed by 'apt depends php-imagick' -- while php{{ php_version }}-imagick installs (despite not being shown within 'apt list "php*imagick"') it's no better -- and 'apt depends phpX.Y-imagick' mysteriously does NOT show its deps.  Likewise installed in wordpress/tasks/install.yml
      - php{{ php_version }}-intl          # OPTIONAL (increases language translation performance and fixes sorting of non-ASCII characters): Likewise installed in mediawiki/tasks/install.yml, moodle/tasks/install.yml, wordpress/tasks/install.yml
      #- php{{ php_version }}-json         # Included with PHP >= 8.0 -- see stanza just below
      #- php{{ php_version }}-libxml       # NOT INSTALLABLE: ENABLED BY DEFAULT (https://www.php.net/manual/en/libxml.installation.php)
      - php{{ php_version }}-mbstring      # Likewise installed in mediawiki/tasks/install.yml, moodle/tasks/install.yml, pbx/tasks/freepbx_dependencies.yml, wordpress/tasks/install.yml
      - php{{ php_version }}-mysql         # Likewise installed in mysql/tasks/install.yml, pbx/tasks/freepbx_dependencies.yml, wordpress/tasks/install.yml
      #- php{{ php_version }}-openssl      # Included with PHP >= 8.0 -- NOT INSTALLABLE
      #- php{{ php_version }}-pdo_mysql    # NOT INSTALLABLE: php{{ php_version }}-mysql handles this on all OS's?
      #- php{{ php_version }}-redis        # @m-anish future work?
      #- php{{ php_version }}-session      # NOT INSTALLABLE: ENABLED BY DEFAULT?
      #- php{{ php_version }}-smbclient    # Optional (SMB/CIFS integration)
      - php{{ php_version }}-xml           # Likewise installed in mediawiki/tasks/install.yml, moodle/tasks/install.yml, pbx/tasks/freepbx_dependencies.yml, wordpress/tasks/install.yml -- AND REGARDLESS dragged in later by Admin Console's use of php-pear for roles/cmdsrv/tasks/main.yml -- Nextcloud's official requirements include {SimpleXML, XMLReader, XMLWriter} as confirmed by 'php -m | grep -i xml' which in the end shows {libxml, SimpleXML, xml, xmlreader, xmlwriter}
      #- php{{ php_version }}-xmlrpc       # 2021-06-27: Experimentally remove, as explained in moodle/tasks/install.yml
      - php{{ php_version }}-zip           # Likewise installed in moodle/tasks/install.yml, pbx/tasks/freepbx_dependencies.yml, wordpress/tasks/install.yml
      #- php{{ php_version }}-zlib         # NOT INSTALLABLE: ENABLED BY DEFAULT?
    state: present

# For PHP >= 8.0: phpX.Y-json is baked into PHP itself.
# For PHP <  8.0: phpX.Y-json auto-installed by phpX.Y-fpm AND phpX.Y-cli in 3-base-server's nginx/tasks/install.yml, as confirmed by: apt rdepends phpX.Y-json
#
#- name: Install php{{ php_version }}-json if PHP < 8.0
#  package:
#    name: php{{ php_version }}-json
#    state: present
#  when: php_version is version('8.0', '<')

- name: "Run roles/www_options/tasks/php-settings.yml with 'nginx_high_php_limits: False' by default"
  include_tasks: roles/www_options/tasks/php-settings.yml
  when: php_settings_done is undefined


- name: Create dir {{ nextcloud_root_dir }} (by default 755 dirs & 644 files)
  file:
    state: directory
    path: "{{ nextcloud_root_dir }}"    # /library/www/nextcloud

- name: "2023-03-24: NEXTCLOUD 26 REQUIRES PHP 8 -- SO THIS TEMPORARY PATCH INSTALLS THE OLDER NEXTCLOUD 25 ON OS's WITH PHP <= 7.4 -- WHOSE END-OF-LIFE WAS NOVEMBER 2022"
  set_fact:
    nextcloud_dl_url: https://download.nextcloud.com/server/releases/latest-25.tar.bz2
  when: php_version is version('7.4', '<=')

- name: Unarchive {{ nextcloud_dl_url }} (~140 MB) to {{ nextcloud_root_dir }} (~519 MB initially, sometimes ~543 MB later, {{ apache_user }}:{{ apache_user }})
  unarchive:
    remote_src: yes    # Overwrite even if "already exists on the target"
    src: "{{ nextcloud_dl_url }}"
    #dest: "{{ nextcloud_base_dir }}"    # /library/www
    dest: "{{ nextcloud_root_dir }}"     # /library/www/nextcloud
    owner: "{{ apache_user }}"           # www-data on debuntu
    group: "{{ apache_user }}"
    extra_opts: --strip-components=1     # Or use 'dest: /library/www' above

- name: Provision Nextcloud's MySQL DB, run Nextcloud's install wizard, etc
  include_tasks: setup.yml


# RECORD Nextcloud AS INSTALLED

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

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