#!/bin/bash
# This script is used to deploy configuration changes and database updates to the Drupal site.
# This script should be run after the codebase has been updated and the database has been backed up.
#
# Usage: bin/deploy-config-db-updates /home/elixireurope/elixir-europe.org
#
# Parameters:
#   - $1: The path to the project directory.
#
# Please use envrionment wise path for the project

# Set the path to the Drupal & vendor directory
# Check if the script received the required parameters
if [ "$#" -ne 1 ]; then
  echo "Usage: $0 <project_path>"
  echo ""
  echo "Example: $0 /home/elixireurope/elixir-europe.org"
  echo ""
  exit 1
fi

# Set the path to the Drupal & vendor directory
PROJECT_PATH="$1"
PROJECT_ROOT_PATH="${PROJECT_PATH}/web"
PROJECT_VENDOR_PATH="${PROJECT_PATH}/vendor"

# Check if PROJECT_PATH and PROJECT_ROOT_PATH are not empty
if [ -z "$PROJECT_PATH" ] || [ -z "$PROJECT_ROOT_PATH" ]; then
  echo "Error: PROJECT_PATH or PROJECT_ROOT_PATH is empty."
  exit 1
fi

# Define the drush command
DRUSH_CMD="${PROJECT_VENDOR_PATH}/bin/drush --root=${PROJECT_ROOT_PATH} --yes"
# Import configuration
$DRUSH_CMD config-import

# Run database updates
$DRUSH_CMD updatedb --no-cache-clear

# Clear cache
$DRUSH_CMD cache:rebuild

# Import configuration again after update database
$DRUSH_CMD config-import

# Clear cache
$DRUSH_CMD cache-rebuild

echo "Drupal configuration import, cache clear, and database updates completed."
