#!/bin/bash

set -eo pipefail

#
# This script deploys the build artifact to Pantheon.
# On the master branch the dev environment is used.
# Otherwise a multidev environment is used.
#

# Authenticate with Terminus
terminus -n auth:login --machine-token="$TERMINUS_TOKEN"

if [[ $TERMINUS_ENV == ci-* ]]  &&  [[ $CI_BUILD == 'NO' ]]
then
  echo "We do not build CI environments. If you see this on a Pull Request, please rerun the CircleCI Workflow."
  terminus -n build:comment:add:commit --message "We do not build CI environments. If you see this on a Pull Request, please rerun the CircleCI Workflow."
  exit
fi


# Delete old multidev environments associated
# with a PR that has been merged or closed.
echo "Deleting old Multidevs for Closed PRs. Sending to Background."
( terminus -n build:env:delete:pr $TERMINUS_SITE --yes || true ) &

# Prepare for Pantheon
echo "Preparing Artifact for Pantheon."
composer -n run prepare-for-pantheon

echo "Building from $CI_BRANCH."
echo "The Main Branch is $MAIN_BRANCH."
if [[ "$CI_BRANCH" == "$MAIN_BRANCH" ]]
then
  echo "Building the Dev environment."
  if [[ "$CANONICAL_ENV" != "dev" ]]
  then
    terminus -n env:clone-content "$TERMINUS_SITE.$CANONICAL_ENV" dev
    if [[ ! -z "$SANATIZE_SCRIPT" ]]
    then
      echo "Sanatizing Database."
      # If we've set a sanatization script for the database, run it.
      $( $SANATIZE_SCRIPT )
    fi
  fi
  terminus -n build:env:push "$TERMINUS_SITE.dev" --yes
else
  # Create a new multidev environment (or push to an existing one)
  echo "Building Multidev $TERMINUS_ENV from $CANONICAL_ENV"
  if [[ "$TERMINUS_ENV" != "$DEVELOPMENT_ENV" ]] && [[ "$REBUILD_MULTIDEV_ENV_EVERY_PUSH" == "YES" ]]
  then
    echo "This project is set to clone content for multidevs every push."
    terminus -n build:env:create "$TERMINUS_SITE.$CANONICAL_ENV" "$TERMINUS_ENV" --yes --clone-content
    if [[ ! -z "$SANATIZE_SCRIPT" ]]
    then
      echo "Sanatizing Database."
      # If we've set a sanatization script for the database, run it.
      $( $SANATIZE_SCRIPT )
    fi
  elif [[ "$TERMINUS_ENV" == "$DEVELOPMENT_ENV" ]] && [[ "$REBUILD_DEVELOPMENT_ENV_EVERY_PUSH" == "YES" ]]
  then
    echo "This project is set to clone content for the github development multidev every push."
    terminus -n build:env:create "$TERMINUS_SITE.$CANONICAL_ENV" "$TERMINUS_ENV" --yes --clone-content
    if [[ ! -z "$SANATIZE_SCRIPT" ]]
    then
      echo "Sanatizing Database."
      # If we've set a sanatization script for the database, run it.
      $( $SANATIZE_SCRIPT )
    fi
  else
    terminus -n build:env:create "$TERMINUS_SITE.$CANONICAL_ENV" "$TERMINUS_ENV" --yes
  fi
fi

echo "Starting environment deploy commmands."
# Update the Drupal database
terminus -n drush "$TERMINUS_SITE.$TERMINUS_ENV" -- updatedb -y

# If exported configuration is available, then import it.
if [ -f "config/system.site.yml" ] ; then
  terminus -n drush "$TERMINUS_SITE.$TERMINUS_ENV" -- config-import --yes
fi

# Clear Drupal cache
terminus -n drush "$TERMINUS_SITE.$TERMINUS_ENV" -- cr

echo "Clearing the Edge cache."
# Clear the environment cache
terminus -n env:clear-cache $TERMINUS_SITE.$TERMINUS_ENV

# Ensure secrets are set
terminus -n secrets:set "$TERMINUS_SITE.$TERMINUS_ENV" token "$GITHUB_TOKEN" --file='github-secrets.json' --clear --skip-if-empty

echo "Done deploying. Your environment is ready for your review."
