#!/bin/bash

################################################################################
################################################################################
########## This is the main script for the automated xmpptest ##################
################################################################################
################################################################################

# set working directory
WORKING_DIR=/tmp/xmpptest

# execute testing scripts
echo -e "\n-------- Testing sqlite backend --------\n"
SQLITE_START=$SECONDS
$1/xmpptest-main-sqlite $2 $WORKING_DIR
SQLITE_DUR=$(( SECONDS - $SQLITE_START ))

# wait until ports are available again
if [ $2 -gt 27000 ]; then
	echo "wait until ports are available again..."
	sleep 60
fi

echo -e "\n-------- Testing berkeley db backend --------\n"
BERKELEY_START=$SECONDS
$1/xmpptest-main-berkeleydb $2 $WORKING_DIR
BERKELEY_DUR=$(( SECONDS - $BERKELEY_START ))

# successfully created/connected user
NUMBER_USERS=$((`tail -1 $WORKING_DIR/test.output | cut -f1 -d ' '` +1))

# display output and append it to file
echo -e "$2 ($NUMBER_USERS) testuser:\tsqlite needed $SQLITE_DUR seconds and berkeley db needed $BERKELEY_DUR seconds" | tee -a $WORKING_DIR/duration-xmpp
sort -n $WORKING_DIR/duration-xmpp -o $WORKING_DIR/duration-xmpp
