#!/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 "$errormsg"
		exit 2
	fi
}

$DAV_BIN mkcol dav-test-repo/sync/test1

# 1. test
# copy files to the test1a dir and run push
# expected result: 2 uploads, no errors or conflicts

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

OUT=`$DAV_SYNC_BIN push test1a | tail -n 1`

TEST=`echo $OUT | grep Result`
if [ $? -ne 0 ];
then
	echo "push 1 failed"
	exit 2
fi

TEST=`echo $OUT | grep "2 files pushed"`
if [ $? -ne 0 ];
then
	echo "wrong push counter"
	exit 2
fi

TEST=`echo $OUT | grep "0 conflicts"`
if [ $? -ne 0 ];
then
	echo "wrong conflict counter"
	exit 2
fi

TEST=`echo $OUT | grep "0 files deleted"`
if [ $? -ne 0 ];
then
	echo "wrong delete counter"
	exit 2
fi

# 2. test
# do nothing
# expected result: no uploads or updates, only status line with zeros

dav_sync_push test1a "push 2 failed"

OUT=`wc -l < tmp-sync/out.txt`
if [ "$OUT" != "1" ];
then	
	echo "push 2: number of output lines not 1"
	exit 2
fi

check_tmpout "0 files pushed" "push 2: wrong push counter"
check_tmpout "0 errors" "push 2: wrong error counter"

# 3. test
# add empty dir
# expected result: 1 mkcol
mkdir tmp-sync/test1a/emptydir

dav_sync_push test1a "push 3 failed"
check_tmpout "mkcol: /emptydir" "push 3: no mkcol"

# 4. test
# do nothing again, test if double mkcol happens
# expected result: no mkcol
dav_sync_push test1a "push 4 failed"

OUT=`wc -l < tmp-sync/out.txt`
if [ "$OUT" != "1" ];
then	
	echo "push 4: number of output lines not 1"
	exit 2
fi

check_tmpout "0 files pushed" "push 4: wrong push counter"
check_tmpout "0 errors" "push 4: wrong error counter"


# 5. test
# touch file
# expected result: upload touched file
sleep 2
touch tmp-sync/test1a/file1

dav_sync_push test1a "push 5 failed"
check_tmpout "put: /file1" "push 5: no put"
check_tmpout "1 file pushed" "push 5: wrong push counter"
check_tmpout "0 conflicts" "push 5: wrong conflict counter"
check_tmpout "0 files deleted" "push 5: wrong delete counter"
check_tmpout "0 errors" "push 5: wrong error counter"

# 6. test
# add deep dir hierarchy and some files, test if mkcol order is fine
# expected result: multiple mkcol requests and some puts after that
mkdir -p tmp-sync/test1a/dir_a/1/2/3/4/5/6/7/8
mkdir -p tmp-sync/test1a/dir_a/1/2/3/a/b/c/d
mkdir -p tmp-sync/test1a/dir_a/i/j/k
mkdir -p tmp-sync/test1a/dir_b/1/2/3/4/5/6/7/8
mkdir -p tmp-sync/test1a/dir_b/1/2/3/a/b/c/d
mkdir -p tmp-sync/test1a/dir_b/i/j/k
mkdir -p tmp-sync/test1a/dir_c/sub1/sub2/sub3/sub4
mkdir -p tmp-sync/test1a/dir_c/sub1/sub2/1
mkdir -p tmp-sync/test1a/dir_c/sub1/sub2/2
mkdir -p tmp-sync/test1a/dir_c/sub1/sub2/3
mkdir -p tmp-sync/test1a/dir_c/sub1/sub2/4
mkdir -p tmp-sync/test1a/dir_c/sub1/sub2/5
mkdir -p tmp-sync/test1a/dir_c/sub_a/x
mkdir -p tmp-sync/test1a/dir_c/sub_a/y/d1/d2/d3
touch tmp-sync/test1a/dir_a/1/2/3/4/5/6/7/8/file1
touch tmp-sync/test1a/dir_a/1/2/3/4/5/6/7/file1
touch tmp-sync/test1a/dir_a/1/2/3/4/5/6/file1
touch tmp-sync/test1a/dir_a/1/2/3/a/b/c/d/d_file1
touch tmp-sync/test1a/dir_a/1/2/3/a/b/b_file1
touch tmp-sync/test1a/dir_a/a_file
echo "test6-file-1" > tmp-sync/test1a/dir_b/1/2/3/4/5/6/7/8/t6f1-8
echo "test6-file-2" > tmp-sync/test1a/dir_b/i/t6f2-i
echo "test6-file-3" > tmp-sync/test1a/dir_c/sub1/sub2/sub3/sub4/t6f3-s4
echo "test6-file-4" > tmp-sync/test1a/dir_c/sub1/sub2/3/t6f4-3

dav_sync_push test1a "push 6 failed"
check_tmpout "10 files pushed" "push6: wrong push counter"
check_tmpout "0 files deleted" "push6: wrong delete counter"
check_tmpout "0 conflicts" "push6: wrong conflict counter"
check_tmpout "0 errors" "push6: wrong error counter"
OUT=`grep mkcol tmp-sync/out.txt | wc -l`
if [ "$OUT" != "48" ];
then	
	echo "push 6: number of mkcol output lines not 48"
	exit 2
fi

# 7. test
# delete file
# expected result: delete file on server
rm -f tmp-sync/test1a/file1

dav_sync_push test1a "push 7 failed"
check_tmpout "0 files pushed" "push7: wrong push counter"
check_tmpout "1 file deleted" "push7: wrong delete counter"
check_tmpout "0 conflicts" "push7: wrong conflict counter"
check_tmpout "0 errors" "push7: wrong error counter"

# 8. test
# do nothing, test if double delete happens
# expected result: no delete

dav_sync_push test1a "push 8 failed"
check_tmpout "0 files pushed" "push8: wrong push counter"
check_tmpout "0 files deleted" "push8: wrong delete counter"
check_tmpout "0 conflicts" "push8: wrong conflict counter"
check_tmpout "0 errors" "push8: wrong error counter"

# 9. test
# delete multiple files
# expected result: multiple delete requests

rm -Rf tmp-sync/test1a/dir_a
rm -f tmp-sync/test1a/dir_b/1/2/3/4/5/6/7/8/t6f1-8
rm -f tmp-sync/test1a/dir_b/i/t6f2-i

dav_sync_push test1a "push 9 failed"

# don't test if there was a single delete for each collection
# and don't check if the delete counter has a specific value
# because maybe there will be some optimizations 
check_tmpout "0 files pushed" "push8: wrong push counter"
check_tmpout "0 conflicts" "push8: wrong conflict counter"
check_tmpout "0 errors" "push8: wrong error counter"

OUT=`grep "delete: /dir_a/" tmp-sync/out.txt`
if [ $? -ne 0 ];
then
	echo "missing: delete /dir_a/"
	exit 2
fi
OUT=`grep "delete: /dir_b/i/t6f2-i" tmp-sync/out.txt`
if [ $? -ne 0 ];
then
	echo "missing: delete: /dir_b/i/t6f2-i"
	exit 2
fi
OUT=`grep "delete: /dir_b/1/2/3/4/5/6/7/8/t6f1-8" tmp-sync/out.txt`
if [ $? -ne 0 ];
then
	echo "missing: delete: /dir_b/1/2/3/4/5/6/7/8/t6f1-8"
	exit 2
fi



