#! /bin/sh
#
# update-step-6 <compressed-tarball>
#
# Verify the correct deployment of the tarball.
#
# Logilin 2020 - All right reserved.

UPDATE_MNT="/tmp/mnt"
BOARD_NAME_FILE="/etc/hytem-board-name"
TARBALL="$1"


compatible_machine()
{
	local mach_1="$1"
	local mach_2="$2"

	if [ $# -ne 2 ]; then return 1; fi

	# Same machine -> compatible
	if [ "${mach_1}" = "${mach_2}" ]; then return 0; fi

	# One of the machine is a Raspberry Pi 3 -> not compatible
	if [ "${mach_1#rb3}" != "${mach_1}" ]; then return 1; fi
	if [ "${mach_2#rb3}" != "${mach_2}" ]; then return 1; fi

	# We have Banana Pi and a Banana Pro, compare the screen type.
	if [ "${mach_1#bp?}" = "${mach_2#bp?}" ]; then return 0; fi

	return 1
}


if ! compatible_machine $(cat "${BOARD_NAME_FILE}")  $(cat "${UPDATE_MNT}${BOARD_NAME_FILE}")
then
	echo "${TARBALL} is not for this kind of board" >&2
	cd
	/bin/umount "${UPDATE_MNT}"
	/bin/rm -rf "${UPDATE_MNT}"
	/bin/rm -f "${TARBALL}"
	exit 1
fi

/bin/umount "${UPDATE_MNT}"
/bin/rm -rf "${UPDATE_MNT}"
/bin/rm -f "${TARBALL}"

exit 0
