#!/bin/sh
usage() {
    echo "
Usage: $(basename ${0}) <options>
	--update-config.txt
	--enable-serial
"
}

# check the args
while [ $# -gt 0 ]; do
        case $1 in
                --debug)
                        set -x
                        ;;
                -h|--help)
                        usage
                        exit 0
                        ;;
                --update-config.txt)
                        NEW_CONFIG=1
                        ;;
		--enable-serial)
			SERIAL=1
			;;
		--reboot)
			REBOOT=1
			;;
	esac
	shift
done
# ensure sudo user
if [ "$(whoami)" != "root" ]; then
	echo "Error: This script requires 'sudo' privileges in order to write to disk & mount media."
	exit 1
fi

if [ "$(uname -m)" == "aarch64" ] ; then
	ARCH="aarch64"
	DESTDIR="/boot/efi"
	UBOOT=$(rpm -q uboot-images-armv8)
	UBOOTPKG=uboot-images-armv8
else
	ARCH="armhf"
	DESTDIR="/boot/fw"
	UBOOT=$(rpm -q uboot-images-armv7)
	UBOOTPKG=uboot-images-armv7
fi
FW=$(rpm -q bcm283x-firmware)

echo
echo "= This will update U-Boot and the "
echo "= Raspberry Pi Firmware to the latest."

if [ "$UBOOT" = "package ${UBOOTPKG} is not installed" ]; then
	echo
	echo "$UBOOT"
	read -p "Would you like to install ${UBOOTPKG} (yes or no)? " INSTALL_UBOOT
	if [ "$(echo ${INSTALL_UBOOT} | tr [:lower:] [:upper:])" = "YES" ]; then
		dnf install -y ${UBOOTPKG}
		UBOOT=$(rpm -q ${UBOOTPKG})
	fi
fi
if [ "$FW" = "package bcm283x-firmware is not installed" ]; then
	echo
	echo "$FW"
	read -p "Would you like to install bcm283x-firmware (yes or no)? " INSTALL_FW
	if [ "$(echo ${INSTALL_FW} | tr [:lower:] [:upper:])" = "YES" ]; then
		dnf install -y bcm283x-firmware
		FW=$(rpm -q bcm283x-firmware)
	fi
fi
if [ "$FW" = "package bcm283x-firmware is not installed" ] && [ "$UBOOT" = "package ${UBOOTPKG} is not installed" ]; then
	echo
	echo "= Nothing to update, exiting."
	exit 0
fi
echo
echo "= Uboot: $UBOOT"
echo "= Firmware: $FW"
if [ "${ARCH}" == "aarch64" ]; then
	# dont mount or umount when aarch64 is efi
	# copy uboot
	if [ -f /usr/share/uboot/rpi_3/u-boot.bin ]; then
		cp -rp /usr/share/uboot/rpi_3/u-boot.bin /boot/efi/rpi3-u-boot.bin
	else
		echo "= Missing U-Boot for Raspberry Pi 3 Aarch64, no file copied."
	fi
	# copy fw
	if [ -d /usr/share/bcm283x-firmware ]; then
		cp -rfp /usr/share/bcm283x-firmware/*.{bin,dat,elf} /boot/efi/
		if [ "$NEW_CONFIG" = "1" ]; then
                        echo "= Using new config.txt."
                        echo "= Existing config.txt saved as config.txt.old."
                        mv /boot/efi/config.txt /boot/efi/config.txt.old
                        cp /usr/share/bcm283x-firmware/config-64.txt /boot/efi/config.txt
                fi
		if [ "$SERIAL" = "1" ]; then
                        echo "= Enabling serial console."
                        sed -i "s|# enable_uart=1|enable_uart=1|" /boot/efi/config.txt
                fi
	else
		echo "= Missing bcm283x-firmware, no files copied."
	fi
else
	if [ -d /boot/fw ]; then
		mount /dev/mmcblk0p1 /boot/fw
	else
		mkdir /boot/fw
		mount /dev/mmcblk0p1 /boot/fw
	fi
	# copy uboot
	if [ -f /usr/share/uboot/rpi_2/u-boot.bin ]; then
		cp -rp /usr/share/uboot/rpi_2/u-boot.bin /boot/fw/rpi2-u-boot.bin
	else
		echo "= Missing U-Boot for Raspberry Pi 2, no file copied."
	fi
	if [ -f /usr/share/uboot/rpi_3_32b/u-boot.bin ]; then
		cp -rp /usr/share/uboot/rpi_3_32b/u-boot.bin /boot/fw/rpi3-u-boot.bin
	else
		echo "= Missing U-Boot for Raspberry Pi 3, no file copied."
	fi
	# copy fw
	if [ -d /usr/share/bcm283x-firmware ]; then
		cp -rfp /usr/share/bcm283x-firmware/*.{bin,dat,elf} /boot/fw/
		if [ "$NEW_CONFIG" = "1" ]; then
			echo "= Using new config.txt."
			echo "= Existing config.txt saved as config.txt.old."
			mv /boot/fw/config.txt /boot/fw/config.txt.old
			cp /usr/share/bcm283x-firmware/config.txt /boot/fw/
		fi
		if [ "$SERIAL" = "1" ]; then
			echo "= Enabling serial console."
			sed -i "s|# enable_uart=1|enable_uart=1|" /boot/fw/config.txt
		fi
	else
		echo "Missing bcm283x-firmware, no files copied."
	fi
	umount /boot/fw
fi
echo
# reboot after writing
if [ "REBOOT" = "1" ]; then
        echo "= Complete, rebooting.."
        reboot
else
        echo "= Complete!"
fi
