#!/bin/sh
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#
# Copyright 2019 Olaf Wintermann. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#   1. Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#
#   2. Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in the
#      documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

if [ -z "$DAV_BIN" ];
then
	echo "DAV_BIN variable not set"
	exit 1
fi
if [ -z "$DAV_SYNC_BIN" ];
then
	echo "DAV_BIN variable not set"
	exit 1
fi

XATTR=../../build/xattrtool

# checks if tmp-sync/out.txt contains a specific text
# arg1: pattern
# arg2: errormsg
check_tmpout()
{
	TEST=`cat tmp-sync/out.txt | grep "$1"`
	if [ $? -ne 0 ];
	then
		echo "$2"
		exit 2	
	fi
}

# do dav-sync push and check return value
# arg1: dir
# arg2: errormsg
dav_sync_push()
{
	$DAV_SYNC_BIN push $1 > tmp-sync/out.txt
	if [ $? -ne 0 ];
	then
		echo "$2"
		exit 2
	fi
}
# do dav-sync pull and check return value
# arg1: dir
# arg2: errormsg
dav_sync_pull()
{
	$DAV_SYNC_BIN pull $1 > tmp-sync/out.txt
	if [ $? -ne 0 ];
	then
		echo "$2"
		exit 2
	fi
}

rm -f .dav/dav-sync-tests-test4a-db.xml
rm -f .dav/dav-sync-tests-test4b-db.xml

$DAV_BIN rm dav-test-repo/sync/test4 2> /dev/null

$DAV_BIN mkcol dav-test-repo/sync/test4 2> /dev/null

# tmp sync dir
rm -Rf tmp-sync
mkdir tmp-sync
mkdir tmp-sync/test4a
mkdir tmp-sync/test4b

# ----------------------------------------------------------------------------
# test 1: sync executable file and check if it can be executed
# expected result: exec works

echo "#!/bin/sh"     > tmp-sync/test4a/script.sh
echo "echo itworks" >> tmp-sync/test4a/script.sh

chmod +x tmp-sync/test4a/script.sh

dav_sync_push test4a "test 1: push failed"
check_tmpout "1 file pushed" "test 1: wrong push counter"
check_tmpout "0 conflicts" "test 1: wrong conflict counter (push)"
check_tmpout "0 errors" "test 1: wrong error counter (push)"

dav_sync_pull test4b "test 1: pull failed"
check_tmpout "1 file pulled" "test 1: wrong pull counter"
check_tmpout "0 conflicts" "test 1: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 1: wrong error counter (pull)"

# test if script in test4a works, if not, shell scripts are unsupported
# on this platform
./tmp-sync/test4a/script.sh > /dev/null 2>&1
if [ $? -eq 0 ]; then
	# shell scripts work
	
	# test synced script
	./tmp-sync/test4b/script.sh > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "script not executable"
		exit 2
	fi
	OUT=`./tmp-sync/test4b/script.sh 2> /dev/null`
	if [ $OUT != "itworks" ]; then
		echo "script.sh has wrong output"
		exit 2
	fi
fi

# ----------------------------------------------------------------------------
# test 2: set mtime to specific date and sync the file
# expected result: mtime the same on both sides

mkdir tmp-sync/test4a/dir1

cp synctest/file1 tmp-sync/test4a/
cp synctest/file2 tmp-sync/test4a/dir1/

touch -t 01011200 tmp-sync/test4a/file1
touch -t 02021200 tmp-sync/test4a/dir1/file2

dav_sync_push test4a "test 2: push failed"
check_tmpout "2 files pushed" "test 2: wrong push counter"
check_tmpout "0 conflicts" "test 2: wrong conflict counter (push)"
check_tmpout "0 errors" "test 2: wrong error counter (push)"

dav_sync_pull test4b "test 2: pull failed"
check_tmpout "2 files pulled" "test 2: wrong pull counter"
check_tmpout "0 conflicts" "test 2: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 2: wrong error counter (pull)"

MTIMEA1=`stat -c %Y tmp-sync/test4a/file1`
MTIMEB1=`stat -c %Y tmp-sync/test4b/file1`

MTIMEA2=`stat -c %Y tmp-sync/test4a/dir1/file2`
MTIMEB2=`stat -c %Y tmp-sync/test4b/dir1/file2`

if [ $MTIMEA1 != $MTIMEB1 ]; then
	echo "file1: mtime not synced"
	exit 2
fi
if [ $MTIMEA2 != $MTIMEB2 ]; then
	echo "file2: mtime not synced"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 3: modify file1 and push/pull
# expected result: file content synced, mtime also synced

echo "test3-mod1" >> tmp-sync/test4a/file1

