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

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

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

# ----------------------------------------------------------------------------
# 1. test: pull empty collection
# expected result: 0 files pulled, 0 errors
dav_sync_pull test1b "test1: pull failed"

check_tmpout "0 files pulled" "pull1: wrong pull counter"
check_tmpout "0 errors" "pull1: wrong error counter"
check_tmpout "0 conflicts" "pull1: wrong conflicts counter"

# ----------------------------------------------------------------------------
# 2. test: add 2 files to test1a and push it and pull test1b
# expected result: 2 files pushed, 2 files pulled

rm -f .dav/dav-sync-tests-test1a-db.xml

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

dav_sync_push test1a "test2: push failed"
check_tmpout "2 files pushed" "test2: wrong push counter"

dav_sync_pull test1b "test2: pull failed"
check_tmpout "2 files pulled" "test2: wrong pull counter"
check_tmpout "0 errors" "test2: wrong error counter"
check_tmpout "0 conflicts" "test2: wrong conflicts counter"

# ----------------------------------------------------------------------------
# 3. test: pull empty directory
# expected result: empty directory created

mkdir tmp-sync/test1a/emptydir
dav_sync_push test1a "test3: push failed"
check_tmpout "mkcol" "test3: mkcol missing"



dav_sync_pull test1b "test3: pull failed"
ls tmp-sync/test1b/emptydir > /dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "emptydir not created"
	exit 2
fi

# check if file1 or file2 are pulled
TEST=`cat tmp-sync/out.txt | grep "get: /file1"`
if [ $? -eq 0 ]; then
	echo "test3: file1 must not be pulled"
	exit 2	
fi
TEST=`cat tmp-sync/out.txt | grep "get: /file2"`
if [ $? -eq 0 ]; then
	echo "test3: file2 must not be pulled"
	exit 2	
fi

# ----------------------------------------------------------------------------
# 4. test: do nothing, test if double mkdir happens
# expected result: no files pulled, no mkdir

dav_sync_pull test1b "test4: pull failed"

check_tmpout "0 files pulled" "test4: wrong pull counter"
check_tmpout "0 errors" "test4: wrong error counter"
check_tmpout "0 conflicts" "test4: wrong conflicts counter"

OUT=`cat tmp-sync/out.txt | wc -l`
if [ "$OUT" != "1" ]; then	
	echo "test4: wrong line count"
	exit 2
fi

# ----------------------------------------------------------------------------
# 5. test: pull changed files
# expected result: 1 file pulled

echo testx2 > tmp-sync/test1a/file1
dav_sync_push test1a "test5: push failed"
check_tmpout "1 file pushed" "test5: wrong push counter"

dav_sync_pull test1b "test5: pull failed"
check_tmpout "0 errors" "test5: wrong error counter"
check_tmpout "0 conflicts" "test5: wrong conflicts counter"

# ----------------------------------------------------------------------------
# 6. test: pull new dir and files
# expected result: mkdir and some files pulled

mkdir tmp-sync/test1a/subdir
cp testdir/subdir/sub1 tmp-sync/test1a/subdir/
cp testdir/subdir/sub2 tmp-sync/test1a/subdir/
dav_sync_push test1a "test6: push failed"

dav_sync_pull test1b "test6: pull failed"
check_tmpout "get: /subdir/sub1" "test6: sub1 not pulled"
check_tmpout "get: /subdir/sub2" "test6: sub2 not pulled"
