#!/bin/bash
GFORTH=${GFORTH:-gforth}
rm -rf dvcs-test.d dvcs-test.2
mkdir -p dvcs-test.d/.net2o dvcs-test.2/.net2o
cd dvcs-test.d
cat >.net2o/config <<EOF
.net2o="$PWD/.net2o"
keys="$PWD/.net2o/keys"
chats="$PWD/.net2o/chats"
objects="$PWD/.net2o/objects"
date=2
host="test-host"
prio=10
rootdirs="$PWD"
EOF
export PASSPHRASE=1234
export NET2O_CONF=$PWD/.net2o/config
$GFORTH ../n2o $* -yes keygen test
$GFORTH ../n2o $* script tests/dvcs-test.s2o
echo
cd ../dvcs-test.2
cat >.net2o/config <<EOF
.net2o="$PWD/.net2o"
keys="$PWD/.net2o/keys"
chats="$PWD/.net2o/chats"
objects="$PWD/.net2o/objects"
date=2
host="test-host2"
prio=10
rootdirs="$PWD"
EOF
export NET2O_CONF=$PWD/.net2o/config
$GFORTH ../n2o $* -yes keygen test2
cd ../dvcs-test.d
export NET2O_CONF=$PWD/.net2o/config
$GFORTH ../n2o $* cmd <<EOF
keylist
keyin ~/test2.n2o
keylist
bye
EOF
exec 3> >($GFORTH ../n2o $* chat dvcs-test)
export GFORTHPID=$[$!+1]
function killgforth {
    kill $GFORTHPID
}
trap killgforth EXIT

sleep 1 #wait for gforth to start the chat
cd ../dvcs-test.2
export NET2O_CONF=$PWD/.net2o/config
$GFORTH ../n2o $* cmd <<EOF
keyin ~/test.n2o
keylist
init dvcs-test
pull dvcs-test@test
log
up
bye
EOF
echo "/bye" >&3

cd ../dvcs-test.d
export NET2O_CONF=$PWD/.net2o/config
$GFORTH ../n2o $* script tests/dvcs-test2.s2o

exec 3> >($GFORTH ../n2o $* chat dvcs-test)
export GFORTHPID=$[$!+1]
sleep 1 #wait for gforth to start the chat
cd ../dvcs-test.2
export NET2O_CONF=$PWD/.net2o/config
$GFORTH ../n2o $* cmd <<EOF
pull dvcs-test@test
log
up
bye
EOF
echo "/chats" >&3
echo "/bye" >&3
exec 3>&-
sleep 5