dav_sync_push test4a "test 3: push failed"
check_tmpout "1 file pushed" "test 3: wrong push counter"
check_tmpout "0 conflicts" "test 3: wrong conflict counter (push)"
check_tmpout "0 errors" "test 3: wrong error counter (push)"

dav_sync_pull test4b "test 3: pull failed"
check_tmpout "1 file pulled" "test 3: wrong pull counter"
check_tmpout "0 conflicts" "test 3: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 3: wrong error counter (pull)"

MTIMEA1=`stat -c %Y tmp-sync/test4a/file1`
MTIMEB1=`stat -c %Y tmp-sync/test4b/file1`

if [ $MTIMEA1 != $MTIMEB1 ]; then
	echo "file1: mtime not synced"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 4: add xattr to files, push/pull
# expected result: xattr synced

# test if xattr are supported
../../build/xattrtool set tmp-sync/test4a/file1 attr1 testv  2> /dev/null
ATTR_TEST=`../../build/xattrtool get tmp-sync/test4a/file1 attr1 2> /dev/null`
if [ $ATTR_TEST != "testv" ]; then
	echo "xattr unsupported, skip"
	exit 2
fi

sleep 2

# add xattr to file without content modification
$XATTR set tmp-sync/test4a/file1 attr2 value2
$XATTR set tmp-sync/test4a/file1 attr3 hello
$XATTR set tmp-sync/test4a/file1 attr4 hello
touch tmp-sync/test4a/file1

# modify file and add xattr
echo "test4-mod1" >> tmp-sync/test4a/dir1/file2
$XATTR set tmp-sync/test4a/dir1/file2 msg helloworld

# create new file with extended attributes
echo "newfile" > tmp-sync/test4a/file3
$XATTR set tmp-sync/test4a/file3 msg helloworld

dav_sync_push test4a "test 4: push failed"
check_tmpout "0 conflicts" "test 4: wrong conflict counter (push)"
check_tmpout "0 errors" "test 4: wrong error counter (push)"
check_tmpout "put: /file3" "test 4: file 3 not pushed"
check_tmpout "put: /dir1/file2" "test 4: file 2 not pushed"
check_tmpout "update: /file1" "test 4: file 1 not pushed"

dav_sync_pull test4b "test 4: pull failed"
check_tmpout "0 conflicts" "test 4: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 4: wrong error counter (pull)"
check_tmpout "get: /file3" "test 4: file 3 not pulled"
check_tmpout "get: /dir1/file2" "test 4: file 2 not pulled"
check_tmpout "update: /file1" "test 4: file 1 not pulled"

FILE1_ATTR2=`$XATTR get tmp-sync/test4b/file1 attr2 2> /dev/null`
FILE1_ATTR3=`$XATTR get tmp-sync/test4b/file1 attr3 2> /dev/null`
FILE1_ATTR4=`$XATTR get tmp-sync/test4b/file1 attr4 2> /dev/null`

FILE2_ATTR0=`$XATTR get tmp-sync/test4b/dir1/file2 msg 2> /dev/null`

FILE3_ATTR0=`$XATTR get tmp-sync/test4b/file3 msg 2> /dev/null`


if [ "$FILE1_ATTR2" != "value2" ]; then
	echo "file1 attr2 has wrong value: $FILE1_ATTR2"
	exit 2
fi
if [ "$FILE1_ATTR3" != "hello" ]; then
	echo "file1 attr3 has wrong value: $FILE1_ATTR3"
	exit 2
fi
if [ "$FILE1_ATTR4" != "hello" ]; then
	echo "file1 attr4 has wrong value: $FILE1_ATTR4"
	exit 2
fi

if [ "$FILE2_ATTR0" != "helloworld" ]; then
	echo "file2 attr has wrong value: $FILE2_ATTR0"
	exit 2
fi

if [ "$FILE3_ATTR0" != "helloworld" ]; then
	echo "file3 attr has wrong value: $FILE3_ATTR0"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 5: do nothing, push/pull
# expected result: no update, no files pushed/pulled

sleep 2

dav_sync_push test4a "test 5: push failed"
check_tmpout "0 files pushed" "test 5: wrong push counter"
check_tmpout "0 conflicts" "test 5: wrong conflict counter (push)"
check_tmpout "0 errors" "test 5: wrong error counter (push)"
LN=`cat tmp-sync/out.txt | wc -l`
if [ "$LN" != "1" ]; then
	echo "test 5: wrong output (push)"
	exit 2
fi

dav_sync_pull test4b "test 5: pull failed"
check_tmpout "0 files pulled" "test 5: wrong pull counter"
check_tmpout "0 conflicts" "test 5: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 5: wrong error counter (pull)"
LN=`cat tmp-sync/out.txt | wc -l`
if [ "$LN" != "1" ]; then
	echo "test 5: wrong output (pull)"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 6: add additional xattr to files, push/pull
