#!/bin/bash
GFORTH=${GFORTH:-gforth}
count=${count:-100}
while [ "$1" != "${1#+}" ]
do
    debugs="$debugs $1"
    shift
done
rm -rf chat-test.d
mkdir -p chat-test.d/.net2o
cd chat-test.d
cat >.net2o/config <<EOF
.net2o="$PWD/.net2o"
keys="$PWD/.net2o/keys"
chats="$PWD/.net2o/chats"
objects="$PWD/.net2o/objects"
date=\$75
host="test-host"
prio=10
rootdirs="$PWD"
EOF
export NET2O_CONF=$PWD/.net2o/config
PASSPHRASE=1234 $GFORTH ../n2o $debugs -yes keygen chat-test1
PASSPHRASE=2345 $GFORTH ../n2o $debugs -yes keygen chat-test2
PASSPHRASE=3456 $GFORTH ../n2o $debugs -yes keygen chat-test3
PASSPHRASE=4567 $GFORTH ../n2o $debugs -yes keygen chat-test4
PASSPHRASE=4321 $GFORTH ../n2o $debugs -yes keygen chat-test5
PASSPHRASE=5432 $GFORTH ../n2o $debugs -yes keygen chat-test6
PASSPHRASE=6543 $GFORTH ../n2o $debugs -yes keygen chat-test7
PASSPHRASE=7654 $GFORTH ../n2o $debugs -yes keygen chat-test8

PASSPHRASE=1234 $GFORTH ../n2o $debugs keyin ~/chat-test[2-8].n2o
PASSPHRASE=2345 $GFORTH ../n2o $debugs keyin ~/chat-test[13-8].n2o
PASSPHRASE=3456 $GFORTH ../n2o $debugs keyin ~/chat-test[124-8].n2o
PASSPHRASE=4567 $GFORTH ../n2o $debugs keyin ~/chat-test[1-35-8].n2o
PASSPHRASE=4321 $GFORTH ../n2o $debugs keyin ~/chat-test[1-46-8].n2o
PASSPHRASE=5432 $GFORTH ../n2o $debugs keyin ~/chat-test[1-578].n2o
PASSPHRASE=6543 $GFORTH ../n2o $debugs keyin ~/chat-test[1-68].n2o
PASSPHRASE=7654 $GFORTH ../n2o $debugs keyin ~/chat-test[1-7].n2o

function killgforth {
    killall $GFORTH
    usleep 100000
    killall -9 $GFORTH
}
trap killgforth EXIT

export PASSPHRASE=1234
exec 3> >($GFORTH ../n2o $debugs $* chat test >msg-test.out.1)
export GFORTHPID1=$[$!+1]
sleep 5
export PASSPHRASE=2345
exec 4> >($GFORTH ../n2o $debugs 10% $* chat test@chat-test1 >msg-test.out.2)
export GFORTHPID2=$[$!+1]
sleep 2

x=""

for i in $(seq 1 $count)
do
    echo "Phase 1: Test Message $i on 1/2 $x" >&3
    usleep 20000
    echo "Phase 1: Test Message $i on 2/2 $x" >&4
    usleep 20000
    x="x$x"
    case $i in
	*0)
	    sleep 2
	    ;;
    esac
done

exec 5> >(PASSPHRASE=3456 $GFORTH ../n2o $debugs 5% $* chat test@chat-test1 >msg-test.out.3)
export GFORTHPID3=$[$!+1]
exec 6> >(PASSPHRASE=4567 $GFORTH ../n2o $debugs 15% $* chat test@chat-test2 >msg-test.out.4)
export GFORTHPID4=$[$!+1]
exec 7> >(PASSPHRASE=4321 $GFORTH ../n2o $debugs 5% $* chat test@chat-test1 >msg-test.out.5)
export GFORTHPID5=$[$!+1]
exec 8> >(PASSPHRASE=5432 $GFORTH ../n2o $debugs 15% $* chat test@chat-test2 >msg-test.out.6)
export GFORTHPID6=$[$!+1]
exec 9> >(PASSPHRASE=6543 $GFORTH ../n2o $debugs 5% $* chat test@chat-test1 >msg-test.out.7)
export GFORTHPID7=$[$!+1]
exec 10> >(PASSPHRASE=7654 $GFORTH ../n2o $debugs 15% $* chat test@chat-test2 >msg-test.out.8)
export GFORTHPID8=$[$!+1]
sleep 2

#echo "/sync" >&5

#sleep 1

for j in $(seq 1 4)
do
    x=""
    
    for i in $(seq 1 $count)
    do
	echo "Phase 2: Test Message $i/$j on 1/4 $x" >&3
	usleep 20000
	echo "Phase 2: Test Message $i/$j on 2/4 $x" >&4
	usleep 20000
	echo "Phase 2: Test Message $i/$j on 3/4 $x" >&5
	usleep 20000
	echo "Phase 2: Test Message $i/$j on 4/4 $x" >&6
	usleep 20000
	x="x$x"
	case $i in
	    *[05])
		sleep 2
		;;
	esac
    done
    
    echo "/bye" >&6
    sleep 3
    
    x=""

    for i in $(seq 1 $count)
    do
	echo "Phase 3: Test Message $i/$j on 1/3 $x" >&3
	usleep 20000
	echo "Phase 3: Test Message $i/$j on 2/3 $x" >&4
	usleep 20000
	echo "Phase 3: Test Message $i/$j on 3/3 $x" >&5
	usleep 20000
	x="y$x"
	case $i in
	    *[05])
		sleep 2
		;;
	esac
    done

    exec 6> >($GFORTH ../n2o $debugs 15% $* chat test@chat-test2 >>msg-test.out.4)
    export GFORTHPID5=$[$!+1]
    sleep 5
done

echo "Last Message $i on 1/8 $x" >&3
usleep 20000
echo "Last Message $i on 2/8 $x" >&4
usleep 20000
echo "Last Message $i on 3/8 $x" >&5
usleep 20000
echo "Last Message $i on 4/8 $x" >&6
usleep 20000
echo "Last Message $i on 5/8 $x" >&7
usleep 20000
echo "Last Message $i on 6/8 $x" >&8
usleep 20000
echo "Last Message $i on 7/8 $x" >&9
usleep 20000
echo "Last Message $i on 8/8 $x" >&10
usleep 20000

echo "/sync" >&6
echo "/chats" >&10
sleep 3
echo "/bye" >&10
echo "/chats" >&9
sleep 3
echo "/bye" >&9
echo "/chats" >&8
sleep 3
echo "/bye" >&8
echo "/chats" >&7
sleep 3
echo "/bye" >&7
echo "/chats" >&5
sleep 3
echo "/bye" >&5
echo "/chats" >&4
sleep 3
echo "/bye" >&4
echo "/chats" >&3
sleep 3
echo "/bye" >&3
echo "/chats" >&6
sleep 3
echo "/bye" >&6
sleep 5
