#!/bin/bash

# Put the application into maintenance mode
php artisan down

# Backup the database
php artisan brentwood:backup-database

# Pull the latest code from GitHub
#git pull origin master

# Optional: Run composer update (uncomment to enable)
# composer update --optimize-autoloader --no-dev

# Run database migrations
php artisan migrate

# Optional: Run npm install (uncomment to enable)
# npm install

# Build the frontend assets
sudo npm run build

# Clear and cache configurations, routes, and views
sudo -u www-data php artisan config:clear
sudo -u www-data php artisan cache:clear
sudo -u www-data php artisan event:cache
sudo -u www-data php artisan view:cache
sudo -u www-data php artisan route:cache

# Restart Supervisor services
sudo supervisorctl restart www-brentwood-ca-horizon
sudo supervisorctl restart all

# Bring the application back up
php artisan up

echo "Web server restart completed."