# expected result: xattr synced

sleep 2

$XATTR set tmp-sync/test4a/file1 test5 f1value5

echo "test6-mod1" >> tmp-sync/test4a/file3
$XATTR set tmp-sync/test4a/file3 test5 f3value5

dav_sync_push test4a "test 6: push failed"
check_tmpout "0 conflicts" "test 6: wrong conflict counter (push)"
check_tmpout "0 errors" "test 6: wrong error counter (push)"
check_tmpout "put: /file3" "test 6: file 3 not pushed"
check_tmpout "update: /file1" "test 6: file 1 not pushed"

dav_sync_pull test4b "test 6: pull failed"
check_tmpout "0 conflicts" "test 6: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 6: wrong error counter (pull)"
check_tmpout "get: /file3" "test 6: file 3 not pulled"
check_tmpout "update: /file1" "test 6: file 1 not pulled"

TEST5=`$XATTR get tmp-sync/test4b/file1 test5 2> /dev/null`
if [ "$TEST5" != "f1value5" ]; then
	echo "test 6: file1 attr has wrong value"
	exit 2
fi
TEST5=`$XATTR get tmp-sync/test4b/file3 test5 2> /dev/null`
if [ "$TEST5" != "f3value5" ]; then
	echo "test 6: file3 attr has wrong value"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 7: do nothing, push/pull
# expected result: no update, no files pushed/pulled

sleep 2

dav_sync_push test4a "test 7: push failed"
check_tmpout "0 files pushed" "test 7: wrong push counter"
check_tmpout "0 conflicts" "test 7: wrong conflict counter (push)"
check_tmpout "0 errors" "test 7: wrong error counter (push)"
LN=`cat tmp-sync/out.txt | wc -l`
if [ "$LN" != "1" ]; then
	echo "test 7: wrong output (push)"
	exit 2
fi

dav_sync_pull test4b "test 7: pull failed"
check_tmpout "0 files pulled" "test 7: wrong pull counter"
check_tmpout "0 conflicts" "test 7: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 7: wrong error counter (pull)"
LN=`cat tmp-sync/out.txt | wc -l`
if [ "$LN" != "1" ]; then
	echo "test 7: wrong output (pull)"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 8: remove xattr
# expected result: xattr removed in test4b

$XATTR remove tmp-sync/test4a/dir1/file2 msg
if [ $? -ne 0 ]; then
	echo "test 8: xattrtool remove failed"
	exit 2
fi

touch tmp-sync/test4a/dir1/file2

dav_sync_push test4a "test 8: push failed"
check_tmpout "0 conflicts" "test 8: wrong conflict counter (push)"
check_tmpout "0 errors" "test 8: wrong error counter (push)"
check_tmpout "update: /dir1/file2" "test 8: file 1 not pushed"

dav_sync_pull test4b "test 8: pull failed"
check_tmpout "0 conflicts" "test 8: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 8: wrong error counter (pull)"
check_tmpout "update: /dir1/file2" "test 8: file 1 not pulled"

TEST6=`$XATTR get tmp-sync/test4b/dir1/file2 msg 2> /dev/null`
if [ "$TEST6" != "" ]; then
	echo "test 8: xattr msg not removed"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 9: do nothing, push/pull
# expected result: no update, no files pushed/pulled

sleep 2

dav_sync_push test4a "test 9: push failed"
check_tmpout "0 files pushed" "test 9: wrong push counter"
check_tmpout "0 conflicts" "test 9: wrong conflict counter (push)"
check_tmpout "0 errors" "test 9: wrong error counter (push)"
LN=`cat tmp-sync/out.txt | wc -l`
if [ "$LN" != "1" ]; then
	echo "test 9: wrong output (push)"
	exit 2
fi

dav_sync_pull test4b "test 9: pull failed"
check_tmpout "0 files pulled" "test 9: wrong pull counter"
check_tmpout "0 conflicts" "test 9: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 9: wrong error counter (pull)"
LN=`cat tmp-sync/out.txt | wc -l`
if [ "$LN" != "1" ]; then
	echo "test 9: wrong output (pull)"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 10: add tags to files and sync
# expected result: tags synced

touch tmp-sync/test4a/file1
echo "test10-mod1" >> tmp-sync/test4a/dir1/file2
echo "test10-new" >> tmp-sync/test4a/file4

$DAV_SYNC_BIN add-tag -s test4a tmp-sync/test4a/file1 test
if [ $? -ne 0 ]; then
	echo "test 10: add-tag failed"
	exit 2
fi
$DAV_SYNC_BIN add-tag -s test4a tmp-sync/test4a/dir1/file2 test
$DAV_SYNC_BIN add-tag -s test4a tmp-sync/test4a/file4 test

