#!/bin/sh
#set -x

# This file is AUTO GENERATED, do not modify it or your changes might be
# lost!
#
# shell test suite v0.4 for kdb command
#set -x


#
# VARIABLES
#

# variables to count up errors and tests
nbError=0
nbTest=0

# the script itself
scriptName=`basename $0`

#RANDOM does not work with testdata
#RANDOM=`mktemp -u --tmpdir=/ XXXXXXXXXXXX`
MOUNTPOINT="/tests/script$RANDOM"
RANDOMNAME=`echo $RANDOM | tr / _`
MOUNTNAME="_tests_script$RANDOMNAME"

USER_ROOT="user/tests/script$RANDOM"
SYSTEM_ROOT="system/tests/script$RANDOM"

IS_INSTALLED=YES
if [ "x$IS_INSTALLED" = "xYES" ]
then
	DATADIR="/usr/share/elektra/test_data/shell"
else
	DATADIR="/build/elektra-WdiOtz/elektra-0.8.7/tests/shell/shell"
fi

USER="`id -un`"
GROUP="`id -gn`"

SYSTEM_FOLDER=/etc/kdb
if [ -n "$HOME" ]
then
	USER_FOLDER=$HOME/.kdb
else
	USER_FOLDER=/home/$USER/.kdb
fi
PLUGINS=`echo "dump;resolver;template;doc;ccode;error;fstab;hexcode;hidden;ni;null;struct;success;tracer;type;validation;glob;hosts;iconv;network;path;keytometa;syslog;uname;timeofday;simpleini;yajl;dbus;tcl;xmltool" | tr ';' ' '`

DATE=`date "+%b %d %H:%M"`



#
# ASSERTIONS
#

#succeed if the previous command was successful
succeed_if ()
{
	if [ $? != "0" ]
	then
		nbError=$(( $nbError + 1 ))
		echo error: $*
	fi
	nbTest=$(( $nbTest + 1 ))
}

#fails and exits the program if the previous command failed
exit_if_fail ()
{
	if [ $? != "0" ]
	then
		if type cleanup
		then
			echo "fatal: $*"
			cleanup
		else
			echo "fatal (no cleanup): $*"
		fi
		exit 1
	fi
	nbTest=$(( $nbTest + 1 ))
}

end_script()
{
	echo "$scriptName RESULTS: $nbTest test(s) done $nbError error(s)."
	exit "$nbError"
}


#
# COMMON CHECKS
#
KDB_VERSION="0.8.7"
SO_VERSION="4"

check_version()
{
	echo "Check if script tests the correct version"

	REAL_KDB_VERSION="`$KDB get system/elektra/version/constants/KDB_VERSION 2> /dev/null`"
	[ "x$REAL_KDB_VERSION" = "x$KDB_VERSION" ]
	exit_if_fail "Script was not compiled ($KDB_VERSION) with this elektra version ($REAL_KDB_VERSION): KDB_VERSION mismatch"

	REAL_SO_VERSION="`$KDB get system/elektra/version/constants/SO_VERSION 2> /dev/null`"
	[ "x$REAL_SO_VERSION" = "x$SO_VERSION" ]
	exit_if_fail "Script was not compiled ($SO_VERSION) with this elektra version ($REAL_SO_VERSION): SO_VERSION mismatch"
}

check_remaining_files()
{
	USER_REMAINING="`find $USER_FOLDER -maxdepth 1 -name $1'*' -print -quit`"
	test -z "$USER_REMAINING"
	exit_if_fail "files $USER_REMAINING in $USER_FOLDER would be removed during tests, so test is aborted"

	SYSTEM_REMAINING="`find $SYSTEM_FOLDER -maxdepth 1 -name $1'*' -print -quit`"
	test -z "$SYSTEM_REMAINING"
	exit_if_fail "files $SYSTEM_REMAINING in $SYSTEM_FOLDER would be removed during tests, so test is aborted"
}

check_set_rm()
{
	$KDB set "$1" "$2" 1>/dev/null
	succeed_if "could not set $1 with $2"

	[ "x`$KDB get "$1" 2> /dev/null`" = "x$2" ]
	succeed_if "cant get $1 (expected $2)"

	$KDB rm "$1" 1>/dev/null
	succeed_if "could not rm $1 (having value $2)"

	[ "x`$KDB sget $1 defvalue 2> /dev/null`" = "xdefvalue" ]
	succeed_if "Value still there after remove"
}


check_set_mv_rm()
{
	$KDB set "$1" "$3" 1>/dev/null
	succeed_if "could not set $1 with $3"

	[ "x`$KDB get "$1" 2> /dev/null`" = "x$3" ]
	succeed_if "cant get $1 (expected $3)"

	$KDB mv "$1" "$2" 1>/dev/null
	succeed_if "could not mv $1 to $2"

	[ "x`$KDB sget $1 defvalue 2> /dev/null`" = "xdefvalue" ]
	succeed_if "Value still there after move"

	[ "x`$KDB get "$2" 2> /dev/null`" = "x$3" ]
	succeed_if "cant get $2 (expected $3)"

	$KDB rm "$2" 1>/dev/null
	succeed_if "could not rm $2 (having value $3)"

	[ "x`$KDB sget $2 defvalue 2> /dev/null`" = "xdefvalue" ]
	succeed_if "Value still there after remove"
}



#
# COMMON UTILITIES
#


