#!/bin/sh

# Copyright (C) 2013 Red Hat Inc.
# SPDX-License-Identifier:  GPL-2.0+

# This script will download and install uboot

# usage message
usage() {
    echo "
Usage: $(basename ${0}) <options>

	--media=DEVICE	- media device file (/dev/[sdX|mmcblkX])
	--target=TARGET	- target board

optional
        --tag=KOJI TAG  - koji tag to download build
        --reboot        - reboot after update

Example: $(basename ${0}) --target=panda --media=/dev/mmcblk0

For list of supported boards please check SUPPORTED-BOARDS file.
"
}

# check the args
while [ $# -gt 0 ]; do
	case $1 in
		--debug)
			set -x
			;;
		-h|--help)
			usage
			;;
		--target*)
			if echo $1 | grep '=' >/dev/null ; then
				TARGET=$(echo $1 | sed 's/^--target=//')
			else
				TARGET=$2
				shift
			fi
			;;
		--media*)
			if echo $1 | grep '=' >/dev/null ; then
				MEDIA=$(echo $1 | sed 's/^--media=//')
			else
				MEDIA=$2
				shift
			fi
			;;
		--tag*)
			if echo $1 | grep '=' >/dev/null ; then
				KOJI_TAG=$(echo $1 | sed 's/^--tag=//')
			else
				KOJI_TAG=$2
				shift
			fi
			;;
                 --reboot)
                         REBOOT=1
                        ;;
		*)
			echo "$(basename ${0}): Error - ${1}"
			usage
			exit 1
			;;
	esac
	shift
done

if [ -d "/usr/share/arm-image-installer/boards.d" ]; then
	BOARDDIR="/usr/share/arm-image-installer/boards.d"
else
	DIR=$(dirname $0)
	BOARDDIR="${DIR}/boards.d"
fi
# 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

# check if media exists
if [[ ! -e $MEDIA ]]; then
	echo "Missing media"
	usage
	exit 1
fi

if [[ $TARGET = '' ]]; then
	echo "Missing target"
	usage
	exit 1
fi
if [[ $KOJI_TAG != '' ]]; then
	if [[ ! -f /usr/bin/koji ]]; then
		echo "Please install koji-utils for this option."
		exit 1
	else
		PREFIX='/tmp/root/'
		rm -rf /tmp/root &> /dev/null
		mkdir $PREFIX

		#get the latest uboot
		pushd $PREFIX &> /dev/null
		if [ $KOJI_TAG = f22 ]; then
			koji download-build --arch=armv7hl --latestfrom=$KOJI_TAG uboot-tools
		else
			koji download-build --arch=noarch --latestfrom=$KOJI_TAG uboot-tools
		fi
		# unpack uboot
		for rpm in uboot-images*.rpm
		do
			rpm2cpio "${rpm}" | cpio -idv &> /dev/null
		done
		popd &> /dev/null
	fi
fi
# determine uboot and write to disk
if [ "$TARGET" != "" ]; then
        if [ "$TARGET" = "rpi2" ] || [ "$TARGET" = "rpi3" ] || [ "$TARGET" = "olpc_xo175" ]; then
                . "${BOARDDIR}/${TARGET}"
        elif [ -d "${PREFIX}/usr/share/uboot/${TARGET}" ]; then
                . "${BOARDDIR}/${TARGET}"
        else
                echo "= No U-Boot files found for $TARGET."
        fi
else
        echo "= No U-boot will be written."
        TARGET="Mystery Board"
fi

# reboot after writing
if [ "REBOOT" = "1" ]; then 
        echo "= Complete, rebooting.."
        reboot
else
        echo "= Complete!"
fi

# vi: tabstop=8 softtabstop=0 expandtab shiftwidth=8 smarttab
