#!/bin/bash

#
# This is run once after `composer create-project` to fix
# up the behat-pantheon.yml file.
#

# Use 'sed $INPLACE' for cross-platform 'sed -i'.
OS="$(uname -s)"
INPLACE="-i.dist"
if [ "$OS" == "Darwin" ]
then
  INPLACE="-i .dist"
fi

# Find the project base directory, where this script was executed from.
SELF_DIRNAME="`dirname -- "$0"`"

# The project base dir is the immediate parent of the 'bin' dir
PROJECT_BASE_DIR="`cd -P -- "$SELF_DIRNAME/.." && pwd -P`"

# The project name is the name of the directory that contains the project
PROJECT_NAME="$(basename "$PROJECT_BASE_DIR")"

# Defaults
ALIAS_GROUP=pantheon-ci
BASE_DOMAIN=pantheon.io
ENV=dev

# Fix up our behat-pantheon.yml configuration file
sed $INPLACE \
  -e "s#\(^[ \t]*base_url:\).*#\1 http://$ENV-$PROJECT_NAME.$BASE_DOMAIN#" \
  -e "s#\(^[ \t]*alias:\).*#\1 '@$ALIAS_GROUP.$PROJECT_NAME.$ENV'#" \
  $PROJECT_BASE_DIR/behat/behat-pantheon.yml

# Rename @localtest to @$PROJECT_NAME
sed $INPLACE \
  -e "s#^DEFAULT_ALIAS=.*#DEFAULT_ALIAS=@$PROJECT_NAME#" \
  -e "s#^DEFAULT_SITE_NAME=.*#DEFAULT_SITE_NAME=$PROJECT_NAME#" \
  $PROJECT_BASE_DIR/scripts/default-environment
sed $INPLACE \
  -e "s#\(^[ \t]*alias:\).*#\1 '@$PROJECT_NAME'#" \
  $PROJECT_BASE_DIR/behat/behat-ci.yml
sed $INPLACE \
  -e "s#\(^[ \t]*alias:\).*#\1 '@$PROJECT_NAME'#" \
  $PROJECT_BASE_DIR/behat/behat-local.yml