is_not_rw_storage()
{
	test ! "x`$KDB info $PLUGIN provides 2>/dev/null`" = "xstorage" \
	-o "x$PLUGIN" = "xhosts" \
	-o "x$PLUGIN" = "xfstab" \
	-o "x$PLUGIN" = "xuname" \
	-o "x$PLUGIN" = "xaugeas"
}

is_plugin_available()
{
	for PLUGIN in $PLUGINS
	do
		if [ "x$1" = "x$PLUGIN" ]
		then
			return 0
		fi
	done
	return 1
}









































































































































































































































































































































































































































































































































































































































































































































































































































# empty lines up to 1000 so that line numbers are more useful
if [ -z "$KDB" ]; then KDB=kdb-full; fi

echo
echo ELEKTRA BASIC COMMAND SCRIPTS TESTS
echo

check_version

VALUE=value

#override for specific testing
#PLUGINS=tcl

for PLUGIN in $PLUGINS
do
	if is_not_rw_storage
	then
		echo "$PLUGIN not a read-write storage"
		continue;
	fi

	case "$PLUGIN" in
	"tcl")
		MOUNT_PLUGIN="tcl ccode null"
		;;
	"yajl")
		MOUNT_PLUGIN="$PLUGIN"
		#TODO: add plugin to fix problem
		DO_NOT_TEST_ROOT_VALUE="yes"
		;;
	"simpleini")
		MOUNT_PLUGIN="simpleini ccode null"
		;;
	*)
		MOUNT_PLUGIN="$PLUGIN"
		;;
	esac

	unset -f cleanup
	FILE=test.$PLUGIN

	check_remaining_files $FILE

	$KDB mount $FILE $MOUNTPOINT $MOUNT_PLUGIN 1>/dev/null
	exit_if_fail "could not mount $FILE at $MOUNTPOINT using $MOUNT_PLUGIN"

	cleanup()
	{
		$KDB umount $MOUNTNAME >/dev/null
		succeed_if "could not umount $MOUNTNAME"
		rm -f $USER_FOLDER/$FILE
		rm -f $SYSTEM_FOLDER/$FILE

		USER_REMAINING="`find $USER_FOLDER -maxdepth 1 -name $FILE'*' -print -exec rm {} +`"
		test -z "$USER_REMAINING"
		succeed_if "found remaining files $USER_REMAINING in $USER_FOLDER"

		SYSTEM_REMAINING="`find $SYSTEM_FOLDER -maxdepth 1 -name $FILE'*' -print -exec rm {} +`"
		test -z "$SYSTEM_REMAINING"
		succeed_if "found remaining files $SYSTEM_REMAINING in $SYSTEM_FOLDER"
	}

	for ROOT in $USER_ROOT $SYSTEM_ROOT
	do
		echo "do preparation for $PLUGIN in $ROOT"
		$KDB set $ROOT "root" 1>/dev/null
		succeed_if "could not set root"

		[ "x`$KDB sget $ROOT/value defvalue 2> /dev/null`" = "xdefvalue" ]
		succeed_if "Did not get default value"

		if [ "x$DO_NOT_TEST_ROOT_VALUE" != "xyes" ]
		then
			#TODO: Fix by directoryvalue plugin
			[ "x`$KDB get $ROOT 2> /dev/null`" = "xroot" ]
			succeed_if "could not get root"

			[ "x`$KDB sget $ROOT default 2> /dev/null`" = "xroot" ]
			succeed_if "could not shell get root"
		fi

		$KDB set "$ROOT/value" "$VALUE" 1>/dev/null
		succeed_if "could not set value"

		[ "x`$KDB get $ROOT/value 2> /dev/null`" = "x$VALUE" ]
		succeed_if "cant get $ROOT/value"

		[ "x`$KDB sget $ROOT/value default 2> /dev/null`" = "x$VALUE" ]
		succeed_if "cant shell get $ROOT/value"

		echo "testing ls command"

		[ "x`$KDB ls $ROOT/value 2> /dev/null`" = "x$ROOT/value" ]
		succeed_if "cant ls $ROOT (may mean that $ROOT folder is not clean)"

		echo "testing rm command"

		$KDB rm $ROOT/value 1> /dev/null
		succeed_if "could not remove user/test/value"

		$KDB get $ROOT/value 1> /dev/null
		[ $? != "0" ]
		succeed_if "got removed key $ROOT/value"

		$KDB rm $ROOT 1>/dev/null
		succeed_if "could not remove user/test/value"

		[ "x`$KDB sget $ROOT/value value 2> /dev/null`" = "xvalue" ]
		succeed_if "Did not get default value after remove"

		$KDB get $ROOT/value 1>/dev/null
		[ $? != "0" ]
		succeed_if "got removed key $ROOT"

		check_set_rm $ROOT/value other_value

		echo "testing array"

		KEY=$ROOT/hello/a/key
		$KDB set "$KEY" "$VALUE" 1>/dev/null
		succeed_if "could not set key $KEY"

		[ "x`$KDB get $KEY`" = "x$VALUE" ]
		succeed_if "$KEY is not $VALUE"

		for i in `seq 0 9`
		do
			KEY="$ROOT/hello/a/array/#$i"
			VALUE="$i"

			$KDB set "$KEY" "$VALUE" 1>/dev/null
			succeed_if "could not set key $ROOT/hello/a/array/#0"

			[ "x`$KDB get $KEY`" = "x$VALUE" ]
			succeed_if "$KEY is not $VALUE"
		done

		$KDB rm -r "$ROOT"
		succeed_if "could not remove all keys"
	done

	cleanup
done

end_script basic commands
