#!/bin/bash

clear ; read -p '
########################################################
# Insira aqui o SUBDOMINIO da sua nuvem privada.       #
# Por exemplo, se voce deseja criar o subdominio       #
# "nuvem.empresa.com.br", insira o PREFIXO "nuvem".    #
########################################################

Entao, qual o SUBDOMINIO da sua nuvem? ' subdominio

clear ; read -p '
#########################################################
# Agora insira o DOMINIO da sua nuvem privada.          #
# Por exemplo: se o endereco da sua nuvem for for       #
# "nuvem.empresa.com.br", o DOMINIO é "empresa.com.br". #
#########################################################

Entao, qual o DOMINIO da sua nuvem? ' dominio

dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
dnf config-manager --set-enabled PowerTools
dnf install -y ImageMagick ImageMagick-devel

dnf install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm
dnf -y module enable php:remi-7.4

dnf install -y mlocate nano mc net-tools rsync vim ntsysv \
wget zip yum-utils unzip telnet tcpdump nmap mutt \
cyrus-sasl-plain bind-utils tar htop mysql-server httpd \
httpd-tools php php-cli php-common php-curl php-gd \
php-intl php-json php-ldap php-mbstring php-mysqlnd php-xml \
php-zip php-opcache php-devel php-pear php-bcmath php-gmp \
php-pecl-apcu mod_ssl postfix make unzip

yes '' | pecl install imagick

echo "extension=imagick.so" > /etc/php.d/20-imagick.ini

rm -rf /etc/httpd/conf.d/welcome.conf

cat >> /etc/my.cnf.d/mysql-server.cnf << EOF
innodb_buffer_pool_size=1G
innodb_io_capacity=4000
EOF

systemctl enable mysqld
systemctl start mysqld
systemctl enable httpd
systemctl restart httpd

mkdir /var/www/$dominio
chown -R apache.apache /var/www/$dominio

sed -i '64i\submission inet n - n - - smtpd\' /etc/postfix/master.cf

systemctl enable postfix
systemctl start postfix

mysql -e "CREATE DATABASE nextcloud"
mysql -e "CREATE USER 'nextcloud'@'localhost' IDENTIFIED WITH mysql_native_password BY 'nextcloud'"
mysql -e "GRANT USAGE ON *.* TO 'nextcloud'@'localhost'"
mysql -e "GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextcloud'@'localhost'; ALTER USER 'nextcloud'@'localhost'"

download() {
    local url=https://downloads.aprendendolinux.com/nextcloud-20.0.2.zip
    echo -n "Baixando NextCloud...    "
    wget --progress=dot $url 2>&1 | grep --line-buffered "%" | \
    sed -u -e "s,\.,,g" | awk '{printf("\b\b\b\b%4s", $2)}'
    echo -ne "\b\b\b\b"
    echo ' Pronto!'
}

download  

unzip -qq nextcloud-20.0.2.zip
mv nextcloud /var/www/$dominio/$subdominio
chown -R apache.apache /var/www/$dominio/$subdominio
mkdir /home/$subdominio
chown -R apache.apache /home/$subdominio

cat >> /etc/httpd/conf/httpd.conf << EOF
IncludeOptional conf.d/*/*.conf
EOF

cat > /etc/httpd/conf.d/ssl.conf << EOF
#
Listen 443 https
SSLPassPhraseDialog exec:/usr/libexec/httpd-ssl-pass-dialog
SSLSessionCache         shmcb:/run/httpd/sslcache(512000)
SSLSessionCacheTimeout  300
SSLRandomSeed startup file:/dev/urandom  256
SSLRandomSeed connect builtin
SSLCryptoDevice builtin
EOF

mkdir /etc/httpd/conf.d/$dominio

cat > /etc/httpd/conf.d/$dominio/$subdominio.conf << EOF
Redirect 301 /.well-known/carddav https://$subdominio.$dominio/remote.php/dav
Redirect 301 /.well-known/caldav https://$subdominio.$dominio/remote.php/dav

<VirtualHost *:80>
    Redirect permanent / https://$subdominio.$dominio
    ServerName $subdominio.$dominio
</VirtualHost>

<VirtualHost *:443>
    ServerName $subdominio.$dominio
    DocumentRoot /var/www/$dominio/$subdominio
  <IfModule mod_headers.c>
      Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains"
  </IfModule>
    <Directory /var/www/$dominio/$subdominio>
            DirectoryIndex index.php index.htm index.html
    </Directory>
    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/apache.crt
    SSLCertificateKeyFile /etc/pki/tls/private/apache.key
</VirtualHost>
EOF

sed -i '46i\TimeOut 3000\' /etc/httpd/conf/httpd.conf
sed -i 's/max_execution_time = 30/max_execution_time = 3000/' /etc/php.ini
sed -i 's/memory_limit = 128M/memory_limit = 1024M/' /etc/php.ini

openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/pki/tls/private/apache.key \
-out /etc/pki/tls/certs/apache.crt \
-subj "/CN=$subdominio.$dominio"

cat > /var/www/$dominio/$subdominio/config/config.php << EOF
<?php
\$CONFIG = array (
    'datadirectory' => "/home/$subdominio",
    'memcache.local' => '\OC\Memcache\APCu',
    'default_language' => 'pt_BR',
    'force_language' => 'pt_BR',
    'default_locale' => 'pt_BR',
    'force_locale' => 'pt_BR',
);
EOF

chown apache.apache /var/www/$dominio/$subdominio/config/config.php 
 
systemctl restart php-fpm
systemctl restart httpd

cat > /etc/ssh/ssh_config << EOF
Host *
        GSSAPIAuthentication yes
        GSSAPIDelegateCredentials no
        StrictHostKeyChecking no
        UserKnownHostsFile /dev/null
        HostKeyAlgorithms=+ssh-dss
        HashKnownHosts no
        LogLevel ERROR
        ForwardX11Trusted yes
        SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
        SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
        SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE
        SendEnv XMODIFIERS
EOF

cat >> /root/.bashrc << EOF

HISTSIZE=(some number less than 0)
HISTFILESIZE=(some number less than 0)
export HISTTIMEFORMAT="%d/%m/%y %T "
EOF

sed -i 's/\\W/\\w/' /etc/bashrc
sed -i s/AcceptEnv/#AcceptEnv/g /etc/ssh/sshd_config
sed -i s/#UseDNS\ yes/UseDNS\ no/g /etc/ssh/sshd_config
sed -i s/alias/#alias/g /root/.bashrc
sed -i s/rhgb/crashkernel=auto/g /etc/default/grub
sed -i s/^SELINUX=.*$/SELINUX=disabled/g /etc/selinux/config
systemctl disable firewalld
dnf -y update
yum clean all
grub2-mkconfig -o /boot/grub2/grub.cfg

reboot -f
