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

# stat parameter for getting unix time
STAT_TIME_FMT="-c %Y"

TEST=`uname -a | grep BSD`
if [ $? -ne 0 ];
then
	STAT_TIME_FMT="-f %m"
fi
TEST=`uname -a | grep Darwin`
if [ $? -ne 0 ];
then
	STAT_TIME_FMT="-f %m"
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-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 file with all kinds of metadata and sync
# expected result: everything synced

mkdir tmp-sync/test4a/dir1

echo "#!/bin/sh" > tmp-sync/test4a/file1
echo "echo file1out" >> tmp-sync/test4a/file1

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

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

# add xattr
$XATTR set tmp-sync/test4a/file1 attr1 testvalue

# set mtime
touch -t 01011200 tmp-sync/test4a/file1

# +x
chmod +x tmp-sync/test4a/file1

dav_sync_push test4a "test 1: push failed"
check_tmpout "4 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 "4 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)"

# check metadata
MTIMEA1=`stat $STAT_TIME_FMT tmp-sync/test4a/file1`
MTIMEB1=`stat $STAT_TIME_FMT tmp-sync/test4b/file1`

if [ "$MTIMEA1" != "$MTIMEB1" ]; then
	echo "test 1: mtime not synced"
	exit 2
fi

OUT=`tmp-sync/test4b/file1 2> /dev/null`
if [ "$OUT" != "file1out" ]; then
	echo "test 1: not executable"
	exit 2
fi

OUT=`$XATTR get tmp-sync/test4b/file1 attr1 2> /dev/null`
if [ "$OUT" != "testvalue" ]; then
	echo "test 1: xattr not synced"
	exit 2
fi

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 1: list-tags failed"
	exit 2
fi
check_tmpout "mytag" "test 1: file1: missing tag mytag"
check_tmpout "test1" "test 1: file1: missing tag test1"


# ----------------------------------------------------------------------------
# test 2: copy file (without xattr and tags) and sync
# expected result: WebDAV COPY, but metadata adjusted

cp tmp-sync/test4a/file1 tmp-sync/test4a/copy1

$XATTR remove tmp-sync/test4a/copy1 tags > /dev/null 2>&1
$XATTR remove tmp-sync/test4a/copy1 attr1 > /dev/null 2>&1

chmod -x tmp-sync/test4a/copy1

touch tmp-sync/test4a/copy1

dav_sync_push test4a "test 2: push failed"
check_tmpout "1 file 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)"
check_tmpout "copy" "test 2: no copy (push)"

dav_sync_pull test4b "test 2: pull failed"
check_tmpout "1 file pulled" "test 2: wrong pull counter"
check_tmpout "0 conflicts" "test 2: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 2: wrong error counter (pull)"

# check metadata
MTIMEA1=`stat $STAT_TIME_FMT tmp-sync/test4a/copy1 2> /dev/null`
MTIMEB1=`stat $STAT_TIME_FMT tmp-sync/test4b/copy1 2> /dev/null`

if [ "$MTIMEA1" != "$MTIMEB1" ]; then
	echo "test 2: mtime not synced"
	exit 2
fi

OUT=`tmp-sync/test4b/copy1 2> /dev/null`
if [ $? -eq 0 ]; then
	echo "test 2: copy1 should be executable"
	exit 2
fi

TAGS=`$DAV_SYNC_BIN list-tags -s test4b tmp-sync/test4b/copy1 > tmp-sync/out.txt 2> /dev/null`
if [ $? -ne 0 ]; then
	echo "test 2: list-tags failed"
	exit 2
fi
LN=`cat tmp-sync/out.txt | wc -l`
if [ $LN -ne 0 ]; then
	echo "test 2: list-tags not empty"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 3: push again
# expected result: 0 files pushed/updated

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

LN=`cat tmp-sync/out.txt | wc -l 2> /dev/null`
if [ $LN -ne 1 ]; then
	echo "test 3: wrong line count"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 4: pull test4b again
# expected result: 0 files pushed/updated

dav_sync_pull test4b "test 4: pull failed"
check_tmpout "0 files 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)"

