#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: Test Univention Config Registry dump
## bugs: [16637]
## tags:
##  - basic
##  - apptest
## exposure: safe

UCRLINK=ucr
UCR=univention-config-registry

Dump () {
	"$@" dump >"${TMPDIR}/${1##*/}.dump"
	local rc=$?
	if [ $rc -ne 0 ]
	then
		echo "Failed to execute \"$* dump\". Return code $rc"
		exit 110
	fi
}

Diff () {
	diff "${TMPDIR}/${1##*/}.dump" "${TMPDIR}/${2##*/}.dump"
}

##### Beginning #####
TMPDIR=$(mktemp -d) || (echo "Unable to create temporary directory"; exit 140)
trap 'rm -rf "${TMPDIR}"' EXIT

##### dump #####
echo "Checking dump..."
Dump "${UCRLINK}"
Dump "${UCR}"

if ! Diff "${UCRLINK}" "${UCR}"
then
	echo "Test failed. Output of dump is not equal."
	exit 110
fi

##### --shell dump #####
echo "Checking dump --shell..."
Dump "${UCRLINK}" --shell
Dump "${UCR}" --shell

if ! Diff "${UCRLINK}" "${UCR}"
then
	echo "Test failed. Output of --shell dump is not equal."
	exit 110
fi

echo "Checking quoting..."
"${UCR}" --keys-only search | while read -r key
do
	case "$key" in *'.*'*) continue ;; esac
	key_val="$("${UCR}" shell "^$key\$")"
	eval "value=${key_val#*=}"
	# shellcheck disable=SC2154
	[ "${value}" = "$("${UCR}" get "$key")" ] || {
		echo "$key=${value}" >&2
		exit 1
	}
done || {
	echo "Test failed. Output of --shell dump contains non-quoted values."
	echo "Bug exists as expected: Bug #16637"
	exit 121
}

##### End #####
exit 0
