#!/bin/sh
# This script is for firmware updater to test
# if new firmware can use current configuration.

# executed path
path="${0%/*}"
# current firmware path
gwconfPath='/usr/local/bin/gwconf-files'

die () {
	echo >&2 "$@"
	exit 1
}

testConf() {
	# $1: setup path
	# $2: message
	if ! test -e "${1}"; then
		echo "gwconf: ${2} setup missing, skip test"
		return 0
	fi
	if extractAndTestTarXz "${1}"; then
		echo "gwconf: ${2} setup OK"
	else
		die "gwconf: ${2} setup failed"
	fi
}

# diff
if [ -e "${gwconfPath}" ]; then
	echo "gwconf: compare versions"
	if [ "${path}" = "${gwconfPath}" ]; then
		echo "gwconf: same path, skip test"
		exit 0
	fi

	"${path}/gensha256"
	if (
		cd "${gwconfPath}"
		sha256sum -c "${path}/sha256" &>/dev/null
	); then
		echo "gwconf: same version, skip test"
		exit 0
	fi
else
	echo "gwconf: not present"
fi
echo "gwconf: start test"

# test
. "${path}/gwconf-common"
filterPath="${path}/filters"

testConf "${current_setup_file}"      'current'
testConf "${DEFAULT_USER_SETUP_FILE}" 'user default'
testConf "${DEFAULT_SETUP_FILE}"      'default'

echo "gwconf: test OK"
