#!/bin/sh
#
# version to test setOwn=1 behavior
#
#regression analysis:
#
#1) create directory, file, symlink, hardlink
#   test client, server: -c behavior
#   
#2) modify client and server sides. Test -n behavior.
#
#3) test rsh, ssh transport mechanisms.
#
#4) test message reporting.
#
# test hardlink mismatches
#

# allow rsh access
FUSER=root
FHOME=/root
echo "localhost $FUSER" cat - $FHOME/.rhosts > $FHOME/.rhosts

umask 22
testdir=/tmp/fsync-test
locdir=$testdir/local/
remdir=$testdir/remote/
fsyncrc=$testdir/fsyncrc
fsyncprog="perl -w `pwd`/fsync"

mkdir $testdir
echo > $fsyncrc 
echo "\$verbose = 0;                     " >> $fsyncrc
echo "\$remhost = \"localhost\";	 " >> $fsyncrc
echo "\$rsh = \"rsh -n REMHOST exec\";	 " >> $fsyncrc
echo "\$port = 9006;			 " >> $fsyncrc
echo "\$Slogfile = \"$testdir/fsyncd.log\";	 " >> $fsyncrc
echo "\$progname = \"$fsyncprog\";       " >> $fsyncrc
echo "\$backupDir     = \"loc-save\";    " >> $fsyncrc
echo "\$rBackupDir    = \"rem-save\";    " >> $fsyncrc
echo "\$hashSaveFile  = \"$testdir/loc-hash\";    " >> $fsyncrc
echo "\$rHashSaveFile = \"$testdir/rem-hash\";    " >> $fsyncrc
echo "\$rootdir       = \"$locdir\";     " >> $fsyncrc
echo "\$rem_rootdir   = \"$remdir\";     " >> $fsyncrc

echo -n "testing syncing local to remote..."
# create file, directory, link, symlink 
mkdir $locdir
mkdir $remdir
cd $locdir
echo "abcde" > file
chmod 511 file
chown mail.mail file
ln file link
ln file link2
ln -s file symlink
mkdir dir
chown mail.mail dir
echo "fghik" > dir/file

$fsyncprog -own -c -f $fsyncrc . < /dev/null > /dev/null

ok=1
diff $locdir/file $remdir/file || (echo "error in syncing file"; ok=0)
diff $locdir/dir/file $remdir/dir/file || \
	(echo "error in syncing dir/file"; ok=0)
diff $locdir/link $remdir/link || (echo "error in syncing link"; ok=0)
diff $locdir/link2 $remdir/link2 || (echo "error in syncing link2"; ok=0)
locsym=`ls -l symlink|cut -d \> -f 2`
remsym=`ls -l symlink|cut -d \> -f 2`
if [ "$locsym" != "$remsym" ] ; then
	echo "error in syncing symlink"
	ok=0
fi
locmod=`ls -l $locdir/file|cut -c -10`
remmod=`ls -l $remdir/file|cut -c -10`
if [ "$locmod" != "$remmod" ] ; then
	echo "error in setting mode"
	ok=0
fi
locown=`ls -l $locdir/file|cut -c 16-23`
remown=`ls -l $remdir/file|cut -c 16-23`
if [ "$locown" != "$remown" ] ; then
	echo "error in setting file uid: $locown $remown"
	ok=0
fi
locgrp=`ls -l $locdir/file|cut -c 25-32`
remgrp=`ls -l $remdir/file|cut -c 25-32`
if [ "$locgrp" != "$remgrp" ] ; then
	echo "error in setting dir gid $locgrp $remgrp"
	ok=0
fi
[ $ok -eq 1 ] && echo ok.
[ $ok -eq 0 ] && echo FAILED.

rm -rf $locdir $remdir

echo -n "testing syncing remote to local..."
# create file, directory, link, symlink 

mkdir $locdir
mkdir $remdir
cd $remdir
echo "abcde" > file
chmod 511 file
ln file link
ln -s file symlink
mkdir dir
echo "fghik" > dir/file
chown mail.mail file
chown mail.mail dir

cd $locdir
$fsyncprog -own -c -f $fsyncrc . </dev/null > /dev/null

ok=1
diff $locdir/file $remdir/file || (echo "error in syncing file"; ok=0)
diff $locdir/dir/file $remdir/dir/file || \
	(echo "error in syncing dir/file"; ok=0)
diff $locdir/link $remdir/link || (echo "error in syncing link"; ok=0)
locsym=`ls -l symlink|cut -d \> -f 2`
remsym=`ls -l symlink|cut -d \> -f 2`
if [ "$locsym" != "$remsym" ] ; then
	echo "error in syncing symlink"
	ok=0
fi
locmod=`ls -l $locdir/file|cut -c -10`
remmod=`ls -l $remdir/file|cut -c -10`
if [ "$locmod" != "$remmod" ] ; then
	echo "error in setting mode"
	ok=0
fi
locown=`ls -l $locdir/file|cut -c 16-23`
remown=`ls -l $remdir/file|cut -c 16-23`
if [ "$locown" != "$remown" ] ; then
	echo "error in setting file uid: $locown $remown"
	ok=0
fi
locgrp=`ls -l $locdir/file|cut -c 25-32`
remgrp=`ls -l $remdir/file|cut -c 25-32`
if [ "$locgrp" != "$remgrp" ] ; then
	echo "error in setting dir gid $locgrp $remgrp"
	ok=0
fi
[ $ok -eq 1 ] && echo ok.
[ $ok -eq 0 ] && echo FAILED.

echo -n "testing -n behavior..."
cd $locdir
rm -f file link $remdir/link
echo "asdpoianwred" > file
chmod 522 file
chown mail.mail file

$fsyncprog -own -n -S -f $fsyncrc . > /dev/null

ok=1
diff $locdir/file $remdir/file || (echo "error in syncing file"; ok=0)
diff $locdir/dir/file $remdir/dir/file || \
	(echo "error in syncing dir/file"; ok=0)
#diff $locdir/link $remdir/link || (echo "error in syncing link"; ok=0)
locsym=`ls -l symlink|cut -d \> -f 2`
remsym=`ls -l symlink|cut -d \> -f 2`
if [ "$locsym" != "$remsym" ] ; then
	echo "error in syncing symlink"
	ok=0
fi
locmod=`ls -l $locdir/file|cut -c -10`
remmod=`ls -l $remdir/file|cut -c -10`
if [ "$locmod" != "$remmod" ] ; then
	echo "error in setting mode"
	ok=0
fi
locown=`ls -l $locdir/file|cut -c 16-23`
remown=`ls -l $remdir/file|cut -c 16-23`
if [ "$locown" != "$remown" ] ; then
	echo "error in setting file uid"
	ok=0
fi
[ $ok -eq 1 ] && echo ok.
[ $ok -eq 0 ] && echo FAILED.

# remove rsh access
tail +2 $FHOME/.rhosts > $FHOME/.rhosts

#rm -rf $testdir
