#!/bin/bash

###################################################
# Script to run behat and unit tests.
###################################################

########### Variables used ##############
# Path of moodle.
MOODLE_PATH='/var/www/html/m30/'

# Selenium to use.
SELENIUM_DOCKER='selenium/standalone-firefox:2.53.0'
##SELENIUM_DOCKER='selenium/standalone-chrome:2.53.0'
##SELENIUM_DOCKER='rajeshtaneja/selenium:2.53.0'

# Fail dump to save in
MAP_FAILDUMP='/home/rajesh/Desktop/faildump'

# Php version to use.
PHP_SERVER_DOCKER='rajeshtaneja/php:7.0.4'
##PHP_SERVER_DOCKER='rajeshtaneja/php:5.4.45'
##PHP_SERVER_DOCKER='rajeshtaneja/php:5.5.9'
##PHP_SERVER_DOCKER='rajeshtaneja/php:5.6.22'

MOODLE_BRANCH='31'
TEST_TO_RUN='behat'
#EXTRA_OPT='--tags=@mod_assign'
## Database activities.
DBTYPE='pgsql'
DBHOST='raji.per.in.moodle.com'
DBUSER='moodle'
DBPASS='moodle'
DBNAME='firefox'
DBPREFIX='b_'
#PROFILE='chrome'
PROFILE='default'
#PROFILE='phantomjs-selenium'
#PROFILE='phantomjs'
RUN=0
TOTAL_RUNS=3
DOCKER_MOODLE_PATH=/moodle
#EXTRA_OPT='--tags=@mod_feedback'
###################################################
# Optional Params.
if [ -z ${DBNAME} ]; then
    DBNAME=${SiteId}
fi
if [ -z ${DBPREFIX} ]; then
    DBPREFIX=mdl_
fi

# Select db to use today.

