#!/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: add some files and add tags, sync only files with tag test1
# expected result: only files with tag test1 pushed

mkdir tmp-sync/test4a/dir1

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

cp synctest/file3 tmp-sync/test4a/dir1/
cp synctest/file4 tmp-sync/test4a/dir1/

$DAV_SYNC_BIN add-tag -s test4a tmp-sync/test4a/file1 mytag
$DAV_SYNC_BIN add-tag -s test4a tmp-sync/test4a/file1 test1

$DAV_SYNC_BIN add-tag -s test4a tmp-sync/test4a/file2 mytag
$DAV_SYNC_BIN add-tag -s test4a tmp-sync/test4a/file2 test1

$DAV_SYNC_BIN add-tag -s test4a tmp-sync/test4a/dir1/file3 sub

$DAV_SYNC_BIN push test4a -t test1 > tmp-sync/out.txt
if [ $? -ne 0 ];
then
	echo "test 1: push failed"
	exit 2
fi
check_tmpout "2 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 test4b "test 1: pull failed"
check_tmpout "2 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)"

cat tmp-sync/test4b/dir1/file3 > /dev/null 2>&1
if [ $? -eq 0 ]; then
	echo "test 1: file 3 must not be synced"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 2: push again without tag filter
# expected result: remaining files pushed

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)"


# ----------------------------------------------------------------------------
# test 3: add another tag to 2 files and pull with tag filter
# expected result: 2 files pulled

$DAV_SYNC_BIN add-tag -s test4a tmp-sync/test4a/file1 test3
$DAV_SYNC_BIN add-tag -s test4a tmp-sync/test4a/dir1/file3 test3

dav_sync_push test4a "test 3: push failed"
check_tmpout "0 conflicts" "test 3: wrong conflict counter (push)"
check_tmpout "0 errors" "test 2: wrong error counter (push)"
check_tmpout "update: /file1" "test 3: file1 not updated (push)"
check_tmpout "update: /dir1/file3" "test 3: file3 not updated (push)"

$DAV_SYNC_BIN pull test4b -t test3 > tmp-sync/out.txt
if [ $? -ne 0 ];
then
	echo "test 3: pull failed"
	exit 2
fi
check_tmpout "2 files 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)"

TAGS=`$DAV_SYNC_BIN list-tags -s test4b tmp-sync/test4b/file1 > tmp-sync/out.txt 2> /dev/null`
if [ $? -ne 0 ]; then
	echo "test 3: file1 not pulled"
	exit 2
fi
check_tmpout "test3" "test 3: file1: missing tag test3"
check_tmpout "mytag" "test 3: file1: missing tag mytag"
check_tmpout "test1" "test 3: file1: missing tag test1"

TAGS=`$DAV_SYNC_BIN list-tags -s test4b tmp-sync/test4b/dir1/file3 > tmp-sync/out.txt 2> /dev/null`
if [ $? -ne 0 ]; then
	echo "test 3: file3 not pulled"
	exit 2
fi
check_tmpout "test3" "test 3: file1: missing tag test3"
check_tmpout "sub" "test 3: file1: missing tag sub"


# ----------------------------------------------------------------------------
# test 4: pull without tag filter
# expected result: remaining files pulled

dav_sync_pull test4b "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)"


