#!/bin/bash
#
# Скрипт автоматической установки КБ на свежеустановленную Ubuntu 18.04
#
# В командной строке нового сервера, с правами администратора, выполнить команду:
#   wget -qO- https://clientbase.ru/download/install/install-cb-ubuntu-1804.sh | sudo bash

echo
echo Clientbase installation
echo =======================
echo

# Общий контекст выполнения
TMPDIR=/tmp/cb_$(date | md5sum | head -c 4)
DBDATA=cb_data
DBUSER=cb_user
DBPSWD=$(date | md5sum | head -c 16)
DBQUERY=(
  "CREATE USER '$DBUSER'@'localhost' IDENTIFIED BY '$DBPSWD';"
  "CREATE DATABASE $DBDATA;"
  "GRANT ALL PRIVILEGES ON  $DBDATA.* TO '$DBUSER'@'localhost';"
  "FLUSH PRIVILEGES;"
)
CONFIGPHP=(
  "<?php"
  "\$config[\"type\"] = \"WEB\";"
  "\$config[\"version\"] = \"030004\";"
  "\$config[\"lang\"] = \"russian\";"
  "\$config[\"dbhost\"] = \"localhost\";"
  "\$config[\"dbuser\"] = \"$DBUSER\";"
  "\$config[\"dbpass\"] = \"$DBPSWD\";"
  "\$config[\"dbname\"] = \"$DBDATA\";"
  "\$config[\"table_prefix\"] = \"cb_\";"
)
SERVERNAME=$(hostname -I)

mkdir "$TMPDIR" ; cd "$TMPDIR" || exit

# check Ubuntu Server 18.04
if [[ "$(cat /etc/os-release)" != *"Ubuntu 18.04"* ]]; then
  echo "Error: this is not Ubuntu 18.04"
  exit;
fi

# check for installed apache, php, mysql
if [[ -d /etc/apache2 ]]; then
  echo "Error: apache has already installed"
  exit
fi
if [[ -d /etc/php ]]; then
  echo "Error: php has already installed"
  exit
fi
if [[ -d /etc/mysql ]]; then
  echo "Error: mysql has already installed"
  exit
fi

# update Ubuntu Server
apt -y update && apt -y autoremove

# install apache
apt -y install apache2

# install and tune mariadb
apt -y install mariadb-server
echo "${DBQUERY[*]}" | mysql
cat >> /etc/mysql/conf.d/tune-for-cb.cnf << EOF
[mysqld]
sql_mode=ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
EOF

# install php 7.2
apt -y install php7.2 php7.2-gd php7.2-mbstring php7.2-imap php7.2-zip php7.2-curl php7.2-mysql php7.2-xml
apt -y install php7.2-imagick php7.2-recode php7.2-tidy php7.2-xmlrpc php7.2-bcmath php7.2-opcache

# install ioncube
wget https://downloads.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.tar.gz
tar -xvf ioncube_loaders_lin_x86-64.tar.gz
mv -f ioncube /opt

# tune php.ini
echo zend_extension=/opt/ioncube/ioncube_loader_lin_7.2.so >> /etc/php/7.2/apache2/conf.d/01-ioncube.ini
mv -f /etc/php/7.2/apache2/php.ini /etc/php/7.2/apache2/php.ini.dist
touch /etc/php/7.2/apache2/php.ini
cat > /etc/php/7.2/apache2/php.ini << EOF
error_reporting = E_ALL & ~E_DEPRECATED & ~E_NOTICE & ~E_STRICT
max_input_time = 120
post_max_size = 512M
upload_max_filesize = 512M
memory_limit= 512M
max_execution_time = 360
EOF

# restart services
service mysql restart ; service apache2 restart

# install CB
apt -y install unzip
wget https://clientbase.ru/download/cb_web_3.0.4_ioncube_72.zip
unzip cb_web_3.0.4_ioncube_72.zip
mv -f /var/www/html /var/www/html-dist
mv -f ./cb /var/www/html
echo "${CONFIGPHP[*]}" > /var/www/html/include/config.php
chown -R www-data:www-data /var/www/html

# install cron
cat > /etc/cron.d/clientbase << EOF
# clientbase cron
* * * * * root wget -qO- http://localhost/cron.php > /dev/null 2>&1
EOF

# clean up
rm -rfd "$TMPDIR"

echo
echo =====================================
echo Clientbase was installed successfully
echo
echo "your Clientbase: http://$SERVERNAME"
echo
