#!/bin/bash
#
# install-drupal
#
# Prepare a Drupal site to run the behat tests on.
#

SELF_DIRNAME="`dirname -- "$0"`"
PROJECT_BASE_DIR="`cd -P -- "$SELF_DIRNAME/.." && pwd -P`"

# Set up our $PATH
export PATH="$PROJECT_BASE_DIR/bin:$HOME/bin:$PATH"

# Fix bug in our custom installer
if [ -d $PROJECT_BASE_DIR/drupal/sites/sites/default ]
then
  mv $PROJECT_BASE_DIR/drupal/sites/sites/default $PROJECT_BASE_DIR/drupal/sites/default
fi

if [ ! -f $PROJECT_BASE_DIR/drupal/sites/default/default.settings.php ]
then
  echo "No default.settings.php file; did you run composer install?"
  exit 1
fi

# Do the settings.php shuffle for an empty settings.php
# This prevents permissions issues with the sites/default directory
chmod +w $PROJECT_BASE_DIR/drupal/sites/default
chmod +w $PROJECT_BASE_DIR/drupal/sites/default/settings.php
cp $PROJECT_BASE_DIR/drupal/sites/default/default.settings.php $PROJECT_BASE_DIR/drupal/sites/default/settings.php

# Set up some variables to match the environment
DBURL=mysql://root@localhost/drupal
INSTALLNAME="$SITE_NAME Local CI Test Site"
if [ -n "$TRAVIS" ]
then
  INSTALLNAME="$SITE_NAME Travis CI Test Site"
fi
if [ -n "$CIRCLECI" ]
then
  DBURL=mysql://ubuntu@127.0.0.1/circle_test
  INSTALLNAME="$SITE_NAME Circle CI Test Site"
fi

# Use Drush to install Drupal
cd $PROJECT_BASE_DIR/drupal
drush site-install -y "${PROFILE:-standard}" --site-name="$INSTALLNAME" --db-url="$DBURL" --account-name=admin --account-pass=admin
if [ $? != 0 ]
then
  echo "Could not install Drupal" >&2
  exit 1
fi

# Add in include of settings.local.php to the settings.php file.
chmod +w $PROJECT_BASE_DIR/drupal/sites/default
chmod +w $PROJECT_BASE_DIR/drupal/sites/default/settings.php
grep -q 'settings.local.php' "$PROJECT_BASE_DIR/drupal/sites/default/settings.php"
if [ $? != 0 ]
then
  cat << __EOF__ >> "$PROJECT_BASE_DIR/drupal/sites/default/settings.php"
\$local_settings = __DIR__ . "/settings.local.php";
if (is_file(\$local_settings)) {
  include "\$local_settings";
}
__EOF__
fi

# If there is a local script 'install-configuration', then run it.
INSTALL_CONFIGURATION="$PROJECT_BASE_DIR/scripts/install-configuration"
if [ -f "$INSTALL_CONFIGURATION" ]
then
  "$INSTALL_CONFIGURATION"
fi
