#!/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 master branch
echo "Pulling latest code from 'master' branch..."
git pull origin master

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

# Now switch to root and run secondary maintenance tasks along with drush operations
echo "*** NOTE: CACHE CLEAR AND DB OPERATIONS MUST RUN POST ALL VMs ARE UP WITH AUTO SCALE SCRIPT ***"
echo "Follow the steps mentioned below to switch to root user and run auto-scale script ==="
echo "Step1: sudo - i" 
echo "Step2: /srv/script/auto-scale.sh"
echo "Step3: Once all new VMs are up and running, perform cache clear or config import operations using deploy user"
echo "*** NOTE: PERFORM CACHE CLEAR AND DB OPERATIONS MUST RUN POST ALL VMs ARE UP WITH AUTO SCALE SCRIPT ***"
echo "*** Additional Information for drush commands ***"
echo "===From deploy user run below commands based on need==="
echo "===Cache clear: vendor/drush/drush/drush cr"
echo "===Config Import: vendor/drush/drush/drush cim"

EOF