#!/bin/bash
INSTALL_SOURCE="$(dirname "$0")"
apt-get update
apt-get -y upgrade
apt install software-properties-common
LC_ALL=C.UTF-8 add-apt-repository -y ppa:ondrej/php 
apt-get update
apt-get install -y php7.3
apt-get install -y mariadb-server
apt-get install -y apache2
apt-get install -y composer
apt-get install -y git
apt-get install -y npm
apt-get install -y php7.3-mbstring
apt-get install -y php7.3-xml
apt-get install -y php7.3-pdo-mysql
apt-get install -y cron
a2enmod rewrite
a2dismod php7.2
a2dismod php7.4
a2enmod php7.3
systemctl restart apache2

mysql -u root -e "CREATE DATABASE mqttstd";
mysql -u root -e "CREATE USER mqttstd IDENTIFIED BY 'diplomka1234/';";
mysql -u root -e "grant usage on *.* TO mqttstd;";
mysql -u root -e "grant all privileges on mqttstd.* to mqttstd;";
mysql -u root -e "flush privileges";

mysql mqttstd < $INSTALL_SOURCE/mqttstd.sql

cp -R $INSTALL_SOURCE/apache2.conf /etc/apache2/
cp -R $INSTALL_SOURCE/000-default.conf /etc/apache2/sites-enabled/
cp -R $INSTALL_SOURCE/000-default.conf /etc/apache2/sites-available/
cp -R $INSTALL_SOURCE/DP_STD /var/www/html/
cp -R $INSTALL_SOURCE/mqtt_subscribe.service /etc/systemd/system/
cp -R $INSTALL_SOURCE/mqtt_subscribe /usr/local/sbin/
chmod 777 /etc/systemd/system/mqtt_subscribe.service
chmod 777 /usr/local/sbin/mqtt_subscribe
systemctl daemon-reload

chmod -R 777 /var/www/html/DP_STD

cd /var/www/html/DP_STD/ 
composer install
composer update
npm install
npm install fibers
npm install node-sass
npm install webpack
npm update
npm uninstal sass-loader
npm install sass-loader@7.1.0
npm run production
chmod -R 777 /var/www/html/DP_STD

chmod -R 777 /var/spool/cron/crontabs

echo "* * * * * cd /var/www/html/DP_STD && php artisan schedule:run >> /dev/null 2>&1" >> /var/spool/cron/crontabs/root

systemctl restart apache2
systemctl restart cron
systemctl enable mqtt_subscribe.service
systemctl start mqtt_subscribe.service


