#!/bin/bash

if [ -d "./behat" ]; then
    status_message "Your Behat directory is already configured." warning
    exit
fi

if [ ! -n "${url}" ]; then
    status_message "You need to specify the URL of a project using the \"--url=http://example.com\" parameter." error
fi

if [ ! -d "${package_dir}/behat" ]; then
    status_message "Something went wrong. Try to reinstall the package. :(" error
fi

cp -r ${package_dir}/behat ./
rm behat/resources/README.md

project_dir=`pwd`/
project_exist=false

if [ -f "${project_dir}index.php" ]; then
    project_exist=true
else
    if [ -n "${dir}" ]; then
        project_dir+="${dir}/"

        if [ -f "${project_dir}/index.php" ]; then
            project_exist=true
        fi
    fi
fi

project_dir=${project_dir%/}

if ${project_exist} && ! grep -Fq 'DRUPAL_ROOT' ${project_dir}/index.php; then
    project_exist=false
fi

if ${project_exist}; then
    replace_in_file '<DRUPAL_ROOT>' ${project_dir} behat/behat.yml
    replace_in_file '<BASE_URL>' ${url} behat/behat.yml

    status_message "Behat configuration was created sucessfully!"
else
    status_message "The Drupal was not installed here: ${project_dir}" error
fi
