---
# CentOS related tasks

- name: Prep OS... Check web server installed
  shell: "httpd -v"
  register: web_apache
  ignore_errors: yes

- name: Prep OS... Check nginx installed
  shell: "nginx -v"
  register: web_nginx
  ignore_errors: yes
  when: web_apache.stderr | length > 0

- name: Prep OS... Set nextcloud_websrv variable
  set_fact:
    nextcloud_websrv: "apache"

- name: Prep OS... Set nextcloud_websrv variable
  set_fact:
    nextcloud_websrv: "nginx"
  when: web_apache.stderr | length > 0

- name: Prep OS... display web server
  debug: var=nextcloud_websrv

#- name: Prep OS... Create tmp directory
#  file:
#    path: "{{ item }}"
#    state: directory
#    owner: "{{ ansible_user }}"
#    mode: 0770
#  with_items:
#    - "/tmp/ansible_{{ ansible_user }}"
#
#- name: Prep OS... Set remote tmp
#  set_fact:
#    ansible_remote_tmp: "/tmp/ansible_{{ ansible_user }}"

- name: Prep OS... add rpmfusion-free-release centos{{ ansible_distribution_major_version|int }} repo
  dnf:
    name: https://download1.rpmfusion.org/free/el/rpmfusion-free-release-{{ ansible_distribution_major_version|int }}.noarch.rpm
    disable_gpg_check: yes
    validate_certs: no
    state: latest

- name: Prep OS... import key from Collabora centos{{ ansible_distribution_major_version|int }} repo
  ansible.builtin.rpm_key:
    state: present
    key: https://www.collaboraoffice.com/repos/CollaboraOnline/CODE-centos{{ ansible_distribution_major_version|int }}/repodata/repomd.xml.key
  when: nc_collabora

- name: Prep OS... add Collabora repos centos{{ ansible_distribution_major_version|int }} repo
  ansible.builtin.yum_repository:
    name: collabora
    description: Collabora Online CODE repo
    baseurl: https://www.collaboraoffice.com/repos/CollaboraOnline/CODE-centos{{ ansible_distribution_major_version|int }}
  when: nc_collabora

- name: Prep OS... update os
  dnf:
    name: '*'
    update_cache: true
    state: latest

- name: Prep OS... install Collabora packages
  dnf:
    name:
      - coolwsd
      - CODE-brand
      - inotify-tools 
      - psmisc 
      - perl
    state: latest
  when: nc_collabora

- name: Prep OS... enable powertools repo
  shell: dnf config-manager --set-enabled powertools

- name: Prep OS... install needed packages
  dnf:
    name:
      - ffmpeg
      - libreoffice-writer 
      - libreoffice-base 
      - libreoffice-impress 
      - libreoffice-draw 
      - libreoffice-math
      - pigz
    state: latest
    enablerepo: epel

- name: Prep OS... Set http env on {{ ansible_facts['distribution'] }}
  set_fact:
    http_service_name: httpd
    http_webroot: /var/www/html
    nextcloud_websrv_user: apache
    nextcloud_websrv_group: apache
  when: nextcloud_websrv in ["apache", "apache2"]

- name: Prep OS... Get httpd version
  shell: "httpd -v | head -n1 | cut -d'/' -f2 | cut -d' ' -f1"
  register: web_version
  when: nextcloud_websrv in ["apache", "apache2"]

- name: Prep OS... Get SSL version
  shell: "openssl version | cut -d' ' -f2 | cut -d'-' -f1"
  register: ssl_version

- name: Prep OS... Set web and SSL status
  set_fact:
    web_status: "{{ web_version.stdout|string is version('2.4.8', '>=') }}"
    ssl_status: "{{ ssl_version.stdout|string is version('1.1.1', '>') }}"

- name: Prep OS... Set php env for {{ ansible_facts['distribution'] }}
  set_fact: 
    php_bin: /usr/bin/php
    #php_dir: "/etc/opt/remi/php{{ php_version | replace('.','') }}/php.d"

- include_tasks: "ssl.yml"

- name: Prep OS... Generate Nextcloud configuration for apache
  template:
    dest: /etc/httpd/conf.d/nextcloud.conf
    src: apache2_nc_conf.j2
    #src: apache2_nc_conf_old.j2
    mode: 0640
  when: nextcloud_websrv in ["apache", "apache2"]

- name: Prep OS... Install glusterFS client
  dnf:
    name: glusterfs-fuse
    state: latest
  when: nb_web_servers | int > 1
