#!/bin/bash -e
# SPDX-License-Identifier: LGPL-2.1-or-later

BUILD_DIR=$1
TREE_DUMP=$2
SYSFS_INPUT=$3
EXPECTED_OUTPUT=$4

TEST_NAME="$(basename -s .out "$EXPECTED_OUTPUT")"
TEST_DIR="${BUILD_DIR}/${TEST_NAME}"
ACTUAL_OUTPUT="${TEST_DIR}.out"

rm -rf "${TEST_DIR}"
mkdir "${TEST_DIR}"
tar -x -f "${SYSFS_INPUT}" -C "${TEST_DIR}"

HOSTNQN="nqn.2014-08.org.nvmexpress:uuid:ce4fee3e-c02c-11ee-8442-830d068a36c6"
HOSTID="ce4fee3e-c02c-11ee-8442-830d068a36c6"

cmd=(
	"${TREE_DUMP}"
	--set-options "test-sysfs-dir=${TEST_DIR},hostnqn=${HOSTNQN},hostid=${HOSTID}"
)

echo "Running command:"
printf '%q ' "${cmd[@]}"
printf '> %q\n' "$ACTUAL_OUTPUT"

if "${cmd[@]}" > "$ACTUAL_OUTPUT"; then
	:
else
	rc=$?
	echo "test failed (exit code $rc)"
	exit "$rc"
fi

diff -u "${EXPECTED_OUTPUT}" "${ACTUAL_OUTPUT}"
