#!/usr/bin/env bash

apt update
apt install -y apache2

service apache2 restart

exit

libapache2-mod-php php-gd php-mysql \
php-curl php-mbstring php-intl php-gmp php-bcmath php-xml php-imagick php-zip

apt install -y \
  php7 \
  apache2-mod_php7 \
  php7-APCu \
  php7-bcmath \
  php7-dom \
  php7-gmp \
  php7-imagick \
  php7-ldap \
  php7-openssl \
  php7-gd \
  php7-json \
  php7-mysql \
  php7-curl \
  php7-mbstring \
  php7-intl \
  php7-exif \
  php7-zip \
  php7-zlib \
  php7-fileinfo \
  php7-pcntl \
  php7-posix \
  php7-xmlreader \
  php7-xmlwriter \
  php7-ctype \
  php7-bz2 \
  php7-ftp \
  php7-smbclient \
  php7-memcached \
  php7-redis \
  php7-phar \
  php7-opcache

#zypper install --no-confirm libreoffice ffmpeg

a2enmod rewrite
a2enmod headers

#PUT NEXTCLOUD CONF APACHE

#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/php7/conf.d/nextcloud.ini
echo "upload_max_filesize=${PHP_UPLOAD_LIMIT}" >> /etc/php7/conf.d/nextcloud.ini
echo "post_max_size=${PHP_POST_LIMIT}" >> /etc/php7/conf.d/nextcloud.ini
echo "max_file_uploads=${PHP_MAX_FILE}" >> /etc/php7/conf.d/nextcloud.ini
echo "max_execution_time=${PHP_MAX_TIME}" >> /etc/php7/conf.d/nextcloud.ini
echo "max_input_time=${PHP_MAX_TIME}" >> /etc/php7/conf.d/nextcloud.ini
echo "session.gc_maxlifetime=${PHP_MAX_TIME}" >> /etc/php7/conf.d/nextcloud.ini

#APCU

APC_SHM_SIZE=128M
sed -i -e "s/apc.shm_size = 32M/apc.shm_size = ${APC_SHM_SIZE}/g" /etc/php7/conf.d/apcu.ini

#OPCACHE

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

service apache2 restart

zypper install --no-confirm wget bzip2

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"

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 /srv/www/htdocs \
 && mkdir nextcloud \
 && tar xjf /tmp/${NEXTCLOUD_TARBALL} --strip 1 -C nextcloud \
 && chown -R wwwrun:wwwrun nextcloud \
 && rm /tmp/nextcloud*

#sudo -u wwwrun php 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=/srv/www/htdocs/nextcloud/data

#sudo -u wwwrun php occ config:import /tmp/nextcloud.config.json

#zypper install --no-confirm php7-fpm