dav_sync_push test4a "test 10: push failed"
check_tmpout "0 conflicts" "test 10: wrong conflict counter (push)"
check_tmpout "0 errors" "test 10: wrong error counter (push)"
check_tmpout "put: /file4" "test 10: file 4 not pushed"
check_tmpout "put: /dir1/file2" "test 10: file 2 not pushed"
check_tmpout "update: /file1" "test 10: file 1 not pushed"

dav_sync_pull test4b "test 10: pull failed"
check_tmpout "0 conflicts" "test 10: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 10: wrong error counter (pull)"
check_tmpout "get: /file4" "test 10: file 4 not pulled"
check_tmpout "get: /dir1/file2" "test 10: file 2 not pulled"
check_tmpout "update: /file1" "test 10: file 1 not pulled"

$DAV_SYNC_BIN list-tags -s test4b tmp-sync/test4b/file1 > tmp-sync/out.txt
check_tmpout "test" "test 10: file1 missing tag"
$DAV_SYNC_BIN list-tags -s test4b tmp-sync/test4b/file4 > tmp-sync/out.txt
check_tmpout "test" "test 10: file3 missing tag"
$DAV_SYNC_BIN list-tags -s test4b tmp-sync/test4b/dir1/file2 > tmp-sync/out.txt
check_tmpout "test" "test 10: file2 missing tag"


# ----------------------------------------------------------------------------
# test 11: check if double update happens
# expected result: nothing happens

dav_sync_push test4a "test 11: push failed"
check_tmpout "0 files pushed" "test 11: wrong push counter"
check_tmpout "0 conflicts" "test 11: wrong conflict counter (push)"
check_tmpout "0 errors" "test 11: wrong error counter (push)"
LN=`cat tmp-sync/out.txt | wc -l`
if [ "$LN" != "1" ]; then
	echo "test 11: wrong output (push)"
	exit 2
fi

dav_sync_pull test4b "test 11: pull failed"
check_tmpout "0 files pulled" "test 11: wrong pull counter"
check_tmpout "0 conflicts" "test 11: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 11: wrong error counter (pull)"
LN=`cat tmp-sync/out.txt | wc -l`
if [ "$LN" != "1" ]; then
	echo "test 11: wrong output (pull)"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 12: add tag, push test4a and pull test4a
# expected result: tags pushed, nothing pulled

$DAV_SYNC_BIN add-tag -s test4a tmp-sync/test4a/file1 work
touch -t 03011100 tmp-sync/test4a/file1

dav_sync_push test4a "test 12: push failed"
check_tmpout "0 conflicts" "test 12: wrong conflict counter (push)"
check_tmpout "0 errors" "test 12: wrong error counter (push)"
check_tmpout "update: /file1" "test 12: file 1 not pushed"

dav_sync_pull test4a "test 12: pull failed"
check_tmpout "0 files pulled" "test 12: wrong pull counter"
check_tmpout "0 conflicts" "test 12: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 12: wrong error counter (pull)"
LN=`cat tmp-sync/out.txt | wc -l`
if [ "$LN" != "1" ]; then
	echo "test 12: wrong output (pull)"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 13: pull test4b
# expected result: update file1, sync tags

dav_sync_pull test4b "test 13: pull failed"
check_tmpout "0 conflicts" "test 13: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 13: wrong error counter (pull)"
check_tmpout "update: /file1" "test 13: file 1 not pulled"

$DAV_SYNC_BIN list-tags -s test4b tmp-sync/test4b/file1 > tmp-sync/out.txt
check_tmpout "test" "test 13: file1 missing tag 'test'"
check_tmpout "work" "test 13: file1 missing tag 'work'"


# ----------------------------------------------------------------------------
# test 14: remove tags from file4 and sync
# expected result: tags removed

$DAV_SYNC_BIN remove-tag -s test4a tmp-sync/test4a/file4 test
touch -t 04011100 tmp-sync/test4a/file4

dav_sync_push test4a "test 14: push failed"
check_tmpout "0 conflicts" "test 14: wrong conflict counter (push)"
check_tmpout "0 errors" "test 14: wrong error counter (push)"
check_tmpout "update: /file4" "test 14: file 4 not pushed"

dav_sync_pull test4b "test 14: pull failed"
check_tmpout "0 conflicts" "test 14: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 14: wrong error counter (pull)"
check_tmpout "update: /file4" "test 14: file 4 not pulled"

LN=`$DAV_SYNC_BIN list-tags -s test4b tmp-sync/test4b/file4 | wc -l`
if [ "$LN" != "0" ]; then
	echo "test 14: wrong tag count"
	exit 2
fi

