#!/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 [ $# -lt 2 ];
then
	echo "usage:" $0 "<dav-binary> <dav-sync-binary>"
	exit 1
fi

# check if binaries exist
which $1 > /dev/null
if [ $? -ne 0 ];
then
	exit 1
fi
which $2 > /dev/null
if [ $? -ne 0 ];
then
	exit 1
fi

DAV_BIN=$1
DAV_SYNC_BIN=$2
export DAV_BIN
export DAV_SYNC_BIN

# create config
rm -Rf .dav
cp -R $HOME/.dav/ .dav/
cp dav-home/sync.xml .dav/sync.xml
export HOME=`pwd`

# tmp sync dir
rm -Rf tmp-sync
mkdir tmp-sync
mkdir tmp-sync/test1a
mkdir tmp-sync/test1b
mkdir tmp-sync/test2a
mkdir tmp-sync/test2b

# check if config works
TEST1_DIR=`$DAV_SYNC_BIN list-dirs | grep test1a | tail -1`
if [ -z "$TEST1_DIR" ];
then
	echo "Config not working"
	rm -Rf .dav
	rm -Rf tmp-sync
	exit 1
fi

# clean test repo and files
$DAV_BIN rm -N dav-test-repo/tests/ 2> /dev/null
$DAV_BIN rm -N dav-test-repo/sync/ 2> /dev/null

do_test()
{
	printf "TEST: $1 ... "
	./$2
	if [ $? -ne 0 ]; then
		rm -Rf .dav
		exit 2
	fi
	echo ok
}

#
# ok, begin the tests
#
#do_test "dav-sync push (1)" test-dav-sync-push1.sh
#do_test "dav-sync pull (1)" test-dav-sync-pull1.sh
#do_test "dav-sync pull conflict (1)" test-dav-sync-pull-conflict.sh
#do_test "dav-sync push conflict (1)" test-dav-sync-push-conflict.sh
#do_test "dav-sync hashing (1)" test-dav-sync-hashing1.sh
#do_test "dav-sync hashing (2)" test-dav-sync-hashing1.sh
#do_test "dav-sync hash strategy" test-dav-sync-hash-strategy.sh
#do_test "dav-sync hash conflict resolution" test-dav-sync-hash-conflictres.sh
#do_test "dav-sync hashing change cfg" test-dav-sync-hashing-cfgchange.sh
do_test "dav-sync metadata (1)" test-dav-sync-metadata1.sh
do_test "dav-sync metadata (2)" test-dav-sync-metadata2.sh
do_test "dav-sync metadata (3)" test-dav-sync-metadata3.sh
do_test "dav-sync metadata (4)" test-dav-sync-metadata4.sh
do_test "dav-sync versioning (1)" test-dav-sync-versioning1.sh
do_test "dav-sync split (1)" test-dav-sync-split1.sh
do_test "dav-sync symlink (1)" test-dav-sync-symlink1.sh

# cleanup
$DAV_BIN rm dav-test-repo/sync/test1 > /dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "cleanup failed"
	exit 2
fi

rm -Rf .dav
rm -Rf tmp-sync

