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