LN=`cat tmp-sync/out.txt | wc -l 2> /dev/null`
if [ $LN -ne 1 ]; then
	echo "test 4: wrong line count"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 5: move file1
# expected result: file moved, metadata unchanged

mv tmp-sync/test4a/file1 tmp-sync/test4a/move1

dav_sync_push test4a "test 5: push failed"
check_tmpout "1 file pushed" "test 5: wrong push counter"
check_tmpout "0 conflicts" "test 5: wrong conflict counter (push)"
check_tmpout "0 errors" "test 5: wrong error counter (push)"
check_tmpout "move" "test 5: no move (push)"

dav_sync_pull test4b "test 5: pull failed"
check_tmpout "1 file pulled" "test 5: wrong pull counter"
check_tmpout "0 conflicts" "test 5: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 5: wrong error counter (pull)"
check_tmpout "move" "test 5: no move (pull)"

# check metadata
MTIMEA1=`stat $STAT_TIME_FMT tmp-sync/test4a/move1 2> /dev/null`
MTIMEB1=`stat $STAT_TIME_FMT tmp-sync/test4b/move1 2> /dev/null`

if [ $MTIMEA1 -ne $MTIMEB1 ]; then
	echo "test 5: mtime not synced"
	exit 2
fi

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

OUT=`$XATTR get tmp-sync/test4b/move1 attr1 2> /dev/null`
if [ "$OUT" != "testvalue" ]; then
	echo "test 5: xattr not synced"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 6: remove metadata, move file move1 and sync
# expected result: file moved, no tags/xattr

$DAV_SYNC_BIN remove-tag -s test4a tmp-sync/test4a/move1 test1
$DAV_SYNC_BIN remove-tag -s test4a tmp-sync/test4a/move1 mytag

$XATTR remove tmp-sync/test4a/move1 attr1

mv tmp-sync/test4a/move1 tmp-sync/test4a/move_noxattr

dav_sync_push test4a "test 6: push failed"
check_tmpout "1 file 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_tmpout "move" "test 6: no move (push)"

dav_sync_pull test4b "test 6: pull failed"
check_tmpout "1 file pulled" "test 6: wrong pull counter"
check_tmpout "0 conflicts" "test 6: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 6: wrong error counter (pull)"
check_tmpout "move" "test 6: no move (pull)"

# check metadata
MTIMEA1=`stat $STAT_TIME_FMT tmp-sync/test4a/move_noxattr 2> /dev/null`
MTIMEB1=`stat $STAT_TIME_FMT tmp-sync/test4b/move_noxattr 2> /dev/null`

if [ $MTIMEA1 -ne $MTIMEB1 ]; then
	echo "test 6: mtime not synced"
	exit 2
fi

OUT=`$XATTR get tmp-sync/test4b/move1 attr1 2> /dev/null`
if [ "$OUT" = "testvalue" ]; then
	echo "test 6: xattr not synced"
	exit 2
fi

TAGS=`$DAV_SYNC_BIN list-tags -s test4b tmp-sync/test4b/move_noxattr | wc -l`
if [ $TAGS -ne 0 ]; then
	echo "test 6: tags not removed"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 7: create dir and add xattr and tags, sync
# expected result: dir with all metadata synced

mkdir tmp-sync/test4a/newdir1

$XATTR set tmp-sync/test4a/newdir1 dirattribute1 "test7attribute"
$XATTR set tmp-sync/test4a/newdir1 dirattribute2 "directory"

$DAV_SYNC_BIN add-tag -s test4a tmp-sync/test4a/newdir1 "test7"

dav_sync_push test4a "test 7: push failed"
check_tmpout "0 conflicts" "test 7: wrong conflict counter (push)"
check_tmpout "0 errors" "test 7: wrong error counter (push)"
check_tmpout "mkcol" "test 7: no mkcol (push)"

dav_sync_pull test4b "test 7: pull failed"
check_tmpout "0 conflicts" "test 7: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 7: wrong error counter (pull)"

# check metadata
OUT=`$XATTR get tmp-sync/test4b/newdir1 dirattribute1 2> /dev/null`
if [ "$OUT" != "test7attribute" ]; then
	echo "test 7: xattr 1 not synced"
	exit 2
