#!/bin/bash
# Setup script for the Cividesk platform
echo "Cividesk platform install ..."

CONFIG="composer config --no-interaction --working-dir=$GITROOT"
REQUIRE="composer require --working-dir=$GITROOT"

#
# Configure composer
#
${CONFIG} minimum-stability dev
${CONFIG} prefer-stable true

# composer/installers
${CONFIG} allow-plugins.composer/installers true
${REQUIRE} composer/installers

# composer-patches
# DO NOT update to master/2.x as it does not allow patches from dependencies anymore
${REQUIRE} cweagans/composer-patches:~1.7
${CONFIG} allow-plugins.cweagans/composer-patches true
${CONFIG} extra.enable-patching true

#
# Configure the CMS and set installer paths for CiviCRM
#
# --> Drupal
if [[ ! -z "${DRUPAL_DATABASE}" ]]; then
   ${CONFIG} extra.installer-paths --json --merge '{ "web/sites/default/civicrm-extend/{$name}": ["type:civicrm-ext", "type:civicrm-extend"] }';
   ${REQUIRE} cividesk/civicrm

# --> Wordpress
elif [[ ! -z "${WORDPRESS_DATABASE}" ]]; then
   # ATTENTION: mostly unused now as there are no composer package for WP core, so install via cividesk/wordpress image
   ${CONFIG} extra.installer-paths --json --merge '{ "web/wp-content/civicrm-extend/{$name}": ["type:civicrm-ext", "type:civicrm-extend"] }';

# --> Standalone!
else
   ${CONFIG} extra.installer-paths --json --merge '{ "web/ext/{$name}": ["type:civicrm-ext", "type:civicrm-extend"] }';
fi


# ignore CiviCRM advisories in CiviCRM
# TODO: only in 6.5
${CONFIG} --json policy.advisories.ignore '{"pontedilana/php-weasyprint":"Required by CiviCRM until upstream replaces or updates it."}'

#
# Configure CiviCRM
#
# Setup CiviCRM mandatory extensions
${REQUIRE} \
  civicrm/noverwrite \
  civicrm/easycopy \
  civicrm/org.civicrm.recentmenu \
  civicrm/com.aghstrategies.airmail \
  civicrm/uk.co.vedaconsulting.mosaico \
  civicrm/com.cividesk.email.mosaico.custom \
  civicrm/mjwshared \
  civicrm/firewall \
  civicrm/formprotection
  
${REQUIRE} civicrm/com.cividesk.commons

# Downloaded but not installed extensions
${REQUIRE} \
  civicrm/civirules \
  civicrm/nz.co.fuzion.extendedreport \
  civicrm/org.civicrm.module.cividiscount

# STOP here if we are in the testing environment
if [[ -z "${CIVICRM_DATABASE}" ]]; then
  exit;
fi

# Final message
echo "The Cividesk platform is installed, you should run cividesk-platform-configure"