#! /bin/sh
# Simple regression test for track.  At least makes sure it works for
# typical values of work.  This doesn't test the simple case of tracking
# without changing names, which is annoying - need to find a good way
# to test that, but it may require two machines.
# Mark Moraes, University of Toronto.
case "$1" in
-v)	verb=$1; shift; set -x;;
esac
testdir=${1-/tmp/tracktest}
if test ! -d $testdir; then
	echo $0: cannot find $testdir, quitting >&2
	exit 1
fi
PATH=$testdir/clientdir/track/bin:$PATH; export PATH
host=`hostname`
dotest() {
	track -d $testdir/server/track $verb -w $i
	track -d $testdir/clientdir/track -r $testdir/server/track $trackopts -t -l $host $verb $i
}

# ordinary track, presub to something longer

i=test1; trackopts=; dotest
if diff -r $testdir/clientdir/tests $testdir/server/tests; then
	echo test1 ok
else
	echo clientdir/tests and server/tests differ, track on test1 failed
	exit 1
fi

# track -D, presub to something shorter

i=test2; trackopts=-D; dotest
if diff -r $testdir/c/tests $testdir/server/tests; then
	echo test2 ok
else
	echo c/tests and server/tests differ, track on test2 failed
	exit 2
fi

# change something in server, see if it tracks over

echo modified `date` >> $testdir/server/tests/a
echo modified `date` >> $testdir/server/tests/c/cc
i=test1; trackopts=
dotest
if diff -r $testdir/clientdir/tests $testdir/server/tests; then
	echo test1 modified ok
else
	echo clientdir/tests and server/tests differ, track on test1 modified failed
	exit 3
fi

# change something in client, see that it gets left alone

cp $testdir/clientdir/tests/a $testdir/tmp/a.old
echo client >> $testdir/clientdir/tests/a
cp $testdir/clientdir/tests/a $testdir/tmp/a
trackopts=
echo This will complain that $testdir/clientdir/tests/a is newer
dotest
if diff $testdir/clientdir/tests/a $testdir/tmp/a; then
	mv $testdir/tmp/a.old $testdir/clientdir/tests/a
	if diff -r $testdir/clientdir/tests $testdir/server/tests; then
		echo test1 client ok
	else
		echo test1 client messed up, $testdir/clientdir/tests/a ok but something else changed
		exit 4
	fi
else
	echo test 1 client messed up, $testdir/clientdir/tests/a changed
	exit 5
fi
rm $testdir/tmp/a

# change something in client, track -u should fix it up

echo client >> $testdir/clientdir/tests/a
trackopts=-u
dotest
if diff -r $testdir/clientdir/tests $testdir/server/tests; then
	echo test1 -u ok
else
	echo clientdir/tests and server/tests differ, track -u test1 failed
	exit 6
fi

# check that -O updates files

echo -O >> $testdir/server/tests/a
trackopts=-O
dotest
if diff -r $testdir/clientdir/tests $testdir/server/tests; then
	echo test1 -O ok
else
	echo clientdir/tests and server/tests differ, track -O test1 failed
	exit 7
fi

# check that -O doesn't bother with removed files on client

rm $testdir/clientdir/tests/d
trackopts=-O
dotest
if test ! -f $testdir/clientdir/tests/d; then
	echo test1 -O.rm ok
else
	echo $testdir/clientdir/tests/d came back, track -O.rm test1 failed
	exit 8
fi

# check that files don't clobber symlinks  (we really should check all
# cases, buts thats a lot of work and I don't feel like that much
# typing right now.  Volunteers!)

rm $testdir/server/tests/e
echo tests/e > $testdir/server/tests/e
trackopts=
echo This should complain about $testdir/clientdir/tests/e not being a regular file
# and should restore $testdir/clientdir/tests/d
dotest

# Now check that -f works
trackopts=-f
dotest
if diff -r $testdir/clientdir/tests $testdir/server/tests; then
	echo test1 -f ok
else
	echo clientdir/tests and server/tests differ, track -f test1 failed
	exit 9
fi

# no tests after this line, please.  If any test fails, we shouldn't
# reach here.
echo all tests passed