fi
OUT=`$XATTR get tmp-sync/test4b/newdir1 dirattribute2 2> /dev/null`
if [ "$OUT" != "directory" ]; then
	echo "test 7: xattr 1 not synced"
	exit 2
fi

TAGS=`$DAV_SYNC_BIN list-tags -s test4b tmp-sync/test4b/newdir1 > tmp-sync/out.txt 2> /dev/null`
if [ $? -ne 0 ]; then
	echo "test 7: list-tags failed"
	exit 2
fi
check_tmpout "test7" "test 7: tag test7 missing"


# ----------------------------------------------------------------------------
# test 8: sync again
# expected result: no update for newdir1

dav_sync_push test4a "test 8: push failed"
check_tmpout "0 conflicts" "test 8: wrong conflict counter (push)"
check_tmpout "0 errors" "test 8: wrong error counter (push)"
LN=`cat tmp-sync/out.txt | wc -l 2> /dev/null`
if [ $LN -ne 1 ]; then
	echo "test 8 wrong line count"
	exit 2
fi

dav_sync_pull test4b "test 8: pull failed"
check_tmpout "0 conflicts" "test 8: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 8: wrong error counter (pull)"
LN=`cat tmp-sync/out.txt | wc -l 2> /dev/null`
if [ $LN -ne 1 ]; then
	echo "test 8 wrong line count"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 9: change newdir1 metadata and sync
# expected result: update

$XATTR set tmp-sync/test4a/newdir1 dirattribute1 "test9attribute"
$XATTR set tmp-sync/test4a/newdir1 new "hello"

$DAV_SYNC_BIN add-tag -s test4a tmp-sync/test4a/newdir1 "test9"

dav_sync_push test4a "test 9: push failed"
check_tmpout "0 conflicts" "test 9: wrong conflict counter (push)"
check_tmpout "0 errors" "test 9: wrong error counter (push)"
check_tmpout "update" "test 9: no mkcol (push)"

dav_sync_pull test4b "test 9: pull failed"
check_tmpout "0 conflicts" "test 9: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 9: wrong error counter (pull)"
check_tmpout "update" "test 9: no mkcol (pull)"

# check metadata
OUT=`$XATTR get tmp-sync/test4b/newdir1 dirattribute1 2> /dev/null`
if [ "$OUT" != "test9attribute" ]; then
	echo "test 9: xattr 1 not synced"
	exit 2
fi
OUT=`$XATTR get tmp-sync/test4b/newdir1 dirattribute2 2> /dev/null`
if [ "$OUT" != "directory" ]; then
	echo "test 9: xattr 2 not synced"
	exit 2
fi
OUT=`$XATTR get tmp-sync/test4b/newdir1 new 2> /dev/null`
if [ "$OUT" != "hello" ]; then
	echo "test 9: xattr 3 not synced"
	exit 2
fi

TAGS=`$DAV_SYNC_BIN list-tags -s test4b tmp-sync/test4b/newdir1 > tmp-sync/out.txt 2> /dev/null`
if [ $? -ne 0 ]; then
	echo "test 9: list-tags failed"
	exit 2
fi
check_tmpout "test9" "test 9: tag test9 missing"


# ----------------------------------------------------------------------------
# test 10: sync again
# expected result: no update for newdir1

dav_sync_push test4a "test 10: push failed"
check_tmpout "0 conflicts" "test 10: wrong conflict counter (push)"
check_tmpout "0 errors" "test 10: wrong error counter (push)"
LN=`cat tmp-sync/out.txt | wc -l 2> /dev/null`
if [ $LN -ne 1 ]; then
	echo "test 10 wrong line count"
	exit 2
fi

dav_sync_pull test4b "test 10: pull failed"
check_tmpout "0 conflicts" "test 10: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 10: wrong error counter (pull)"
LN=`cat tmp-sync/out.txt | wc -l 2> /dev/null`
if [ $LN -ne 1 ]; then
	echo "test 10 wrong line count"
	exit 2
fi
