#!/bin/bash
#==============================================================================#
# PHP tarantool test suite runner
#==============================================================================#

#------------------------------------------------------------------------------#
# Constants
#------------------------------------------------------------------------------#

# Success constant
SUCCESS=0
# Failure constant
FAILURE=1

# main test directory
TEST_HOME_DIR=`pwd`
# resource directory
RESOURCE_DIR="$TEST_HOME_DIR/share"
# running directory
RUNNING_DIR="$TEST_HOME_DIR/var"

# php.ini file
PHP_INI="$RESOURCE_DIR/php.ini"

# Tarantool configuration file
TARANTOOL_BOX_CFG="$RESOURCE_DIR/tarantool_box.cfg"

# Tarantool pid file
TARANTOOL_BOX_PID="$RUNNING_DIR/tarantool_box.pid"


#------------------------------------------------------------------------------#
# Suite runner functions
#------------------------------------------------------------------------------#

# initialize suite
function init_suite()
{
	# making running directory
	mkdir -p $RUNNING_DIR

	#
	# Tarantool binary
	#

	# check TARANTOOL_HOME variable
	if [ ! -z $TARANTOOL_BOX_BIN ]; then
		# Use user-defined path
		tarantool_box_bin="$TARANTOOL_BOX_BIN"
    else
		# try to find by standard paths
		tarantool_box_bin="tarantool_box"
	fi

	# checking binary
	if ! which $tarantool_box_bin > /dev/null 2>&1; then
		echo "error: can't find Tarantool binary file"
		exit $FAILURE
	fi

	#
	# PHP/Pear
	#

	# checking pear
	if ! which pear > /dev/null 2>&1; then
		echo "error: can't find pear"
		exit $FAILURE
	fi

	#
	# Module shared library
	#

	# checking tarantool module library
	if [ ! -f ../modules/tarantool.so ]; then
		echo "error: can't find tarantool module library"
		exit $FAILURE
	fi

	# checking old tarantool module file
	if [ -f tarantool.so ]; then
		rm -f tarantool.so
	fi

	# making symlink for tarantool.so to test directory
	cp -s ../modules/tarantool.so .

	#
	# tarantool_box.cfg
	#

	# checking tarantool_box.cfg file
	if [ ! -f $RESOURCE_DIR/tarantool_box.cfg ]; then
		echo "error: can't find tarantool_box.cfg"
		exit $FAILURE
	fi

	# checking old tarantool_box.cfg file
	if [ -f $RUNNING_DIR/tarantool_box.cfg ]; then
		rm -f $RUNNING_DIR/tarantool_box.cfg
	fi

	# making symlink for tarantool_box.cfg to running directory
	ln -s $RESOURCE_DIR/tarantool_box.cfg $RUNNING_DIR/tarantool_box.cfg

	#
	# init.lua
	#

	# checking init.lua file
	if [ ! -f $RESOURCE_DIR/init.lua ]; then
		echo "error: can't find init.lua"
		exit $FAILURE
	fi

	# checking old init lua file
	if [ -f $RUNNING_DIR/init.lua ]; then
		rm $RUNNING_DIR/init.lua
	fi

	# making symlink for init.lua to running directory
	ln -s $RESOURCE_DIR/init.lua $RUNNING_DIR/init.lua

	echo "--------------------------------------------------------------------------"
	echo "Tarantool information"
	echo "--------------------------------------------------------------------------"
	echo "Binary:   $tarantool_box_bin"
	echo "Version: `$tarantool_box_bin --version`"
	echo "--------------------------------------------------------------------------"

	return $SUCCESS
}


# clean-up tarantool
function cleanup_suite()
{
	# deleting pid
	rm -f $RUNNING_DIR/*.pid
	# deleting xlogs
	rm -f $RUNNING_DIR/*.xlog
	# deleting snaps
	rm -f $RUNNING_DIR/*.snap
	# deleting configuration file
	rm -f $RUNNING_DIR/*.cfg
	# deleting init.lua
	rm -f $RUNNING_DIR/*.lua
	# deleting module library
	rm -f tarantool.so
}

# initialize tarantool's storage
function tarantool_init_storage()
{
	$tarantool_box_bin --init-storage -c $TARANTOOL_BOX_CFG 1> /dev/null 2>&1
	return $SUCCESS
}

# start tarantool
function tarantool_start()
{
	if [ -f $TARANTOOL_BOX_PID ]; then
		tarantool_stop
		cleanup_suite
	fi

	# run tarantool to background
	$tarantool_box_bin -c $TARANTOOL_BOX_CFG &
	# wait pid file
	for i in {1..500}; do
		if [ -f $TARANTOOL_BOX_PID ]; then
			break
		fi
		sleep 0.01
	done

	if [ ! -f $TARANTOOL_BOX_PID ]; then
		echo "error: can't start tarantool"
		cleanup_suite
		exit $FAILURE
	fi

	return $SUCCESS
}

# stop tarantool
function tarantool_stop()
{
	if [ ! -f $TARANTOOL_BOX_PID ]; then
		return $SUCCESS
	fi

	# get tarantool pid form pid file
	pid=`cat $TARANTOOL_BOX_PID`

	# kill process via SIGTERM
	kill -TERM $pid 1> /dev/null 2>&1

	# Waiting 5 second
	for i in {1..500}; do
		# The server should delete the pid file.
		if [ ! -f $TARANTOOL_BOX_PID ]; then
			# tarantool successfully stopped
			return $SUCCESS
		fi
		sleep 0.01
	done

	if [ -f $TARANTOOL_BOX_PID ]; then
		# The server haven't shutdowed. Probably it's hanged up,
		# sending SIGKILL to it.
		kill -KILL $pid 1> /dev/null 2>&1
	fi

	return $SUCCESS
}


#------------------------------------------------------------------------------#
# run test scrip body
#------------------------------------------------------------------------------#

#
# initialize
#

echo "initialize suite ..."
# initializing suite
init_suite
# initializing storage
tarantool_init_storage

echo "starting tarantool ..."
# start tarantool
tarantool_start


#
# run
#

printf "\n"
printf "================================= PHP test ===============================\n"

# running pear's regression test scrips
PHPRC=$PHP_INI pear run-tests $1

printf "==========================================================================\n"
printf "\n"

#
# stop & clean-up
#

echo "stopping tarantool ..."
# stop tarantool
tarantool_stop

echo "cleaning up suite ..."
# clean-up suite
cleanup_suite

#
# Local variables:
# tab-width: 4
# c-basic-offset: 4
# End:
# vim600: noet sw=4 ts=4 fdm=marker
# vim<600: noet sw=4 ts=4
#
