#!/usr/bin/env bash

# Specific case to support s3fs.

set -ev

cd "$(dirname "$0")" || exit; source _includes.sh

source ${orca_includes}

[[ -d ${ORCA_FIXTURE_DIR} ]] && cd ${ORCA_FIXTURE_DIR}

drupal_version=$(get_drupal_version_using_composer)
echo "Drupal version: $drupal_version"

if [[ -z "${drupal_version}" ]]; then
  echo "No fixtures generated; exiting."
  exit 0
fi

# This is needed until the fix is merged into entity_reference_revisions.
# https://www.drupal.org/project/entity_reference_revisions/issues/3039442.
composer require drupal/entity_reference_revisions:~1.12.0

# Remove acquia_contenthub_s3 content due to incompatibilities with s3fs and higher drupal versions.
if [[ ! ${drupal_version} =~ ^(8\.8\..*|9\..*|10\.[0-6]\..*|11\.0\..*|11\.1\..*|11\.2\.([3-9]|[1-9][0-9]+).*|11\.3\..*)$ ]]; then
  echo "Removing acquia_contenthub_s3 module due to drupal/s3fs unavailability."
  rm -Rf /ramfs${ORCA_SUT_DIR}/modules/acquia_contenthub_s3/*
  exit 0
else
  composer require drupal/s3fs:3.x-dev@dev -W
  # Enable if there's an active Drupal installation available
  /ramfs/acquia/orca-build/vendor/bin/drush pm:enable s3fs acquia_contenthub_s3 2> /dev/null || echo 'S3fs and acquia_contenthub_s3 have not been enabled!'
fi
