#!/usr/bin/env bash
IFS=$'\n\t'
set -euo pipefail

#
# GovCMS cache rebuild.
#
# Allow a cache-rebuild to be toggled at deploy time.
#

LAGOON_ENVIRONMENT_TYPE=${LAGOON_ENVIRONMENT_TYPE:-production}
GOVCMS_SKIP_CACHE_REBUILD=${GOVCMS_SKIP_CACHE_REBUILD:-}

# Drush 12 support.
DRUSH="${GOVCMS_DRUSH:-none}"
if [ "$DRUSH" == "none" ]; then
  DRUSH=$(which /app/vendor/bin/drush > /dev/null 2>&1 && echo "/app/vendor/bin/drush" || echo "/usr/local/bin/drush")
fi

echo "GovCMS Deploy :: Cache rebuild"

if [ -n "${GOVCMS_SKIP_CACHE_REBUILD}" ]; then
  echo "[skip]: Cache rebuild disabled."
  exit 0
fi

"$DRUSH" -y cache:rebuild

echo "[success]: Completed successfully."
