#!/bin/sh

if test $# -ne 4; then
	echo "Usage: $0 <experiment-config> <peers-directory> <node-count> <experiment-length-in-min>"
	exit 1
fi

# expand experiment config filename to full path
cfg_name=$(basename $1)
cfg_path=$(dirname $1)
cd $cfg_path
cfg_path=$(pwd)
EXPERIMENT_CONFIG=$cfg_path/$cfg_name
source $EXPERIMENT_CONFIG # brings in $BRANCH

cd $2
PEERS_DIRECTORY=$(pwd)
NODE_COUNT=$3
EXPERIMENT_LENGTH=$4

current_hour=$(date +%H)
day_of_week=$(date +%u)
if test $current_hour -lt 20 && test $current_hour -gt 8 && test $day_of_week -lt 6 && test $EXPERIMENT_LENGTH -ge 15 ; then
    # according to: http://www.cs.vu.nl/das4/usage.shtml
    # working hours: Mon-Fri, 8:00 - 20:00
    echo "* Adjusting experiment length from $EXPERIMENT_LENGTH min to 14 min (it's working hours!)"
    EXPERIMENT_LENGTH=14
fi

rm -rf $PEERS_DIRECTORY/control
rm -f $PEERS_DIRECTORY/peers

rm -rf $PEERS_DIRECTORY/output
mkdir -p $PEERS_DIRECTORY/output

cd ${BRANCH}
echo "* Starting the DAS4 config sync server in the background..."
./das4-config-sync-server.py $PEERS_DIRECTORY/peer-keys & # it automatically stops after all peers received their config
CONFIG_SERVER_PID=$!
disown # remove job from the job table of the current bash process

cd $PEERS_DIRECTORY
PEER_COUNT=$(cat peer.count)

echo "* Starting experiment on $NODE_COUNT nodes ("$[$PEER_COUNT / $NODE_COUNT]" peers per node)..."

find . -regextype posix-egrep  -type f -regex '\./[0-9]{5}/dispersy\.db' -delete
find . -regextype posix-egrep  -type f -regex '\./[0-9]{5}/dprint\.conf' -delete

cd $BRANCH
qsub -t 1-${NODE_COUNT} das4-node $EXPERIMENT_CONFIG $PEERS_DIRECTORY $PEER_COUNT $EXPERIMENT_LENGTH

sleep 2

JOB_STATUS_FILE=/tmp/das4_jobstat_${USER}
do_kill=0
qstat | grep " ${USER} " > ${JOB_STATUS_FILE}
grep ' qw ' ${JOB_STATUS_FILE}
if test $? -eq 0; then
    do_kill=1
    echo
fi

grep ' qw ' ${JOB_STATUS_FILE} &> /dev/null
while test $? -eq 0; do
    echo "* Some jobs were queued. Deleting all jobs..."
    qstat | grep $USER | grep '@' | sed 's/.*\@\(node[0-9]\{3\}\).*/\1/g' | xargs -I A ssh A killall python
    sleep 1
    qstat | grep $USER | tee ${JOB_STATUS_FILE}
    sleep 1
    grep ${USER} ${JOB_STATUS_FILE} | grep $USER &> /dev/null # kill everything of this user
done
rm ${JOB_STATUS_FILE}


if test $do_kill -eq 0; then
    wait $CONFIG_SERVER_PID # we will get the output of the config server here
else
    echo "* Done killing all jobs."
    kill -9 $CONFIG_SERVER_PID &>/dev/null
    echo "* Config server killed."
fi

echo
qstat
