#!/bin/bash

# Variables
PROJECT_DOCUMENT_ROOT="/home/tenddev/public_html/L/landandroots.com/landandroots.com_web/"
WEB_APP_SERVICE_NAME="landandroots_web"

# Navigate to your project directory
cd "$PROJECT_DOCUMENT_ROOT" || exit

# Install the necessary dependencies for the shared app
echo "Installing node modules"
npm install

echo "Sourcing NVM..."
source ~/.bashrc
source ~/.nvm/nvm.sh

# Build all applications
echo "Building web app please wait..."
npm run build
if [ $? -ne 0 ]; then
    echo "Build failed for shared app."
    exit 1
fi

# Function to restart an app
restart_app() {
    echo "Restarting $1..."
    pm2 restart "$1"
    echo "Waiting for 5 seconds before restarting the next app..."
    sleep 5  # Wait for 5 seconds
}

# Restart all applications
restart_app "$WEB_APP_SERVICE_NAME"

echo "All PM2 Services restarted successfully!"
