#! /bin/sh
#
# update-step-1
#
# Determine the partition to use for update and save its name into a /tmp file.
#
# Logilin 2020 - All right reserved.

PARTITION_PREFIX="/dev/mmcblk0p"
BOOT_PARTITION_A=2
BOOT_PARTITION_B=3
PARTITION_NAME_FILE="/tmp/partition"

if ! /bin/mount /boot -o ro
then
	echo "Unable to mount /boot partition." >&2
	exit 1
fi

line=$(/sbin/fw_printenv current_boot)
if [ $? -eq 0 ]
then
        export $line
else
        current_boot=$(/usr/bin/lsblk | /bin/grep 'part[[:blank:]]\+/$' | /bin/sed 's/^.*mmcblk.p\(.\).*$/\1/')
fi
/bin/umount /boot

if [ "${current_boot}" = "${BOOT_PARTITION_A}" ]
then
	new_boot="${BOOT_PARTITION_B}"
else
	new_boot="${BOOT_PARTITION_A}"
fi

partition="${PARTITION_PREFIX}${new_boot}"


/usr/bin/printf "${partition}" > "${PARTITION_NAME_FILE}"

sleep 1
exit 0
