#!/bin/bash
set -e

function get_root_device
{
	local UUID=""

	for arg in $(cat /proc/cmdline); do
		case $arg in
		"root="*)
			UUID=${arg#root=}
			break
			;;

		*)
			;;

		esac
	done
	[[ -z ${UUID} ]] && return ""
	local PARTITION=$(blkid | grep "${UUID#PARTUUID=}" | cut -d':' -f1)
	if [[ -z ${PARTITION} ]] ; then
		local PARTITION=$(cat /proc/mounts  |grep " / " | cut -d' ' -f1)
		[[ -z ${PARTITION} ]] && return ""
	fi
	DEVICE=$(lsblk -n -o PKNAME "${PARTITION}" | head -n1)

	# Try regex expression
	if [[ -z ${DEVICE} ]]; then
		[[ ${PARTITION} =~ "/dev/mmcblk"[0-9]+ ]] || \
		[[ ${PARTITION} =~ "/dev/sd"[a-z] ]] && \
		DEVICE=${BASH_REMATCH[0]}
	else
		DEVICE="/dev/${DEVICE}"
	fi

	echo ${DEVICE}
}

function install_sunxi
{
	DEV=$1
	TARGET="/usr/lib/u-boot-olinuxino/${TARGET}/u-boot-sunxi-with-spl.bin"

	if [[ ! -e ${TARGET} ]]; then
		echo >&2 "$0: the file \'${TARGET}\' is missing"
		exit 1
	fi

	# Try to detect the root device
	if [[ -z ${DEV} ]]; then
		DEV=$(get_root_device)
		if [[ -z ${DEV} ]]; then
			echo >&2 "$0: failed to detect the root device"
			exit 1
		fi
	fi

	if [[ ! -w ${DEV} ]]; then
		echo >&2 "$0: device/image ($DEV) must be writable"
		exit 1
	fi

	DEV=$(readlink -f ${DEV})

	echo "Writing u-boot-sunxi-with-spl"
	if [[ -c ${DEV} ]] && [[ ${DEV} == "/dev/mtd"* ]]; then
		flashcp ${TARGET} ${DEV}
	else
		dd if=${TARGET} of=${DEV} bs=1k seek=8 conv=notrunc
	fi

}

function install_stm32mp1xx
{
	DEV=$1

	SPL="/usr/lib/u-boot-olinuxino/${TARGET}/u-boot-spl.stm32"

	if [[ ! -e ${SPL} ]]; then
		echo >&2 "$0: the file \'${SPL}\' is missing"
		exit 1
	fi

	UBOOT="/usr/lib/u-boot-olinuxino/${TARGET}/u-boot.img"

	if [[ ! -e ${UBOOT} ]]; then
		echo >&2 "$0: the file \'${UBOOT}\' is missing"
		exit 1
	fi

	# Try to detect the root device
	if [[ -z ${DEV} ]]; then
		DEV=$(get_root_device)
		if [[ -z ${DEV} ]]; then
			echo >&2 "$0: failed to detect the root device"
			exit 1
		fi
	fi

	if [[ ! -w ${DEV} ]]; then
		echo >&2 "$0: device/image ($DEV) must be writable"
		exit 1
	fi

	echo "Writing u-boot"
	if [ -e "${DEV}boot0" ] ; then
		SYSDEV="${DEV#/dev/}"
		echo 0 > /sys/block/${SYSDEV}boot0/force_ro
		echo 0 > /sys/block/${SYSDEV}boot1/force_ro
		dd if=${SPL} of=${DEV}boot0
		dd if=${SPL} of=${DEV}boot1
		dd if=${UBOOT} of=${DEV}p1
		mmc bootpart enable 1 1 ${DEV}
	else
		dd if=${SPL} of=${DEV}p1
		dd if=${SPL} of=${DEV}p2
		dd if=${UBOOT} of=${DEV}p3
	fi
}

# Detect family
for comp in $(cat "/proc/device-tree/compatible" | tr '\0' '\n'); do
	if [[ "${comp}" == "allwinner,sun"* ]]; then
		soc=$(cut -d',' -f2 <<< ${comp})
	elif [[ "${comp}" == "st,stm32mp1"* ]]; then
		soc=$(cut -d',' -f2 <<< ${comp})
	fi
	if [[ "${comp}" == "olimex,"* ]]; then
		board=$(cut -d',' -f2 <<< ${comp})
	fi
done

if [[ -z "${TARGET}" ]]; then
	case ${soc} in
		"sun4i-a10")
                        TARGET="a10-olinuxino"
                        INSTALL_FUNC=install_sunxi
                        ;;

		"sun5i-a13")
			TARGET="a13-olinuxino"
			case ${board} in
				"a13-som"*)
					TARGET="a13-som"
				;;
			esac
			INSTALL_FUNC=install_sunxi
			;;

		"sun7i-a20")
			TARGET="a20-olinuxino"
			INSTALL_FUNC=install_sunxi
			;;

		"sun8i-s3")
                        TARGET="s3-olinuxino"
                        INSTALL_FUNC=install_sunxi
                        ;;

		"sun50i-a64")
			TARGET="a64-olinuxino"
			INSTALL_FUNC=install_sunxi
			;;

		"stm32mp1"*)
			if [[ "${board}" == "stm32mp1xx-olinuxino-lime" ]] ; then
				TARGET="stm32mp1-olinuxino-lime"
			fi
			if [[ "${board}" == "stm32mp1xx-olinuxino-som" ]] ; then
				TARGET="stm32mp1-olinuxino-som"
			fi
			if [[ "${board}" == "stm32mp1xx-olinuxino-som-base" ]] ; then
				TARGET="stm32mp1-olinuxino-som-base"
			fi
			INSTALL_FUNC=install_stm32mp1xx
			;;

		*)
			echo >&2 "$0: Unsupported sunxi family: ${soc}."
			exit 1;
			;;
	esac
fi

${INSTALL_FUNC} $@
