#!/bin/bash
########
# Cron Script to run Sahana Eden Test Suite
########

function repop_database
{
	template1=$1
	# Setting the template
	echo "`date` Setting the template as "$template1"....." >> /tmp/CRON_RUN
	sed -i "s/settings.base.template = \".*\"/settings.base.template = \""$template1"\" /" /home/web2py/applications/eden/models/000_config.py

	# Delete prepopulated data
	echo "`date` databases Deleted....." >> /tmp/CRON_RUN
	rm -rf /home/web2py/applications/eden/databases/*

	echo "`date` Repopulating the databases......" >> /tmp/CRON_RUN
	# Prepop database
	cd /home/web2py
	python web2py.py -S eden -M -R applications/eden/static/scripts/tools/noop.py
	chown -R web2py /home/web2py

	# Kill the current instance of web2py
	echo "`date` Killing the current instance of web2py....." >> /tmp/CRON_RUN
	kill -9 `ps uax | grep "python /home/web2py/web2py.py -a iiit123" | awk '{print $2;}'`
}

function run_selenium_tests
{
	template=$1
	echo "`date` Template is "$template" " >> /tmp/CRON_RUN
	# Repoulate the database and start a w2p instance.
	repop_database IFRC

	# Setting the template
	echo "`date` Setting the template as "$template"....." >> /tmp/CRON_RUN
	sed -i "s/settings.base.template = \".*\"/settings.base.template = \""$template"\" /" /home/web2py/applications/eden/models/000_config.py

	## Start a custom web2py server
	echo "`date` Restarting a custom web2py server....." >>  /tmp/CRON_RUN
	python /home/web2py/web2py.py -a iiit123 &
	echo "Sleeping for 5 seconds....."
	sleep 5

	# Run Selenium Test Suite on template
	echo "`date` Running Selenium Tests on "$template" template......" >> /tmp/CRON_RUN
	cd /home/web2py
	xvfb-run -a python web2py.py -S eden -M -R applications/eden/modules/tests/suite.py -A --html-path /var/spool/RESULTS/SELENIUM/$template -V 2 --browser chrome >> /tmp/CRON_RUN
}

function run_roles_tests
{
	template=$1
	
	echo "`date` Template is "$template" " >> /tmp/CRON_RUN
	# Repoulate the database and start a w2p instance.
	repop_database $template
	
	# Setting the template
	echo "`date` Setting the template as "$template"....." >> /tmp/CRON_RUN
	sed -i "s/settings.base.template = \".*\"/settings.base.template = \""$template"\" /" /home/web2py/applications/eden/models/000_config.py
	
	## Start a custom web2py server
	echo "`date` Restarting a custom web2py server....." >>  /tmp/CRON_RUN
	python /home/web2py/web2py.py -a iiit123 &
	echo "Sleeping for 5 seconds....."
	sleep 5
	
	# Run Roles Test Suite on template
	echo "`date` Running Roles Tests on "$template" template......" >> /tmp/CRON_RUN
	xvfb-run -a python web2py.py -S eden -M -R applications/eden/modules/tests/suite.py -A --suite roles --browser=chrome --html-path /var/spool/RESULTS/ROLES/$template >> /tmp/CRON_RUN

}

function run_smoke_tests
{
	template=$1

	if [ $template == "default" ] || [ $template == "SandyRelief" ]
	then
		repop_database IFRC
	else
		repop_database $template
	fi

	# Setting the template
	echo "`date` Setting the template as "$template"....." >> /tmp/CRON_RUN
	sed -i "s/settings.base.template = \".*\"/settings.base.template = \""$template"\" /" /home/web2py/applications/eden/models/000_config.py

	## Start a custom web2py server
	echo "`date` Restarting a custom web2py server....." >>  /tmp/CRON_RUN
	python /home/web2py/web2py.py -a iiit123 &
	echo "Sleeping for 5 seconds....."
	sleep 5
	
	# Run Smoke Test on template (on linkdepth max)
	echo "`date` Running Smoke Tests on "$template" template......" >> /tmp/CRON_RUN
	cd /home/web2py
	xvfb-run -a python web2py.py -S eden -M -R applications/eden/modules/tests/suite.py -A -V 2 --suite smoke --html-path /var/spool/RESULTS/SMOKE/$template --browser chrome >> /tmp/CRON_RUN
}

# Initialise Log
echo "Started running at : `date`" >> /tmp/CRON_RUN
echo "`whoami`" >> /tmp/CRON_RUN
echo "`which python`" >> /tmp/CRON_RUN

# Kill any previous web2py instance running
kill -9 `ps uax | grep "python /home/web2py/web2py.py -a iiit123" | awk '{print $2;}'`

# Update web2py
# DISABLED
#cd /home/web2py
#git reset --hard HEAD
#git checkout master
#git pull

# Update Eden to current Trunk
cd /home/web2py/applications/eden
git reset --hard HEAD
git checkout master
git pull upstream master

## use the python from pythonbrew 
[[ -s "$HOME/.pythonbrew/etc/bashrc" ]] && source "$HOME/.pythonbrew/etc/bashrc"
/root/.pythonbrew/bin/pythonbrew switch 2.7.2

echo "`which python`" >> /tmp/CRON_RUN

###################################################################################
# Run tests

run_selenium_tests IFRC
run_smoke_tests IFRC
run_roles_tests IFRC
run_selenium_tests default
run_smoke_tests default
run_selenium_tests SandyRelief
run_smoke_tests SandyRelief
run_selenium_tests DRMP
run_smoke_tests DRMP
run_selenium_tests CRMT
run_smoke_tests CRMT
run_smoke_tests DRRPP
run_roles_tests DRRPP

###################################################################################

# Mail results and a brief summary to the mailinglist
/usr/local/bin/mailTestResults.sh

# Kill any previous web2py instance running
kill -9 `ps uax | grep "python /home/web2py/web2py.py -a iiit123" | awk '{print $2;}'`

# Kill the instance of chromedriver
killall -9 chromedriver

echo "Stopped running at : `date`" >> /tmp/CRON_RUN

# Save the last 2000 lines in CRON_RUN, so that file size does not keep growing
tail -2000 /tmp/CRON_RUN > /tmp/temp
mv /tmp/temp /tmp/CRON_RUN
