#!/bin/sh

pid=0
testnum=0
exitval=0

cleanup()
{
    [ -f test.out ] && rm -f test.out
    [ -f c.cf ] && rm -f c.cf
    [ $pid -eq 0 ] && return 0
    kill $pid
    for i in *.log; do
	[ "$i" != "conserver.log" ] && [ -f "$i" ] && rm -f "$i";
    done
    [ "$exitval" = 0 ] && rm -f conserver.log
    [ -d 127.0.0.1 ] && sleep 1 && rm -rf 127.0.0.1
    exit $exitval
}

dotest()
{
    testnum=`expr $testnum + 1`
    $ECHO "executing test #$testnum...$EE"
    if [ "$2" ]; then
	eval "$2" > test.out 2>&1
    else
	echo "$1" | \
	    ../console/console -n -C /dev/null -M 127.0.0.1 -p 7777 shell > test.out 2>&1
    fi
    if [ "$record" ]; then
	echo "recorded"
	mv test.out results/test$testnum
    else
	if [ -f results/test$testnum ]; then
	    if diff -i results/test$testnum >test$testnum.diff test.out 2>&1; then
		echo "succeeded"
		rm -f test$testnum.diff
	    else
		echo "failed (diffs in test$testnum.diff)"
		exitval=1
	    fi
	else
	    echo "unknown (not recorded)"
	fi
	rm -f test.out
    fi
}

[ ! -f ../conserver/conserver -o ! -f ../console/console ] && \
    echo 'binaries do not exist - did you run make yet?' && exit 1

trap cleanup 15

if [ "`echo -n`" = "-n" ]; then
    ECHO="echo"
    EE="\c"
else
    ECHO="echo -n"
    EE=""
fi

$ECHO "starting conserver...$EE"
rm -f c.cf
cp test1.cf c.cf
../conserver/conserver -M 127.0.0.1 -p 7777 -v -C c.cf \
    -P test.passwd -m 32 > conserver.log 2>&1 &

pid=$!
echo "pid $pid"

sleep 3

[ ! -d results ] && mkdir results

dotest EVAL "../console/console -n -C /dev/null -M 127.0.0.1 -p 7777 -u | sed -e 's/[0-9][0-9]*//g' -e 's/[ 	][ 	]*/ /g'"
dotest 'c?c.'
dotest 'cl?c.'
dotest 'cdc.'
dotest 'coc.'

echo "moving in second config file"
rm -f c.cf
cp test2.cf c.cf
kill -1 $pid
sleep 3

dotest EVAL "../console/console -n -C /dev/null -M 127.0.0.1 -p 7777 -u | sed -e 's/[0-9][0-9]*//g' -e 's/[ 	][ 	]*/ /g'"
dotest 'c?c.'
dotest 'cl?c.'
dotest 'cdc.'
dotest 'cocacoc.'

dotest EVAL "echo 'tu.' | ../console/console -n -C /dev/null -M 127.0.0.1 -p 7777 -e 'tu' shell"
dotest EVAL "../console/console -n -C /dev/null -M 127.0.0.1 -p 7777 -R | sed -e 's/ [^ ]*$//'"
dotest EVAL "../console/console -n -C /dev/null -M 127.0.0.1 -p 7777 -x | sed -e 's/ on [^ ]* */ on /'"
dotest EVAL "../console/console -n -C /dev/null -M 127.0.0.1 -p 7777 -x sh | sed -e '1s/^[^:]*//'"
dotest EVAL "../console/console -n -C /dev/null -M 127.0.0.1 -p 7777 -x shell | sed -e 's/ on [^ ]* */ on /'"

cleanup
