# Fixes search @ http://box/modules/es-wikihow (for young Spanish speakers)

# README & Code: https://github.com/iiab/php-stem

# Source Code also here: https://download.iiab.io/packages/php-stem.src.tar
# June 2018 debugging & compilation thanks to Tim Moody & George Hunt
# Original bug: https://github.com/iiab/iiab/issues/829


- name: Install apt package php{{ php_version }}-sqlite3 -- nec for http://box/modules/es-wikihow search -- can also be installed by osm-vector-maps/tasks/install.yml
  package:
    state: present
    name: php{{ php_version }}-sqlite3


- name: Populate php_extensions dictionary (lookup table of recent PHP versions and their corresponding YYYYMMDD, for /usr/lib/php/YYYYMMDD/stem.so)
  set_fact:
    php_extensions:    # Dictionary keys (left side) are always strings, e.g. "7.2"
      7.2: 20170718    # Dictionary values (right side) can be of type int here, as it's auto-cast to string just below.  Strings would also work e.g. 7.2: "20170718"
      7.3: 20180731
      7.4: 20190902
      8.0: 20200930
      8.1: 20210902

- name: Set php_extension to "{{ php_extensions[php_version] }}" using php_extensions[php_version is "{{ php_version }}"]
  set_fact:
    php_extension: "{{ php_extensions[php_version] }}"

# Auto-lookup would also work...
# php -i | grep 'PHP Extension =>' | cut -d' ' -f4
#
# Or...
# ls -d /usr/lib/php/20?????? | cut -d/ -f5
#
# (Or in PHP code...)
# phpinfo()

# Or consider https://github.com/iiab/php-stem/issues/2 in future?
# "Current practice is to put the stem.so file in a different place for each php version.
# Would it be better to create a directory /usr/local/lib/php-stem and always put it there.
# Then put that location in stem.ini"


- name: Populate php_stem_arches dictionary (lookup table of CPU architectures for https://github.com/iiab/php-stem/raw/main/so/stem-[ARCH]-[PHP VERSION].so)
  set_fact:
    php_stem_arches:
      armv6l: armhf
      armv7l: armhf
      aarch64: aarch64
      x86_64: x64

- name: Set php_stem_arch to "{{ php_stem_arches[ansible_machine] }}" using php_stem_arches[ansible_machine is "{{ ansible_machine }}"]
  set_fact:
    php_stem_arch: "{{ php_stem_arches[ansible_machine] }}"


- name: Download https://github.com/iiab/php-stem/raw/main/so/stem-{{ php_stem_arch }}-{{ php_version }}.so to /usr/lib/php/{{ php_extension }}/stem.so
  get_url:
    url: https://github.com/iiab/php-stem/raw/main/so/stem-{{ php_stem_arch }}-{{ php_version }}.so
    dest: /usr/lib/php/{{ php_extension }}/stem.so
    timeout: "{{ download_timeout }}"

# https://en.wikipedia.org/wiki/Here_document
- name: Install "extension=stem.so" in /etc/php/{{ php_version }}/mods-available/stem.ini
  shell: |
    cat > /etc/php/{{ php_version }}/mods-available/stem.ini << EOF
    ; configuration for php common module
    ; priority=20
    extension=stem.so
    EOF

- name: Symlink /etc/php/{{ php_version }}/fpm/conf.d/20-stem.ini -> /etc/php/{{ php_version }}/mods-available/stem.ini
  file:
    src: /etc/php/{{ php_version }}/mods-available/stem.ini
    path: /etc/php/{{ php_version }}/fpm/conf.d/20-stem.ini
    state: link

- debug:
    msg: YOU MAY NEED TO 'systemctl reload php{{ php_version }}-fpm' -- whereas during an IIAB install, roles/www_options restarts it for you


# - name: Set fact stem available php 7.2 - includes x86_64 only
#   set_fact:
#     stem_available: True
#   when: ansible_machine == "x86_64" and php_version == 7.2

# - name: Set fact stem available php 7.3 - excludes i386
#   set_fact:
#     stem_available: True
#   when: not ansible_machine == "i386" and php_version == 7.3

# - name: Set fact stem available php 7.4
#   set_fact:
#     stem_available: True
#   when: php_version == 7.4 and (ansible_machine == "aarch64" or ansible_machine == "x86_64")

# - name: Unarchive https://download.iiab.io/packages/php{{ php_version }}-stem.rpi.tar to / (rpi)
#   unarchive:
#     src: https://download.iiab.io/packages/php{{ php_version }}-stem.rpi.tar
#     dest: /
#     owner: root
#     group: root
#     #mode: ????
#     remote_src: yes
#   when: (ansible_machine == "armv7l" or ansible_machine == "armv6l") and stem_available is defined

# - name: Unarchive https://download.iiab.io/packages/php{{ php_version }}-stem.aarch64.tar to / (rpi)
#   unarchive:
#     src: https://download.iiab.io/packages/php{{ php_version }}-stem.aarch64.tar
#     dest: /
#     owner: root
#     group: root
#     #mode: ????
#     remote_src: yes
#   when: ansible_machine == "aarch64" and stem_available is defined

# - name: Unarchive https://download.iiab.io/packages/php{{ php_version }}-stem.x64.tar to / (x64)
#   unarchive:
#     src: https://download.iiab.io/packages/php{{ php_version }}-stem.x64.tar
#     dest: /
#     owner: root
#     group: root
#     #mode: ????
#     remote_src: yes
#   when: ansible_machine == "x86_64" and stem_available is defined

# - name: Symlink /etc/php/{{ php_version }}/fpm/conf.d/20-stem.ini -> /etc/php/{{ php_version }}/mods-available/stem.ini
#   file:
#     src: "/etc/php/{{ php_version }}/mods-available/stem.ini"
#     path: "/etc/php/{{ php_version }}/fpm/conf.d/20-stem.ini"
#     state: link
#   when: stem_available is defined


# Not sure what to do for apache, so do nothing for now

# The following are probably no longer true 2020-01-25
# Presumably fails on Debian 8 & 10?
# Fails on Debian i686 as of 2018-08-07: https://github.com/iiab/iiab/issues/983
# Fails on Ubuntu 18.04 as of 2018-07-28: https://github.com/iiab/iiab/issues/829
