#!/bin/bash

export PATH=/opt/plesk/php/8.2/bin:$PATH;

cd /var/www/matsen-git-repository/matsen-tool-be/
sudo git pull

echo "$(tput setab 2)matsen api has been PULLED$(tput sgr 0)"

rm -rf /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/composer.lock
#cp -rf /var/www/matsen-git-repository/matsen-tool-be/httpdocs/composer.lock /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs
rm -rf /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/composer.json
cp -rf /var/www/matsen-git-repository/matsen-tool-be/composer.json /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs

rm -rf /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/config/packages
cp -rf /var/www/matsen-git-repository/matsen-tool-be/config/packages /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/config

rm -rf /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/config/routes
cp -rf /var/www/matsen-git-repository/matsen-tool-be/config/routes /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/config

rm -rf /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/config/bundles.php
cp -rf /var/www/matsen-git-repository/matsen-tool-be/config/bundles.php /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/config

rm -rf /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/config/preload.php
cp -rf /var/www/matsen-git-repository/matsen-tool-be/config/preload.php /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/config

rm -rf /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/config/routes.yaml
cp -rf /var/www/matsen-git-repository/matsen-tool-be/config/routes.yaml /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/config

rm -rf /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/config/services.yaml
cp -rf /var/www/matsen-git-repository/matsen-tool-be/config/services.yaml /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/config

rm -rf /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/bin
cp -rf /var/www/matsen-git-repository/matsen-tool-be/bin /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs

rm -rf /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/migrations
cp -rf /var/www/matsen-git-repository/matsen-tool-be/migrations /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs

rm -rf /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/src
cp -rf /var/www/matsen-git-repository/matsen-tool-be/src /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs

rm -rf /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/public/index.php
cp -rf /var/www/matsen-git-repository/matsen-tool-be/public/index.php /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/public

echo "$(tput setab 2)Files have been copied$(tput sgr 0)"

cd /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs
composer update --no-scripts

echo "$(tput setab 2)COMPOSER UPDATED updated$(tput sgr 0)"

php /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/bin/console doctrine:migrations:migrate

echo "$(tput setab 2)DATABASE SCHEMA updated$(tput sgr 0)"

cd /var/www/vhosts/spawntree.de/
sudo chmod 777 matsen-api.spawntree.de
cd /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/
sudo chmod 777 -R *

cd /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/var/cache/
rm -R *

php /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/bin/console cache:clear
php /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/bin/console cache:warmup

echo "$(tput setab 2)CACHE HAS BEEN CLEARED$(tput sgr 0)"

cd /var/www/vhosts/spawntree.de/matsen-api.spawntree.de/httpdocs/var/
sudo chmod 777 -R *
sudo chmod 777 cache/ *
sudo chmod 777 cache/

#service apache2 restart

#echo "$(tput setab 2)CACHE cleared$(tput sgr 0)"

echo "$(tput setab 7)$(tput setaf 1)THINK ABOUT POSSIBLE PATCHES!"

echo "You have updated matsen api!$(tput sgr 0)"
