#!/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
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 and spin up PHP's built-in webserver
cd $PROJECT_BASE_DIR/drupal
drush site-install -y 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
