#!/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
}

# checks if tmp-sync/out.txt does not contain a specific text
# arg1: pattern
# arg2: errormsg
ncheck_tmpout()
{
	TEST=`cat tmp-sync/out.txt | grep "$1"`
	if [ $? -eq 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-test5a-db.xml
rm -f .dav/dav-sync-tests-test5b-db.xml

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

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

# tmp sync dir
rm -Rf tmp-sync
mkdir tmp-sync
mkdir tmp-sync/test5a
mkdir tmp-sync/test5b

# ----------------------------------------------------------------------------
# test 1: add some files (preparation, make sure everything works)
# expected result: everything synced

mkdir tmp-sync/test5a/dir1
mkdir tmp-sync/test5a/dir1/sub1

cp synctest/file1 tmp-sync/test5a
cp synctest/file2 tmp-sync/test5a/dir1
cp synctest/file3 tmp-sync/test5a/dir1/sub1

dav_sync_push test5a "test 1: push failed"
check_tmpout "3 files 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 test5b "test 1: pull failed"
check_tmpout "3 files 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 2: modify file, sync
# expected result: old resource moved to history collection

echo "test2-mod1" >> tmp-sync/test5a/file1
touch -t 01011200 tmp-sync/test5a/file1

dav_sync_push test5a "test 2: push failed"
check_tmpout "1 file 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 test5b "test 2: pull failed"
check_tmpout "1 file 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)"

dav list -R dav-test-repo/sync/test5/.dav-version-history/ > tmp-sync/out.txt 2> /dev/null
if [ $? -ne 0 ]; then
	echo "test 2: dav list failed"
	exit 2
fi

TEST=`cat tmp-sync/out.txt | wc -l 2> /dev/null`
if [ -z "$TEST" ]; then
	echo "test 2: wc failed"
	exit 2
fi

# line count is 1 (sub-history-collection) + 1 file version = 2
if [ $TEST -ne 2 ]; then
	echo "test 2: wrong line count"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 3: modify file again, sync
# expected result: old resource moved to history collection, 2 old versions

echo "test3-mod1" >> tmp-sync/test5a/file1
touch -t 02011200 tmp-sync/test5a/file1

dav_sync_push test5a "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 test5b "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)"

dav list -R dav-test-repo/sync/test5/.dav-version-history/ > tmp-sync/out.txt 2> /dev/null
TEST=`cat tmp-sync/out.txt | wc -l 2> /dev/null`
if [ $TEST -ne 3 ]; then
	echo "test 3: wrong line count"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 4: modify file2, add new file, sync
# expected result: file2 versionized, new file just uploaded

echo "test4-new1" > tmp-sync/test5a/new1
echo "test4-mod1" >> tmp-sync/test5a/dir1/file2

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

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

dav list -R dav-test-repo/sync/test5/.dav-version-history/ > tmp-sync/out.txt 2> /dev/null
TEST=`cat tmp-sync/out.txt | wc -l 2> /dev/null`

# line count is 3 (prev) + 1 new sub-history-collection + 1 file version = 5
if [ $TEST -ne 5 ]; then
	echo "test 4: wrong line count"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 5: restore previous file2 version
# expected result: file restored

VERSION=`$DAV_SYNC_BIN list-versions tmp-sync/test5a/dir1/file2 | grep "name: " 2> /dev/null`
if [ $? -ne 0 ]; then
	echo "test 5: list-versions failed"
	exit 2
fi

# extract version name
VERSION=${VERSION:5}

$DAV_SYNC_BIN restore -V $VERSION tmp-sync/test5a/dir1/file2 > tmp-sync/out.txt 2> /dev/null
if [ $? -ne 0 ]; then
	echo "test 5: restore failed"
	exit 2
fi
check_tmpout "1 file pulled" "test 5: wrong pull counter"

# compare restored file with base file
diff synctest/file2 tmp-sync/test5a/dir1/file2 > /dev/null 2>&1

if [ $? -ne 0 ]; then
	echo "test 5: wrong file2 content"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 6: push test5a
# expected result: file2 pushed

dav_sync_push test5a "test 6: push failed"
check_tmpout "1 file pushed" "test 6: wrong push counter"
check_tmpout "file2" "test 6: file2 not pushed"
check_tmpout "0 conflicts" "test 6: wrong conflict counter (push)"
check_tmpout "0 errors" "test 6: wrong error counter (push)"

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


# ----------------------------------------------------------------------------
# test 7: modify file2 again and push (this is just a prep for test8)
# expected result: file2 pushed

sleep 2

echo "test7-mod1" >> tmp-sync/test5a/dir1/file2

dav_sync_push test5a "test 7: push failed"
check_tmpout "1 file 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)"

dav_sync_pull test5b "test 7: pull failed"
check_tmpout "1 file 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)"


# ----------------------------------------------------------------------------
# test 8: restore previous file2 version and pull
# expected result: file restored, no files pulled

$DAV_SYNC_BIN restore -V $VERSION tmp-sync/test5a/dir1/file2 > tmp-sync/out.txt 2> /dev/null
if [ $? -ne 0 ]; then
	echo "test 8: restore failed"
	exit 2
fi
check_tmpout "1 file pulled" "test 8: wrong pull counter (restore)"

# compare restored file with base file
diff synctest/file2 tmp-sync/test5a/dir1/file2 > /dev/null 2>&1

if [ $? -ne 0 ]; then
	echo "test 8: wrong file2 content"
	exit 2
fi

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

