#!/usr/bin/env bash

dnf install -y epel-release yum-utils unzip curl \
               bash-completion mlocate bzip2 \
               policycoreutils-python-utils

dnf update -y
dnf install -y httpd

systemctl enable httpd.service
systemctl start httpd.service

dnf install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
dnf install -y yum-utils
dnf update -y

dnf install -y \
  php74-php \
  php74-php-pecl-apcu \
  php74-php-bcmath \
  php74-php-dom \
  php74-php-gmp \
  php74-php-pecl-imagick \
  php74-php-ldap \
  php74-php-openssl \
  php74-php-gd \
  php74-php-json \
  php74-php-mysql \
  php74-php-curl \
  php74-php-mbstring \
  php74-php-intl \
  php74-php-exif \
  php74-php-zip \
  php74-php-zlib \
  php74-php-fileinfo \
  php74-php-pcntl \
  php74-php-posix \
  php74-php-xmlreader \
  php74-php-xmlwriter \
  php74-php-ctype \
  php74-php-bz2 \
  php74-php-ftp \
  php74-php-smbclient \
  php74-php-memcached \
  php74-php-redis \
  php74-php-phar \
  php74-php-opcache

dnf localinstall -y --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm
dnf install -y libreoffice ffmpeg

#PUT NEXTCLOUD CONF APACHE
#/etc/httpd/conf.d/nextcloud.conf

#PHP

PHP_POST_LIMIT=50G
PHP_UPLOAD_LIMIT=25G
PHP_MAX_FILE=200
PHP_MAX_TIME=3600
PHP_MEMORY_LIMIT=512M
echo "memory_limit=${PHP_MEMORY_LIMIT}" > /etc/opt/remi/php74/php.d/nextcloud.ini
echo "upload_max_filesize=${PHP_UPLOAD_LIMIT}" >> /etc/opt/remi/php74/php.d/nextcloud.ini
echo "post_max_size=${PHP_POST_LIMIT}" >> /etc/opt/remi/php74/php.d/nextcloud.ini
echo "max_file_uploads=${PHP_MAX_FILE}" >> /etc/opt/remi/php74/php.d/nextcloud.ini
echo "max_execution_time=${PHP_MAX_TIME}" >> /etc/opt/remi/php74/php.d/nextcloud.ini
echo "max_input_time=${PHP_MAX_TIME}" >> /etc/opt/remi/php74/php.d/nextcloud.ini
echo "session.gc_maxlifetime=${PHP_MAX_TIME}" >> /etc/opt/remi/php74/php.d/nextcloud.ini

#APCU

APC_SHM_SIZE=128M
sed -i -e "s/apc.shm_size = 32M/apc.shm_size = ${APC_SHM_SIZE}/g" /etc/opt/remi/php74/php.d/40-apcu.ini

#OPCACHE

OPCACHE_MEM_SIZE=128M
echo "opcache.enable=1" >> /etc/opt/remi/php74/php.d/10-opcache.ini
echo "opcache.enable_cli=1" >> /etc/opt/remi/php74/php.d/10-opcache.ini
echo "opcache.memory_consumption=${OPCACHE_MEM_SIZE}" >> /etc/opt/remi/php74/php.d/10-opcache.ini
echo "opcache.interned_strings_buffer=32" >> /etc/opt/remi/php74/php.d/10-opcache.ini
echo "opcache.max_accelerated_files=10000" >> /etc/opt/remi/php74/php.d/10-opcache.ini
echo "opcache.save_comments=1" >> /etc/opt/remi/php74/php.d/10-opcache.ini
echo "opcache.revalidate_freq=60" >> /etc/opt/remi/php74/php.d/10-opcache.ini
#
echo "opcache.fast_shutdown=1" >> /etc/opt/remi/php74/php.d/10-opcache.ini
echo "opcache.jit=disable" >> /etc/opt/remi/php74/php.d/10-opcache.ini
echo "opcache.jit_buffer_size=0" >> /etc/opt/remi/php74/php.d/10-opcache.ini

systemctl restart httpd.service

NEXTCLOUD_VERSION=24.0.4
NEXTCLOUD_TARBALL="nextcloud-${NEXTCLOUD_VERSION}.tar.bz2"
SHA256_SUM="d107426f8e1c193db882a04c844f9bc7e7eeb7c21e46c46197e5154d6d6ac28e"
GPG_FINGERPRINT="2880 6A87 8AE4 23A2 8372  792E D758 99B9 A724 937A"

dnf install -y wget

cd /tmp \
 && wget -q https://download.nextcloud.com/server/releases/${NEXTCLOUD_TARBALL} \
 && wget -q https://download.nextcloud.com/server/releases/${NEXTCLOUD_TARBALL}.asc \
 && wget -q https://nextcloud.com/nextcloud.asc \
 && echo "Verifying both integrity and authenticity of ${NEXTCLOUD_TARBALL}..." \
 && CHECKSUM_STATE=$(echo -n $(echo "${SHA256_SUM}  ${NEXTCLOUD_TARBALL}" | sha256sum -c) | tail -c 2) \
 && if [ "${CHECKSUM_STATE}" != "OK" ]; then echo "Error: checksum does not match" && exit 1; fi \
 && gpg --import nextcloud.asc \
 && FINGERPRINT="$(LANG=C gpg --verify ${NEXTCLOUD_TARBALL}.asc ${NEXTCLOUD_TARBALL} 2>&1 \
  | sed -n 's#Primary key fingerprint: \(.*\)#\1#p')" \
 && if [ -z "${FINGERPRINT}" ]; then echo "Error: invalid GPG signature!" && exit 1; fi \
 && if [ "${FINGERPRINT}" != "${GPG_FINGERPRINT}" ]; then echo "Error: wrong GPG fingerprint" && exit 1; fi \
 && echo "All seems good, now unpacking ${NEXTCLOUD_TARBALL}..."

cd /var/www/html \
 && mkdir nextcloud \
 && tar xjf /tmp/${NEXTCLOUD_TARBALL} --strip 1 -C nextcloud \
 && mkdir /var/www/html/nextcloud/data \
 && chown -R apache:apache nextcloud \
 && rm /tmp/nextcloud*

#sudo -u apache php74 occ maintenance:install --database "mysql" --database-name "nextcloudb" --database-host=10.90.90.13 --database-user "web" --database-pass "secret" --admin-user "pedro" --admin-pass "pedro" --data-dir=/var/www/html/nextcloud/data

semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/nextcloud/data(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/nextcloud/config(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/nextcloud/apps(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/nextcloud/.htaccess'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/nextcloud/.user.ini'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/nextcloud/3rdparty/aws/aws-sdk-php/src/data/logs(/.*)?'
restorecon -R '/var/www/html/nextcloud/'
setsebool -P httpd_can_network_connect on

#sudo -u apache php74 occ config:import /tmp/nextcloud.config.json
