. /usr/sbin/rpi4-boot-vars

# ensure sudo user
if [ "$(whoami)" != "root" ]
then
	echo "Error: This script requires 'sudo' privileges."
	exit 1
fi

# get system architecture
systemarch=$(rpm --eval '%_host_cpu')
if [ "$systemarch" != aarch64 -a "$systemarch" != armh ]; then
	echo "Error: This script is only for aarch64 and armv7l (armh)"
	exit 1
fi

remount_handler()
{
	local rc="$1"
# Read-only remounting is no longer needed
#	if mountpoint -q "$bootmountpoint"; then
#		mount -o remount,ro "$bootmountpoint"
#	fi
	exit "$rc"
}

exit_handler()
{
	remount_handler $?
}

signal_handler()
{
	remount_handler 2
}

# Old mount point. Recording remounting may be required.
if [ "$bootmountpoint" = "/mnt/FIRMPART" ]; then
	trap exit_handler EXIT
	trap signal_handler HUP PIPE INT QUIT TERM
	# bootmountpoint must exist
	[ -n "$(grep "^\s*[^#].*$bootmountpoint" /etc/fstab)" ] ||
		(echo "$bootmountpoint not found"; exit 1)
	# Mount or remount BOOT with rw rights
	# bootmountpoint must exist
	if mountpoint -q "$bootmountpoint"; then
		mount -o remount,rw "$bootmountpoint"
	else
		mount -o rw "$bootmountpoint"
	fi
fi

# Determine the target kernel and its version
kernel_info_get () {
if [ -n "$1" ]; then
	if [ "$1" == "--default" ]; then
		if [ -L ${kernelpath}z ]; then
			kernelfilename=$(readlink ${kernelpath}z)
		elif [ -L ${kernelpath}x ]; then
			kernelfilename=$(readlink ${kernelpath}x)
		else
			echo "Error: No default kernel in the system."
			exit 1
		fi
	else
		kernelfilename=$(ls /boot | grep vmlinu | grep "$1" | sort | tail -1 )
		[ -L "/boot/$kernelfilename" ] &&
		  kernelfilename=$(readlink "/boot/$kernelfilename")
		if [ ! -f "/boot/$kernelfilename" ]; then
			echo "Error: No kernel with flavour $1 in the system."
			echo "Please choose one of the following flavours:"
			ls /boot | grep vmlinu | grep "$1" | sort
			echo "or install new kernel-image package."
			exit 1
		fi
	fi
	kver=${kernelfilename#*-}
else
# User did not set flavour. Get flavour from the executing kernel.
	kver=$(uname -r)
	if [ -f /boot/vmlinuz-$kver ]; then
		kernelfilename=vmlinuz-$kver
	elif [ -f /boot/vmlinux-$kver ]; then
		kernelfilename=vmlinux-$kver
	else
		echo "Error: Not found kernel image for running kernel!!!"
		exit 1
	fi
fi
}

# Find last dtb with required flavour in the system
dtbdirname_get () {
dtbdirname=( $(ls "$dtbpath" | grep "$kver") )
if [ ! -d "$dtbpath/$dtbdirname" ]; then
	echo "Error: $dtbpath/$dtbdirname not found"
	exit 1
fi
}

# Test labels in extlinux.conf and find last label with required flavour
change_default_label () {
labelarray=( $(grep label "$extlinuxconf" | grep "$kver" | sed 's/^label //') )
if [ "${#labelarray[@]}" -eq 0 ]
then
	echo "Error: No label with flavour $1 in $extlinuxconf. Corrupted?"
	exit 1
else
	needlabel="${labelarray[0]}"
	for i in "${labelarray[@]}"
	do
		if [ "$(rpmvercmp $i $needlabel)" -eq 1 ]
		then
			needlabel="$i"
		fi
	done
# Change default label
	sed -i "s/^default \S\+$/default $needlabel/" "$extlinuxconf"
fi
}

# Set boot mode to u-boot
set_boot_uboot () {
# Check and ensure uboot-rpi_(3)4.bin is present
# If there is no file, then copy the required
# kernel(7)8.img with renaming.
[ -f "$bootmountpoint/$ubootfile3" ] ||
  cp "$ubootpath3/$ubootfile" "$bootmountpoint/$ubootfile3"
[ -f "$bootmountpoint/$ubootfile4" ] ||
  cp "$ubootpath/$ubootfile" "$bootmountpoint/$ubootfile4"
# Backup config.txt before changing
cp -f "$configpath" "$bootmountpoint/config.bkp"
# Copy default file
cp -f "$rpi4bspath/config.txt" "$bootmountpoint"
}

# Copy dtb and overlays
cp_dtb_ovl () {
# Rewrite dtb on BOOT
cd "$dtbpath/$dtbdirname"
cp -f $rpidtb "$bootmountpoint/"
# Change overlays on BOOT
rm -rf "$bootmountpoint/overlays"
cp -rf "overlays" "$bootmountpoint/" || echo \
"Warning! Overlays is not found, dtoverlay in config.txt cannot be used."
cd - >/dev/null
}