if [ -n "$dbtorun" ]; then
	dbtorun=(`echo ${dbtorun}`);
	mysql=(`echo ${mysql}`);
	mariadb=(`echo ${mariadb}`);
	pgsql=(`echo ${pgsql}`);
	oracle=(`echo ${oracle}`);
	mssql=(`echo ${mssql}`);
	# Find which db to run today.
	dayoftheweek=`date +"%u"`
	if [[ -z ${dbtorun} ]]; then
	  dbtouse=pgsql
	else
	  dbtouse=${dbtorun[ $(( ${dayoftheweek} - 1 )) ]}
	fi

	dbtouse=pgsql
	eval dbtousedetails="(\"\${$dbtouse[@]}\")"

	# Set db values.
	for dbtouse in ${dbtousedetails[@]} ; do
	    KEY=${dbtouse%%:*}
	    VALUE=${dbtouse#*:}
	    eval ${KEY}=${VALUE}
	done

	# Get total runs.
	if [[ -z ${PROCESS} ]]; then
	  RUN=1
	  TOTAL_RUNS=1
	else
	  RUN=`echo $PROCESS | cut -d '/' -f 1`
	  TOTAL_RUNS=`echo $PROCESS | cut -d '/' -f 2`
	fi

	DBTYPE=${dbtype}
	DBHOST=${dbhost}
	if [[ -z ${dbname} ]]; then
	  DBNAME=moodle
	else
	  DBNAME=${dbname}
	fi
	if [[ -z ${dbuser} ]]; then
	  DBUSER=moodle
	else
	  DBUSER=${dbuser}
	fi
	if [[ -z ${dbpass} ]]; then
	  DBPASS=moodle
	else
	  DBPASS=${dbpass}
	fi
	if [[ -z ${dbprefix} ]]; then
	  DBPREFIX=b_
	else
	  DBPREFIX=${dbprefix}
	fi
fi

# If db is oci or mssql then use old version.
if [ "$DBTYPE" == "mssql" ] || [ "$DBTYPE" == "oci" ]; then
    if [ "$PHP_SERVER_DOCKER" == "rajeshtaneja/php:7.0.4" ]; then
        #PHP_SERVER_DOCKER='rajeshtaneja/php:5.4.45'
        PHP_SERVER_DOCKER='rajeshtaneja/php:5.5.9'
    fi
fi

# Create a mapping of moodle directory if not available
if [ "$TEST_TO_RUN" == "behat" ]; then
    RUN_DIR_MAP="${MOODLE_PATH}${TEST_TO_RUN}_${MOODLE_BRANCH}_${PROFILE}_${RUN}"
else
    RUN_DIR_MAP="${MOODLE_PATH}${TEST_TO_RUN}_${MOODLE_BRANCH}_${DBTYPE}"
fi

#if [ ! -d "$RUN_DIR_MAP" ]; then
#    echo "Creating the Overlay directory for Run $RUN at $RUN_DIR_MAP"
#    mkdir -p $RUN_DIR_MAP
#    chmod 777 $RUN_DIR_MAP
#    sudo mount -t aufs -o br=${RUN_DIR_MAP}=rw:${MOODLE_PATH}=ro none ${RUN_DIR_MAP}
#fi

# Start moodle test.
NAME_OF_DOCKER_CONTAINER=`echo "$RUN_DIR_MAP" | sed 's,/,_,g' | sed 's/_//1'`

whereami="${PWD}"
    cd $MOODLE_PATH

    if [ ! -f "$MOODLE_PATH/composer.phar" ]; then
        curl -s https://getcomposer.org/installer | php
    fi

    php composer.phar install --prefer-dist --no-interaction
cd $whereami

if [ "$TEST_TO_RUN" == "behat" ]; then
        if [ "$PROFILE" == "phantomjs" ] || [ "$PROFILE" == "phantomjs-selenium" ]; then
            # Start phantomjs instance.
            DOCKER_SELENIUM_INSTANCE=$(docker run -d -v /dev/shm:/dev/shm -v ${MOODLE_PATH}:/var/www/html/moodle $SELENIUM_DOCKER /run.sh $PROFILE)
            LINK_SELENIUM="--link ${DOCKER_SELENIUM_INSTANCE}:SELENIUM_DOCKER"
            sleep 5
            SELENIUMIP=$(docker inspect -f "{{ .NetworkSettings.Networks.bridge.IPAddress }}" $DOCKER_SELENIUM_INSTANCE)
            if [ "$PROFILE" == "phantomjs" ]; then
                SELENIUMURL="--phantomjsurl=${SELENIUMIP}:4443"
            else
                SELENIUMURL="--seleniumurl=${SELENIUMIP}:4444"
            fi
        else
            # Start selenium instance.
            DOCKER_SELENIUM_INSTANCE=$(docker run -d -v /dev/shm:/dev/shm -v ${MOODLE_PATH}:/var/www/html/moodle $SELENIUM_DOCKER /run.sh $PROFILE)
            LINK_SELENIUM="--link ${DOCKER_SELENIUM_INSTANCE}:SELENIUM_DOCKER"
            sleep 5
            SELENIUMIP=$(docker inspect -f "{{ .NetworkSettings.Networks.bridge.IPAddress }}" $DOCKER_SELENIUM_INSTANCE)
            SELENIUMURL="--seleniumurl=${SELENIUMIP}:4444"
        fi

        # Start moodle test.
        NAME_OF_DOCKER_CONTAINER=`echo "$RUN_DIR_MAP" | sed 's,/,_,g' | sed 's/_//1'`
        docker run -it --rm --user=rajesh --name ${NAME_OF_DOCKER_CONTAINER} -v ${MOODLE_PATH}:${DOCKER_MOODLE_PATH} -v ${MAP_FAILDUMP}:/shared ${LINK_SELENIUM} ${PHP_SERVER_DOCKER} /scripts/behat.sh --dbtype=${DBTYPE} --dbhost=${DBHOST} --dbname=${DBNAME} --behatdbprefix=${DBPREFIX} --dbuser=${DBUSER} --dbpass=${DBPASS} --profile=${PROFILE} --process=${RUN} --processes=${TOTAL_RUNS} ${SELENIUMURL} $EXTRA_OPT
else
        docker run -it --rm --user=rajesh --name ${NAME_OF_DOCKER_CONTAINER} -v ${MOODLE_PATH}:${DOCKER_MOODLE_PATH} ${LINK_SELENIUM} ${PHP_SERVER_DOCKER} /scripts/phpunit.sh --dbtype=${DBTYPE} --dbhost=${DBHOST} --dbname=${DBNAME} --phpunitdbprefix=${DBPREFIX} --dbuser=${DBUSER} --dbpass=${DBPASS} $EXTRA_OPT
fi

EXITCODE=$?

# Cleanup all the docker images here.
function finish {
    echo "Stopping docker images..."
    if [ -n "$LINK_SELENIUM" ]; then
        docker stop $DOCKER_SELENIUM_INSTANCE
        docker rm -f $DOCKER_SELENIUM_INSTANCE
    fi
}

trap finish EXIT

exit $EXITCODE

