#!/bin/bash

# Switch to 'deploy' user and run the commands
echo "Switching to 'deploy' user..."
sudo su - deploy << 'EOF'

# Switch to '/var/www/html' directory
echo "Changing directory to /var/www/html/..."
cd /var/www/html/ || { echo 'Failed to change directory'; exit 1; }

# Pull latest code from staging branch
echo "Pulling latest code from 'staging' branch..."
git pull origin staging

# Run Composer Install
echo "Running Composer install..."
composer install

# Run Drush cr
echo "Running Drush cache rebuild..."
vendor/drush/drush/drush cr

# Run Drush cim
echo "Importing configuration using Drush..."
vendor/drush/drush/drush cim -y

# Run Drush cr
echo "Rebuilding cache again..."
vendor/drush/drush/drush cr

echo "Deployment complete."

EOF