#!/bin/bash

export COMPOSER_ALLOW_SUPERUSER=1

# Postavljanje promenljivih za boje
BR='\033[1;91m' # Bold Red
BB='\033[1;94m' # Bold Blue
BG='\033[1;92m' # Bold Green
BC='\033[1;96m' # Bold Cyan
# Background color white
BGW='\033[1;97m' # Bold White
BLR='\033[1;91m' # Bold Light Red
BLG='\033[1;92m' # Bold Light Green
NC='\033[0m' # No Color
echo -e "\n${BB}Runing folder rights and permission script${NC} \n"
# Pokretanje skripte za postavljanje prava i vlasništva
sh website.sh
sleep 0.2
echo -e "\n${BGW}Permissions and ownerships are set${NC} \n"
echo -e "\n${BR}Optimize scritp and folder rights and permissions${NC} \n"

# Čišćenje svih cacheova
php artisan route:clear
php artisan view:clear
php artisan event:clear
php artisan config:clear
php artisan cache:clear
php artisan clear-compiled
php artisan optimize:clear
sleep 0.2
# Node and Vite.js permissions
echo -e "\n${BB}Node JS and VITE${NC} \n"
sudo chown -R fitness:fitness /home/fitness/public_html/laravel-website/node_modules
sudo chmod -R u+x /home/fitness/public_html/laravel-website/node_modules/.bin/
# Dodavanje izvršnih dozvola za Vite.js
sudo chmod +x /home/fitness/public_html/laravel-website/node_modules/vite/bin/vite.js
# Dodavanje izvršnih dozvola za esbuild
sudo chmod +x /home/fitness/public_html/laravel-website/node_modules/@esbuild/linux-x64/bin/esbuild
sleep 0.2
# Ažuriranje Node.js paketa
ncu -u
npm install
echo -e "\n${BG}Node.js packages and dependecies are updated${NC} \n"
sleep 0.2
npm run build
echo -e "\n${BG}Laravel App - JS and CSS files are built and compiled${NC} \n"
# Ažuriranje Composer paketa
composer update -n
# Ponovno generiranje autoloada
composer dump-autoload
sleep 0.2
# Optimizacija i ponovno keširanje
php artisan optimize
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache
sleep 0.2
cd /home/fitness/public_html/laravel-website
sudo chown -R fitness:fitness /home/fitness/public_html/laravel-website
sudo chown -R fitness:fitness /home/fitness/public_html/laravel-website/storage
sudo chown -R fitness:fitness /home/fitness/public_html/laravel-website/bootstrap
echo -e "\n${BGW}Optimizing and caching of website are done${NC} \n"
