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

#
# Test dav-sync pull conflict detection
# and related commands
#

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

# 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-test1a-db.xml
rm -f .dav/dav-sync-tests-test1b-db.xml

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

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

# tmp sync dir
rm -Rf tmp-sync
mkdir tmp-sync
mkdir tmp-sync/test1a
mkdir tmp-sync/test1b

# prepare
cp synctest/file1 tmp-sync/test1a
cp synctest/file2 tmp-sync/test1a

mkdir tmp-sync/test1a/dir1/
mkdir tmp-sync/test1a/dir1/subdir1/

cp synctest/file1 tmp-sync/test1a/dir1/
cp synctest/file2 tmp-sync/test1a/dir1/
cp synctest/file3 tmp-sync/test1a/dir1/
cp synctest/file4 tmp-sync/test1a/dir1/

cp synctest/file1 tmp-sync/test1a/dir1/subdir1/
cp synctest/file2 tmp-sync/test1a/dir1/subdir1/
cp synctest/file3 tmp-sync/test1a/dir1/subdir1/
cp synctest/file4 tmp-sync/test1a/dir1/subdir1/
cp synctest/empty1 tmp-sync/test1a/dir1/subdir1/
cp synctest/empty2 tmp-sync/test1a/dir1/subdir1/

dav_sync_push test1a "prepare: push failed"
dav_sync_pull test1b "prepare: pull failed"
sleep 3

# ----------------------------------------------------------------------------
# 1. test: try to push a file, that has changed on the server
# expected result: 1 conflict

echo "test1-mod" >> tmp-sync/test1a/file1

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

echo "test1-conflict" >> tmp-sync/test1b/file1

dav_sync_push test1b "test 1: push test1b failed"
check_tmpout "0 files pushed" "test 1: wrong push counter (2)"
check_tmpout "1 conflict" "test 1: wrong conflict counter(2)"
check_tmpout "0 errors" "test 1: wrong erro counter(2)"


# ----------------------------------------------------------------------------
# 2. test: try again
# expected result: 1 conflict

dav_sync_push test1b "test 2: push test1b failed"
check_tmpout "0 files pushed" "test 2: wrong push counter"
check_tmpout "1 conflict" "test 2: wrong conflict counter"
check_tmpout "0 errors" "test 2: wrong erro counter"


# ----------------------------------------------------------------------------
# 3. test: try again with -c
# expected result: 1 file pushed, 0 conflicts

$DAV_SYNC_BIN push -c test1b > tmp-sync/out.txt
if [ $? -ne 0 ];
then
	echo "test 1: push test1b failed"
	exit 2
fi

check_tmpout "1 file pushed" "test 3: wrong push counter"
check_tmpout "0 conflicts" "test 3: wrong conflict counter"
check_tmpout "0 errors" "test 3: wrong erro counter"


# ----------------------------------------------------------------------------
# 4. test: get changes in test1a and retry test 1
# expected result: 1 conflict

sleep 3

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

echo "test4-mod" >> tmp-sync/test1a/file1

dav_sync_push test1a "test 4: push test1a failed"
check_tmpout "1 file pushed" "test 4: wrong push counter (1)"
check_tmpout "0 conflicts" "test 4: wrong conflict counter(1)"
check_tmpout "0 errors" "test 4: wrong erro counter(1)"

echo "test4-conflict" >> tmp-sync/test1b/file1

dav_sync_push test1b "test 4: push test1b failed"
check_tmpout "0 files pushed" "test 4: wrong push counter (2)"
check_tmpout "1 conflict" "test 4: wrong conflict counter(2)"
check_tmpout "0 errors" "test 4: wrong erro counter(2)"


# ----------------------------------------------------------------------------
# 5. test: check if resolve-conflicts can fix push conflicts
# expected result: resolve-conflicts only resolves pull conflicts

$DAV_SYNC_BIN resolve-conflicts test1b > /dev/null 2>&1

dav_sync_push test1b "test 5: push test1b failed"
check_tmpout "0 files pushed" "test 5: wrong push counter"
check_tmpout "1 conflict" "test 5: wrong conflict counter"
check_tmpout "0 errors" "test 5: wrong erro counter"


# ----------------------------------------------------------------------------
# 6. test: pull (with conflict), push
# expected result: local conflict file, 0 files pushed

sleep 3

dav_sync_pull test1b "test 6: pull failed"
check_tmpout "1 conflict" "test 6: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 6: wrong error counter (pull)"

# check if file1 is a conflict
$DAV_SYNC_BIN list-conflicts test1b > tmp-sync/out.txt
check_tmpout "/file1" "test 6: list-conflicts doesn't list file1 (1)"

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

# check if file1 is a conflict (again)
$DAV_SYNC_BIN list-conflicts test1b > tmp-sync/out.txt
check_tmpout "/file1" "test 6: list-conflicts doesn't list file1 (2)"

# ----------------------------------------------------------------------------
# 7. test: push after resolve-conflicts
# expected result: removes conflict from orig-file -> 1 file pushed

$DAV_SYNC_BIN resolve-conflicts test1b > /dev/null

TEST=`$DAV_SYNC_BIN list-conflicts test1b | wc -l`
if [ $TEST != "0" ]; then
	echo "test 7: list-conflicts not empty"
	exit 2
fi

dav_sync_push test1b "test 7: push failed"
check_tmpout "1 file pushed" "test 7: wrong push counter"
check_tmpout "0 conflicts" "test 7: wrong conflict counter"
check_tmpout "0 errors" "test 7: wrong error counter"


# ----------------------------------------------------------------------------
# 8. test: pull test1a (actually not a push test, but we need to pull test1a)
# expected result: no conflicts

dav_sync_pull test1a "test 8: pull failed"
check_tmpout "0 conflicts" "test 8: wrong conflict counter"
check_tmpout "0 errors" "test 8: wrong error counter"
TEST=`cat tmp-sync/out.txt | wc -l`
if [ $TEST == "1" ]; then
	echo "test 8: no files pulled"
	exit 2
fi


# ----------------------------------------------------------------------------
# 9. test: multiple files modified locally and on the server, push
# expected result: multiple conflicts

# modify 6 files in test1a
echo "test9conflict_a" >> tmp-sync/test1a/file1
echo "test9conflict_a" >> tmp-sync/test1a/file2

echo "test9conflict_a" >> tmp-sync/test1a/dir1/file1
echo "test9conflict_a" >> tmp-sync/test1a/dir1/file2

echo "test9conflict_a" >> tmp-sync/test1a/dir1/subdir1/empty1
echo "test9conflict_a" >> tmp-sync/test1a/dir1/subdir1/empty2

dav_sync_push test1a "test 9: push test1a failed"
check_tmpout "6 files pushed" "test 9: wrong test1a push counter"
check_tmpout "0 conflicts" "test 9: wrong test1a conflict counter"
check_tmpout "0 errors" "test 9: wrong test1a error counter"

# modify 3 files in test1b, which are also modified in test1a
echo "test9conflict_b" >> tmp-sync/test1b/file2
echo "test9conflict_b" >> tmp-sync/test1b/dir1/file2
echo "test9conflict_b" >> tmp-sync/test1b/dir1/subdir1/empty2

dav_sync_push test1b "test 9: push test1a failed"
check_tmpout "3 files pushed" "test 9: wrong test1b push counter"
check_tmpout "3 conflicts" "test 9: wrong test1b conflict counter"
check_tmpout "0 errors" "test 9: wrong test1b error counter"



